-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support memcached and redis for caching
- Loading branch information
1 parent
4b2b9ce
commit fce240d
Showing
5 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
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
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); | ||
} | ||
} |
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
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); | ||
} | ||
} |