Skip to content

Commit

Permalink
Merge pull request #2 from afosto/hotfix/cache-expiry
Browse files Browse the repository at this point in the history
Make sure the cache is deleted as expected
  • Loading branch information
bakkerpeter committed Oct 12, 2016
2 parents 5695633 + 809442f commit b9a5bc9
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/app/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
namespace Afosto\ActiveAnts;

class Cache {

/**
* Set the user session hash
* @var string
*/
private $_hash;

/**
* The path to the cache directory
* @var string
* @var string
*/
private $_cacheDirectory;

/**
* Set the directory used for caching
* @param string $hash
* @param string $hash
* @param string $cacheDirectory
* @throws ApiException
*/
Expand All @@ -29,19 +29,21 @@ public function __construct($hash, $cacheDirectory) {
throw new ApiException('Cache directory is not writable');
}
return $this;
}
}

/**
* Set the cache key value
* @param string $key
* @param string $contents
* @param integer $expiry Seconds untill expiration
* @param $key
* @param $contents
* @param $expiry
*
* @throws ApiException
*/
public function setCache($key, $contents, $expiry) {
if (file_put_contents($this->_cacheDirectory. '/' . $this->_getCacheKey($key), serialize($contents)) === FALSE) {
throw new ApiException('Cache storage failed');
}
touch($this->_cacheDirectory . '/' . $this->_getCacheKey($key), (time() + (int) $expiry));
touch($this->_cacheDirectory . '/' . $this->_getCacheKey($key), ((time() + (int) $expiry) - 1));
}

/**
Expand All @@ -59,7 +61,7 @@ public function getCache($key) {
}
return false;
}

/**
* Returns the cache key, based on client specific data
* @param string $key
Expand All @@ -68,5 +70,5 @@ public function getCache($key) {
private function _getCacheKey($key) {
return $this->_hash . '-' . $key . '.bin';
}

}

0 comments on commit b9a5bc9

Please sign in to comment.