Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to use with memcached #24

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 60 additions & 11 deletions Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ class Cache implements CacheInterface
public function __construct(CacheClientInterface $client = null)
{
if (!empty($client)) {
if (is_object($client) && ($client instanceof CacheClientInterface)) {
$this->client = $client;
}
else {
throw new \Exception('Invalid Cache Client Interface');
}
$this->setClient($client);
}
}

Expand Down Expand Up @@ -74,9 +69,9 @@ public function setTtl($ttl)
*/
public function setClient(CacheClientInterface $client)
{
if (is_object($client) && ($client instanceof CacheClientInterface))
if (is_object($client) && ($client instanceof CacheClientInterface)) {
$this->client = $client;
else {
} else {
throw new \Exception('Invalid Cache Client Interface');
}
}
Expand All @@ -91,6 +86,10 @@ public function setClient(CacheClientInterface $client)
public function get($key)
{
if ($this->isSafe() && !empty($key)) {
if (is_array($key)) {
return $this->client->getMulti($key);
}

return $this->client->get($key);
}

Expand All @@ -117,20 +116,70 @@ public function set($key, $value, $ttl = null)
return false;
}

/**
* Add a set of values to the memcached
*
* @param array $values An array of pairs key-value.
* @param int $ttl Number of seconds for the value to be valid for
* @access public
* @return void
*/
public function setMulti($values, $ttl)
{
return $this->client->setMulti($values, $ttl);
}

/**
* Delete a key from the cache
*
* @param string $key Unique key
* @param string|array $key Unique key or a bunch of unique keys
* @access public
* @return void
*/
public function delete($key)
{
if ($this->isSafe() && !empty($key)) {
if ($this->isSafe()) {
if (is_array($key)) {
return $this->client->deleteMulti($key);
}

return $this->client->delete($key);
}
}

return false;
/**
* Delete a set of values from the memcached
*
* @param array $regex Regular Expression
* @param array $time Time to wait before delete
* @access public
* @return void
*/
public function deleteRegex($regex, $time = 0)
{
return $this->client->deleteMultiRegex($regex, $time);
}

/**
* Get all the cache keys
*
* @access public
* @return void
*/
public function getKeys()
{
return $this->client->getKeys();
}

/**
* Clear all the cache
*
* @access public
* @return void
*/
public function flush()
{
return $this->client->flush();
}

/**
Expand Down
148 changes: 130 additions & 18 deletions Client/MemcacheClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Beryllium\CacheBundle\CacheClientInterface;

/**
* Client interface for Memcache servers
* Client interface for Memcached servers
*
* @uses CacheClientInterface
* @package
Expand All @@ -15,31 +15,31 @@
*/
class MemcacheClient implements CacheClientInterface
{
const PREFIX_MAX_LENGTH = 128;

protected $safe = false;
protected $mem = null;
protected $servers = array();
protected $sockttl = 0.2;
protected $compression = false;
protected $prefix = '';

/**
* Constructs the cache client using an injected Memcache instance
*
* @access public
*/
public function __construct(\Memcache $memcache)
public function __construct(\Memcached $memcached)
{
$this->mem = $memcache;
$this->mem = $memcached;
}

/**
* Add a server to the memcache pool.
* Add a server to the memcached pool.
*
* Does not probe server, does not set Safe to true.
*
* Should really be private, or modified to handle the probeServer action itself.
*
* @param string $ip Location of memcache server
* @param string $ip Location of memcached server
* @param int $port Optional: Port number (default: 11211)
* @access public
* @return void
Expand All @@ -52,7 +52,7 @@ public function addServer($ip, $port = 11211)
}

/**
* Add an array of servers to the memcache pool
* Add an array of servers to the memcached pool
*
* Uses ProbeServer to verify that the connection is valid.
*
Expand Down Expand Up @@ -95,7 +95,7 @@ public function addServers(array $servers)
* flawed way to go about this.
*
* @param string $ip IP address (or hostname, possibly)
* @param int $port Port that memcache is running on
* @param int $port Port that memcached is running on
* @access public
* @return boolean True if the socket opens successfully, or false if it fails
*/
Expand All @@ -115,24 +115,41 @@ public function probeServer($ip, $port)
}

/**
* Retrieve a value from memcache
* Retrieve a value from memcached
*
* @param string|array $key Unique identifier or array of identifiers
* @param string $key Unique identifier
* @access public
* @return mixed Requested value, or false if an error occurs
*/
public function get($key)
{
if ($this->isSafe()) {
$key = $this->prefix . $key;

return $this->mem->get($key);
}

return false;
}

/**
* Add a value to the memcache
* Retrieve a set of values from memcached
*
* @param array $keys of Unique identifiers
* @access public
* @return mixed Requested value, or false if an error occurs
*/
public function getMulti($keys)
{
if ($this->isSafe()) {

return $this->mem->getMulti($keys);
}

return false;
}

/**
* Add a value to the memcached
*
* @param string $key Unique key
* @param mixed $value A value. I recommend a string, be it serialized or not - other values haven't been tested :)
Expand All @@ -143,15 +160,32 @@ public function get($key)
public function set($key, $value, $ttl)
{
if ($this->isSafe()) {
$key = $this->prefix . $key;
return $this->mem->set($key, $value, $this->compression, $ttl);

return $this->mem->set($key, $value, $ttl);
}

return false;
}

/**
* Delete a value from the memcache
* Add a set of values to the memcached
*
* @param array $values An array of pairs key-value.
* @param int $ttl Number of seconds for the value to be valid for
* @access public
* @return void
*/
public function setMulti($values, $ttl)
{
if ($this->isSafe()) {
return $this->mem->setMulti($values, $ttl);
}

return false;
}

/**
* Delete a value from the memcached
*
* @param string $key Unique key
* @access public
Expand All @@ -160,13 +194,79 @@ public function set($key, $value, $ttl)
public function delete($key)
{
if ($this->isSafe()) {
$key = $this->prefix . $key;

return $this->mem->delete($key, 0);
}

return false;
}

/**
* Delete a set of values from the memcached
*
* @param array $keys Unique key
* @param int $time Time to wait before delete
* @access public
* @return void
*/
public function deleteMulti($keys, $time = 0)
{
if ($this->isSafe()) {

return $this->mem->deleteMulti($keys, $time);
}

return false;
}

/**
* Delete a set of values from the memcached
*
* @param array $regex Regular Expression
* @param array $time Time to wait before delete
* @access public
* @return void
*/
public function deleteMultiRegex($regex, $time = 0)
{
if ($this->isSafe()) {
$keys = $this->getKeys();
$matchingKeys = array();

foreach ($keys as $key) {
if (preg_match($regex, $key)) {
$matchingKeys[] = $key;
}
}

return $this->mem->deleteMulti($matchingKeys, $time);
}

return false;
}

/**
* Get all the cache keys
*
* @access public
* @return void
*/
public function getKeys()
{
return $this->mem->getAllKeys();
}

/**
* Clear all the cache
*
* @access public
* @return void
*/
public function flush()
{
return $this->mem->flush();
}

/**
* Check if the cache is live
*
Expand Down Expand Up @@ -195,6 +295,18 @@ public function getStats()
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;
if (strlen($prefix) <= self::PREFIX_MAX_LENGTH) {
$this->mem->setOption(\Memcached::OPT_PREFIX_KEY, $prefix);
}
}

/**
* Set compression
*
* return self
*/
public function setCompression($status)
{
$this->mem->setOption(\Memcached::OPT_COMPRESSION, $status);
}
}
6 changes: 5 additions & 1 deletion DependencyInjection/BerylliumCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('beryllium_cache.client.prefix', $config['prefix']);
$container->setParameter('beryllium_cache.client.default_ttl', $config['default_ttl']);
$container->setParameter('beryllium_cache.client.compression', $config['compression']);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}
}
Loading