Created
February 6, 2014 13:30
-
-
Save fubhy/8844140 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Guzzle\Cache\CacheAdapterInterface; | |
/** | |
* Custom cache adapter. | |
* | |
* Leverages the default Drupal cache bin system for caching Guzzle responses. | |
*/ | |
class DrupalCacheAdapter implements CacheAdapterInterface { | |
/** | |
* The cache object. | |
* | |
* @var \DrupalCacheInterface | |
*/ | |
protected $cache; | |
/** | |
* Constructs a SugarCacheAdapter instance. | |
* | |
* @param string $bin | |
* (Optional) The cache bin to use. | |
*/ | |
function __construct($bin) { | |
$this->cache = _cache_get_object($bin); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function contains($id, array $options = NULL) { | |
if ($this->cache->get($id)) { | |
return TRUE; | |
} | |
return FALSE; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function delete($id, array $options = NULL) { | |
$this->cache->clear($id); | |
return TRUE; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function fetch($id, array $options = NULL) { | |
if ($cache = $this->cache->get($id)) { | |
return $cache->data; | |
} | |
return FALSE; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function save($id, $data, $lifetime = FALSE, array $options = NULL) { | |
$lifetime = $lifetime === FALSE ? CACHE_PERMANENT : REQUEST_TIME + $lifetime; | |
$this->cache->set($id, $data, $lifetime); | |
return TRUE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment