From 4a41a076350a3938db1fd059e8fad066272c1336 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Mon, 15 Oct 2012 21:51:56 -0400 Subject: [PATCH] Added namespace/prefix to the caching class Closes #3 taking the issues down to zero :-O --- classes/Cache.php | 33 ++++++++++++++++++++++++--------- jar.php | 33 ++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/classes/Cache.php b/classes/Cache.php index 2d9458b..6591311 100644 --- a/classes/Cache.php +++ b/classes/Cache.php @@ -40,7 +40,7 @@ class Cache extends Object * @access private * @var string */ - private $hostname = null; + private $hostname = 'localhost'; /** * Port to use to connect @@ -48,7 +48,15 @@ class Cache extends Object * @access private * @var integer */ - private $port = null; + private $port = 11211; + + /** + * Namespace (prefix) + * + * @access private + * @var string + */ + private $namespace = ''; /** * Connection resource to Memcached @@ -76,13 +84,20 @@ class Cache extends Object { $datasource = $this->config->datasources[$this->config->pickles['cache']]; - if (isset($datasource['hostname'], $datasource['port'])) + foreach (array('hostname', 'port', 'namespace') as $variable) { - $this->hostname = $datasource['hostname']; - $this->port = $datasource['port']; + if (isset($datasource[$variable])) + { + $this->$variable = $datasource[$variable]; + } } } } + + if ($this->namespace != '') + { + $this->namespace .= '-'; + } } /** @@ -140,7 +155,7 @@ class Cache extends Object { if ($this->open()) { - return $this->connection->get($key); + return $this->connection->get($this->namespace . $key); } return false; @@ -164,7 +179,7 @@ class Cache extends Object { if ($this->open()) { - return $this->connection->set($key, $value, 0, $expire); + return $this->connection->set($this->namespace . $key, $value, 0, $expire); } return false; @@ -182,7 +197,7 @@ class Cache extends Object { if ($this->open()) { - return $this->connection->delete($key); + return $this->connection->delete($this->namespace . $key); } return false; @@ -201,7 +216,7 @@ class Cache extends Object { if ($this->open()) { - return $this->connection->increment($key); + return $this->connection->increment($this->namespace . $key); } return false; diff --git a/jar.php b/jar.php index fbd712d..09cb243 100755 --- a/jar.php +++ b/jar.php @@ -473,7 +473,7 @@ class Cache extends Object * @access private * @var string */ - private $hostname = null; + private $hostname = 'localhost'; /** * Port to use to connect @@ -481,7 +481,15 @@ class Cache extends Object * @access private * @var integer */ - private $port = null; + private $port = 11211; + + /** + * Namespace (prefix) + * + * @access private + * @var string + */ + private $namespace = ''; /** * Connection resource to Memcached @@ -509,13 +517,20 @@ class Cache extends Object { $datasource = $this->config->datasources[$this->config->pickles['cache']]; - if (isset($datasource['hostname'], $datasource['port'])) + foreach (array('hostname', 'port', 'namespace') as $variable) { - $this->hostname = $datasource['hostname']; - $this->port = $datasource['port']; + if (isset($datasource[$variable])) + { + $this->$variable = $datasource[$variable]; + } } } } + + if ($this->namespace != '') + { + $this->namespace .= '-'; + } } /** @@ -573,7 +588,7 @@ class Cache extends Object { if ($this->open()) { - return $this->connection->get($key); + return $this->connection->get($this->namespace . $key); } return false; @@ -597,7 +612,7 @@ class Cache extends Object { if ($this->open()) { - return $this->connection->set($key, $value, 0, $expire); + return $this->connection->set($this->namespace . $key, $value, 0, $expire); } return false; @@ -615,7 +630,7 @@ class Cache extends Object { if ($this->open()) { - return $this->connection->delete($key); + return $this->connection->delete($this->namespace . $key); } return false; @@ -634,7 +649,7 @@ class Cache extends Object { if ($this->open()) { - return $this->connection->increment($key); + return $this->connection->increment($this->namespace . $key); } return false;