Added namespace/prefix to the caching class

Closes #3 taking the issues down to zero :-O
This commit is contained in:
Josh Sherman 2012-10-15 21:51:56 -04:00
parent 1154a1d901
commit 4a41a07635
2 changed files with 48 additions and 18 deletions

View file

@ -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,15 +84,22 @@ 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 .= '-';
}
}
/**
* Destructor
*
@ -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;

33
jar.php
View file

@ -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,15 +517,22 @@ 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 .= '-';
}
}
/**
* Destructor
*
@ -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;