Last active
August 29, 2015 14:16
-
-
Save cooperaj/2fffc52808064731ffe2 to your computer and use it in GitHub Desktop.
Revisions
-
cooperaj revised this gist
Mar 3, 2015 . 1 changed file with 11 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ use Doctrine\Common\Cache\Cache; class ResilientCache implements Cache { /** * @var Cache @@ -26,8 +26,11 @@ public function __construct(Cache $cache) { * * @return mixed The cached data or FALSE, if no cache entry exists for the given id. */ public function fetch($id) { if (func_num_args() > 1) $newDataFunction = func_get_arg(1); // attempt to fetch out of the underlying cache if ($cacheObj = $this->cache->fetch($id)) { $data = $cacheObj->data; @@ -37,6 +40,9 @@ public function fetch($id, $newDataFunction) if ( ! is_null($data) && ! $expired) // do we have a valid cache entry return $data; if (is_null($newDataFunction)) return false; if (is_null($data) || is_null($expired) || $expired) { // if we don't have any data or we have expired data try { $newData = $newDataFunction(); @@ -45,13 +51,13 @@ public function fetch($id, $newDataFunction) if (is_null($newData) && ! is_null($data)) // if we don't have new data but we have old data return $data; if ( ! is_null($newData)) return $newData; // we have new data. return it. return false; } /** * Tests if an entry exists in the cache. * -
cooperaj revised this gist
Mar 3, 2015 . 1 changed file with 6 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -43,10 +43,13 @@ public function fetch($id, $newDataFunction) } catch(\Exception $ex) {} } if (is_null($newData) && ! is_null($data)) // if we don't have new data but we have old data return $data; if ( ! is_null($newData)) return $newData; // we have new data. return it. return false; } /** -
cooperaj revised this gist
Mar 3, 2015 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -28,8 +28,6 @@ public function __construct(Cache $cache) { */ public function fetch($id, $newDataFunction) { // attempt to fetch out of the underlying cache if ($cacheObj = $this->cache->fetch($id)) { $data = $cacheObj->data; -
cooperaj revised this gist
Mar 3, 2015 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ $twitter_service = $this->twitter_service; $tweets = $this->cache->fetch($user . $number_to_show, function() use ($twitter_service, $options) { return $twitter_service->getTimeline($options); }); if ($tweets) { $this->cache->save($user . $number_to_show, $tweets, 120); } -
cooperaj created this gist
Mar 3, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,128 @@ <?php namespace Bolt\Extension\Cooperaj\BoltTwitter; use Doctrine\Common\Cache\Cache; class ResilientCache { /** * @var Cache */ private $cache; /** * @param Cache $cache */ public function __construct(Cache $cache) { $this->cache = $cache; } /** * Fetches an entry from the cache. * * @param string $id The id of the cache entry to fetch. * @param $newDataFunction Callable A callback function to allow the fetching of new data. * * @return mixed The cached data or FALSE, if no cache entry exists for the given id. */ public function fetch($id, $newDataFunction) { $expired = false; // attempt to fetch out of the underlying cache if ($cacheObj = $this->cache->fetch($id)) { $data = $cacheObj->data; $expired = $cacheObj->lifeTime < time(); } if ( ! is_null($data) && ! $expired) // do we have a valid cache entry return $data; if (is_null($data) || is_null($expired) || $expired) { // if we don't have any data or we have expired data try { $newData = $newDataFunction(); } catch(\Exception $ex) {} } if ( is_null($newData) && ! is_null($data) && $expired) // if we don't have new data but we have old expired data return $data; return $newData; // we have new data. return it. } /** * Tests if an entry exists in the cache. * * @param string $id The cache id of the entry to check for. * * @return boolean TRUE if a cache entry exists for the given cache id, FALSE otherwise. */ public function contains($id) { return $this->cache->contains($id); // if it's in here it'll return it because infinite expiration. } /** * Puts data into the cache. * * @param string $id The cache id. * @param mixed $data The cache entry/data. * @param int $lifeTime The cache lifetime. * If != 0, sets a specific lifetime for this cache entry (0 => infinite lifeTime). * * @return boolean TRUE if the entry was successfully stored in the cache, FALSE otherwise. */ public function save($id, $data, $lifeTime = 0) { $cacheObj = new \stdClass(); $cacheObj->data = $data; if ($lifeTime > 0) { $cacheObj->lifeTime = time() + $lifeTime; } return $this->cache->save($id, $cacheObj, 0); // set to an infinite expiration. } /** * Deletes a cache entry. * * @param string $id The cache id. * * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise. */ public function delete($id) { return $this->cache->delete($id); } /** * Retrieves cached information from the data store. * * The server's statistics array has the following values: * * - <b>hits</b> * Number of keys that have been requested and found present. * * - <b>misses</b> * Number of items that have been requested and not found. * * - <b>uptime</b> * Time that the server is running. * * - <b>memory_usage</b> * Memory used by this server to store items. * * - <b>memory_available</b> * Memory allowed to use for storage. * * @since 2.2 * * @return array|null An associative array with server's statistics if available, NULL otherwise. */ public function getStats() { return $this->cache->getStats(); } }