Skip to content

Instantly share code, notes, and snippets.

@arleighdickerson
Created May 1, 2016 05:16
Show Gist options
  • Save arleighdickerson/b008ceee358eedc551d6a18709f8a8cb to your computer and use it in GitHub Desktop.
Save arleighdickerson/b008ceee358eedc551d6a18709f8a8cb to your computer and use it in GitHub Desktop.
<?php
namespace common\components;
use Aws\S3\S3Client;
use yii\caching\Cache;
use yii\helpers\ArrayHelper;
class S3Cache extends Cache {
public $bucket;
public function __construct(S3Client $client, $config = []) {
$this->keyPrefix = '';
$this->_client = $client;
parent::__construct($config);
}
/**
* @var S3Client
*/
private $_client;
/**
* @return S3Client
*/
public function getClient() {
return $this->_client;
}
/**
* Retrieves a value from cache with a specified key.
* This method should be implemented by child classes to retrieve the data
* from specific cache storage.
* @param string $key a unique key identifying the cached value
* @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key) {
$time = time();
if ($this->getClient()->doesObjectExist($this->bucket, $this->buildKey($key))) {
$result = $this->getClient()->getObject([
'Bucket' => $this->bucket,
'Key' => $this->buildKey($key)
]);
$entry = unserialize($result['Body']);
$expiration = ArrayHelper::getValue($entry, 'expiration');
if (!$expiration || $expiration > $time) {
return ArrayHelper::getValue($entry, 'value');
}
}
return false;
}
/**
* Stores a value identified by a key in cache.
* This method should be implemented by child classes to store the data
* in specific cache storage.
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $duration) {
$entry = array_filter([
'value' => $value,
'expiration' => $duration ? time() + $duration : null
]);
return $this->getClient()->putObject([
'Bucket' => $this->bucket,
'Key' => $this->buildKey($key),
'Body' => serialize($entry)
]) !== null;
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This method should be implemented by child classes to store the data
* in specific cache storage.
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $duration) {
if (!$this->getClient()->doesObjectExist($this->bucket, $this->buildKey($key))) {
return $this->setValue($key, $value, $duration);
}
return false;
}
/**
* Deletes a value with the specified key from cache
* This method should be implemented by child classes to delete the data from actual cache storage.
* @param string $key the key of the value to be deleted
* @return boolean if no error happens during deletion
*/
protected function deleteValue($key) {
$this->getClient()->deleteMatchingObjects($this->bucket, $this->keyPrefix, "/" . $this->buildKey($key) . "/");
return true;
}
/**
* Deletes all values from cache.
* Child classes may implement this method to realize the flush operation.
* @return boolean whether the flush operation was successful.
*/
protected function flushValues() {
$this->getClient()->deleteMatchingObjects($this->bucket, $this->keyPrefix, "/.*/");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment