Skip to content

Instantly share code, notes, and snippets.

@cooperaj
Last active August 29, 2015 14:16
Show Gist options
  • Save cooperaj/2fffc52808064731ffe2 to your computer and use it in GitHub Desktop.
Save cooperaj/2fffc52808064731ffe2 to your computer and use it in GitHub Desktop.

Revisions

  1. cooperaj revised this gist Mar 3, 2015. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions ResilientCache.php
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    use Doctrine\Common\Cache\Cache;

    class ResilientCache
    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, $newDataFunction)
    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.
    *
  2. cooperaj revised this gist Mar 3, 2015. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions ResilientCache.php
    Original 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) && $expired) // if we don't have new data but we have old expired data
    if (is_null($newData) && ! is_null($data)) // if we don't have new data but we have old data
    return $data;

    return $newData; // we have new data. return it.

    if ( ! is_null($newData))
    return $newData; // we have new data. return it.

    return false;
    }

    /**
  3. cooperaj revised this gist Mar 3, 2015. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions ResilientCache.php
    Original file line number Diff line number Diff line change
    @@ -28,8 +28,6 @@ public function __construct(Cache $cache) {
    */
    public function fetch($id, $newDataFunction)
    {
    $expired = false;

    // attempt to fetch out of the underlying cache
    if ($cacheObj = $this->cache->fetch($id)) {
    $data = $cacheObj->data;
  4. cooperaj revised this gist Mar 3, 2015. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions Usage
    Original 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);
    }
  5. cooperaj created this gist Mar 3, 2015.
    128 changes: 128 additions & 0 deletions ResilientCache.php
    Original 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();
    }
    }