Skip to content

Commit

Permalink
Support memcached and redis for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
tystuyfzand committed May 24, 2018
1 parent 4b2b9ce commit fce240d
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 2 deletions.
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@
"psr-4": {
"SeaweedFS\\": "src/"
}
},
"suggest": {
"predis/predis": "Enables caching for volumes and metadata using redis.",
"ext-memcached": "Enables caching for volumes and metadata using memcached."
}
}
5 changes: 3 additions & 2 deletions src/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @package SeaweedFS\Cache
*/
interface CacheInterface {

/**
* Check if the cache implementation contains the specified key.
*
Expand All @@ -31,15 +32,15 @@ public function get($key, $default = null);
* @param $key
* @param $value
* @param int $minutes
* @return mixed
* @return void
*/
public function put($key, $value, $minutes = 0);

/**
* Remove a value from the cache.
*
* @param $key
* @return mixed
* @return void
*/
public function remove($key);
}
35 changes: 35 additions & 0 deletions src/Cache/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,37 @@
* @package SeaweedFS\Cache
*/
class FileCache implements CacheInterface {
/**
* @var string Base directory.
*/
private $baseDir;

/**
* Construct a new file backed cache with the specified base directory.
*
* @param $baseDir
*/
public function __construct($baseDir) {
$this->baseDir = $baseDir;
}

/**
* Check if the cache implementation contains the specified key.
*
* @param $key
* @return bool
*/
public function has($key) {
return file_exists($this->baseDir . '/' . md5($key));
}

/**
* Get the specified key from the cache implementation.
*
* @param $key
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null) {
if (!$this->has($key)) {
return $default;
Expand All @@ -26,10 +47,24 @@ public function get($key, $default = null) {
return @unserialize(file_get_contents($this->baseDir . '/' . md5($key))) ?: $default;
}

/**
* Set the value in the cache implementation.
*
* @param $key
* @param $value
* @param int $minutes
* @return void
*/
public function put($key, $value, $minutes = 0) {
file_put_contents($this->baseDir . '/' . md5($key), serialize($value));
}

/**
* Remove a value from the cache.
*
* @param $key
* @return void
*/
public function remove($key) {
return unlink($this->baseDir . '/' . md5($key));
}
Expand Down
74 changes: 74 additions & 0 deletions src/Cache/MemcachedCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace SeaweedFS\Cache;

/**
* Cache implementation using Memcached.
*
* @package SeaweedFS\Cache
*/
class MemcachedCache implements CacheInterface {

/**
* @var \Memcached The Memcached client.
*/
private $memcached;

/**
* Construct a new Memcached cache interface.
*
* @param \Memcached $memcached
*/
public function __construct(\Memcached $memcached) {
$this->memcached = $memcached;
}

/**
* Check if the cache implementation contains the specified key.
*
* @param $key
* @return bool
*/
public function has($key) {
return !is_null($this->get($key));
}

/**
* Get the specified key from the cache implementation.
*
* @param $key
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null) {
$value = $this->memcached->get($key);

if ($this->memcached->getResultCode() == 0) {
return $value;
}

return $default;
}

/**
* Set the value in the cache implementation.
*
* @param $key
* @param $value
* @param int $minutes
* @return void
*/
public function put($key, $value, $minutes = 0) {
$this->memcached->set($key, serialize($value), $minutes * 60);
}

/**
* Remove a value from the cache.
*
* @param $key
* @return void
*/
public function remove($key) {
$this->memcached->delete($key);
}
}
76 changes: 76 additions & 0 deletions src/Cache/RedisCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace SeaweedFS\Cache;

use Predis\Client as RedisClient;

/**
* Cache implementation using Predis.
*
* @package SeaweedFS\Cache
*/
class RedisCache implements CacheInterface {

/**
* @var RedisClient The redis client instance.
*/
private $client;

/**
* Construct a new Redis cache.
*
* @param RedisClient $client
*/
public function __construct(RedisClient $client) {
$this->client = $client;
}

/**
* Check if the cache implementation contains the specified key.
*
* @param $key
* @return bool
*/
public function has($key) {
return !is_null($this->get($key));
}

/**
* Get the specified key from the cache implementation.
*
* @param $key
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null) {
$value = $this->client->get($key);

return !is_null($value) ? unserialize($value) : $default;
}

/**
* Set the value in the cache implementation.
*
* @param $key
* @param $value
* @param int $minutes
* @return void
*/
public function put($key, $value, $minutes = 0) {
if ($minutes == 0) {
$this->client->set($key, serialize($value));
} else {
$this->client->set($key, serialize($value), null, $minutes * 60);
}
}

/**
* Remove a value from the cache.
*
* @param $key
* @return mixed
*/
public function remove($key) {
$this->client->del($key);
}
}

0 comments on commit fce240d

Please sign in to comment.