Skip to content

Caching ? #5

Description

@Phhere

Hi,
i think it would be greate if there would be a real caching function.
I extended the SteamAPIDriver with an easy caching function.
Maybe it is something which could be merged into the source code

<?php
namespace Neoseeker\SteamAPI;
class SteamAPIDriverCached extends SteamAPIDriver {
    protected $cacheDir = './cache/';
    protected $cacheTime = 300;
    protected $enabled = true;
    public function __construct() {
        parent::__construct();
    }

    public function get_xml($resource = null) {
        $location = $this->get_xml_location();
        $cacheFile = $this->cacheDir.md5($location).'.dat';
        if($this->enabled && file_exists($cacheFile) && filemtime($cacheFile) > time()-$this->cacheTime){
            $content = file_get_contents($cacheFile);
        }
        else{
            $content = file_get_contents($location);
            file_put_contents($cacheFile, $content);
        }
        $xml_response = @simplexml_load_string($content, null, LIBXML_NOCDATA);
        if ($xml_response == false) {
            return false;
        }
        return $xml_response;
    }
}

?>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions