Last active
August 29, 2015 14:05
-
-
Save fenos/9d59553a58ce82518500 to your computer and use it in GitHub Desktop.
Cache Helper
This file contains 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 characters
<?php namespace Core\Cache; | |
/** | |
* Class CacheMeRemainder | |
* | |
* @package Core\Cache | |
*/ | |
class CacheMeRemainder { | |
/** | |
* @var string | |
*/ | |
protected $classInstance; | |
/** | |
* @var string | |
*/ | |
protected $prefix; | |
/** | |
* @var string | |
*/ | |
private $key; | |
/** | |
* @var int | |
*/ | |
protected $lifeTime; | |
/** | |
* @param $className | |
*/ | |
function __construct($className,$prefix,$key,$lifeTime) | |
{ | |
$this->classInstance = $className; | |
$this->prefix = $prefix; | |
$this->lifeTime = $lifeTime; | |
$this->key = $key; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function cacheInstance() | |
{ | |
return \App::make('cache'); | |
} | |
/** | |
* @param $name | |
* @param $arguments | |
* @return mixed | |
*/ | |
function __call($name, $arguments) | |
{ | |
$cache = $this->cacheInstance(); | |
$cacheKey = $this->prefix.$this->key; | |
if ($cache->has($cacheKey)) | |
{ | |
return $cache->get($cacheKey); | |
} | |
else | |
{ | |
return $this->cacheNewResults($name, $arguments, $cache, $cacheKey); | |
} | |
} | |
/** | |
* Store in cache the new | |
* results | |
* | |
* @param $name | |
* @param $arguments | |
* @param $cache | |
* @param $cacheKey | |
* @return mixed | |
*/ | |
public function cacheNewResults($name, $arguments, $cache, $cacheKey) | |
{ | |
$arguments = (is_array($arguments)) ? $arguments : [$arguments]; | |
// recall back the same function called for get the new results | |
$dataToCache = call_user_func_array([$this->classInstance, $name], $arguments); | |
if ($this->lifeTime === false) | |
{ | |
$cache->forever($cacheKey, $dataToCache); | |
return $dataToCache; | |
} | |
// cache the results | |
$cache->put($cacheKey, $dataToCache, $this->lifeTime); | |
return $dataToCache; | |
} | |
} |
This file contains 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 characters
<?php namespace Core\Cache; | |
/** | |
* Class CashMe | |
* | |
* @package Core\Cache | |
*/ | |
abstract class CashMe { | |
/** | |
* @var string | |
*/ | |
protected $prefix; | |
/** | |
* @var int | |
*/ | |
protected $lifeTime = 60; // min | |
public abstract function classInstance(); | |
/** | |
* Transfer the data of the call in another | |
* class that will be responsable to | |
* return the data | |
* | |
* @return CacheMeRemainder | |
*/ | |
public function cache($key = "") | |
{ | |
return new CacheMeRemainder( | |
$this->classInstance(), | |
$this->prefix, | |
$key, | |
$this->lifeTime | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment