Skip to content

Commit

Permalink
Update core cacheable class to handle namespaced interfaces if passed…
Browse files Browse the repository at this point in the history
…. RE: #2162
  • Loading branch information
brendo committed Nov 13, 2014
1 parent 5efdbe7 commit 5a14cbf
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions symphony/lib/core/class.cacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* The Cacheable class provides a wrapper around an `iCache` interface
* and provides basic CRUD functionality for caching. Historically,
* this class was hardcoded to use MySQL, but since Symphony 2.4 this
* this class was hardcoded to use MySQL, but since Symphony 2.4 this
* may not be the case anymore.
*/

Expand All @@ -30,16 +30,22 @@ class Cacheable
* @param iCache $cacheProvider
* If a `Symphony::Database()` is provided, the constructor
* will create a `CacheDatabase` interface. If `null`, or the
* `$cacheProvider` is not a class that implements `iCache`
* an `InvalidArgumentException` will be thrown.
* `$cacheProvider` is not a class that implements `iCache` or
* `iNamespacedCache` interface an `InvalidArgumentException` will be thrown.
*/
public function __construct($cacheProvider = null)
{
if (($cacheProvider instanceof MySQL)) {
$cache = new CacheDatabase($cacheProvider);
$this->cacheProvider = $cache;
} elseif(is_null($cacheProvider) || ($cacheProvider instanceof iCache)) {
throw new InvalidArgumentException('The cacheProvider must extend the iCache interface.');
} elseif (
is_null($cacheProvider)
|| (
$cacheProvider instanceof iCache === false
&& $cacheProvider instanceof iNamespacedCache === false
)
) {
throw new InvalidArgumentException('The cacheProvider must extend the iCache or iNamespacedCache interface.');
} else {
$this->cacheProvider = $cacheProvider;
}
Expand All @@ -60,17 +66,24 @@ public function getType()
* A wrapper for writing data in the cache.
*
* @param string $hash
* A
* A
* @param string $data
* The data to be cached
* @param integer $ttl
* A integer representing how long the data should be valid for in minutes.
* By default this is null, meaning the data is valid forever
* @param string $namespace
* Write an item and save in a namespace for ease of bulk operations
* later
* @return boolean
* If an error occurs, this function will return false otherwise true
*/
public function write($hash, $data, $ttl = null)
public function write($hash, $data, $ttl = null, $namespace = null)
{
if ($this->cacheProvider instanceof iNamespacedCache) {
return $this->cacheProvider->write($hash, $data, $ttl, $namespace);
}

return $this->cacheProvider->write($hash, $data, $ttl);
}

Expand All @@ -79,10 +92,16 @@ public function write($hash, $data, $ttl = null)
*
* @param string $hash
* The hash of the Cached object, as defined by the user
* @param string $namespace
* Read multiple items by a namespace
* @return mixed
*/
public function read($hash)
public function read($hash, $namespace = null)
{
if ($this->cacheProvider instanceof iNamespacedCache) {
return $this->cacheProvider->read($hash, $namespace);
}

return $this->cacheProvider->read($hash);
}

Expand All @@ -91,10 +110,16 @@ public function read($hash)
*
* @param string $hash
* The user defined hash of the data
* @param string $namespace
* Delete multiple items by a namespace
* @return boolean
*/
public function delete($hash = null)
public function delete($hash = null, $namespace = null)
{
if ($this->cacheProvider instanceof iNamespacedCache) {
return $this->cacheProvider->delete($hash, $namespace);
}

return $this->cacheProvider->delete($hash);
}

Expand Down

0 comments on commit 5a14cbf

Please sign in to comment.