33 $this->
publish([
'type',
'refresh']);
37 if($type == self::TYPE_CHAIN){
39 foreach($params as $sub_type => $sub_params)
41 $params = [$providers];
43 $reflect = new \ReflectionClass(
'\\Doctrine\\Common\\Cache\\' . ucfirst($type) .
'Cache');
44 return $reflect->newInstanceArgs($params ?: []);
52 public function save($id,$data,$ttl = null){
54 $log->debug(__CLASS__ .
"::save('$id',...,$ttl)",__FILE__,__LINE__);
55 if($data ===
false) $log->warning(
"Saving false in cache for id '$id' (means 'no cache found')",__FILE__,__LINE__);
56 if($this->_refresh) $this->_refreshed[] = $id;
58 if($this->ttlVariation) $ttl *= 1 + rand(-1000,1000) * $this->ttlVariation / 100000;
59 return $this->provider->save($id,$data,$ttl);
68 public function fetch($id,$ttl = null,$callback = null){
69 $time = $this->statPrefix ? microtime(
true) :
null;
71 $data = $this->_refresh && !in_array($id,$this->_refreshed) ? false : $this->provider->fetch($id);
72 if($busy = $this->busyPlaceholder && ($data === $this->busyPlaceholder)) sleep(1);
76 $log->debug(__CLASS__ .
"::fetch('$id',$ttl,...)",__FILE__,__LINE__,[
'cache' => $id,
'found' => $data !==
false]);
77 if($data !==
false) $time =
false;
79 if($this->busyPlaceholder) $this->
save($id,$this->busyPlaceholder,$this->_busyTtl ?: ini_get(
'max_execution_time'));
80 $this->
save($id,$data = call_user_func($callback,$id),$ttl);
82 if($this->statPrefix) $this->
component(
'stats')->inc(
83 $this->statPrefix .
':' . $id .
':' . ($time ?
'miss' :
'hit'),
84 $time && $callback ? microtime(
true) - $time : null
86 if($this->_fred->debug){
87 if(array_key_exists($id,$this->_duploCount))
88 $log->info(
'Duplicate fetch' . ($this->_duploCount[$id]++ ?
' (' . $this->_duploCount[$id] .
')' :
''),__FILE__,__LINE__,compact(
'id',
'ttl'));
89 else $this->_duploCount[$id] = 0;
95 if(!$this->_provider) $this->_provider = $this->
createProvider($this->_type,$this->_params);
97 if(!$this->_altProvider) $this->_altProvider = $this->
createProvider($this->_altType,$this->_altParams);
102 $this->_refresh = $value;
103 $this->_refreshed = [];
109 public function __call($func_name,$params){
110 if($this->_useAlt = substr($func_name,0,1) ==
'_'){
111 $result = call_user_func_array([$this,substr($func_name,1)],$params);
112 $this->_useAlt =
false;
115 if(in_array($func_name,[
'save',
'contains',
'fetch',
'delete']))
116 $this->
component(
'log')->debug(__CLASS__ .
"::$func_name('$params[0]')",__FILE__,__LINE__);
117 return call_user_func_array([$this->provider,$func_name],$params);
121 return $this->provider->fetch($id);
fetch($id, $ttl=null, $callback=null)
Fetch with callback function in case cache fails.
Wrapper for the Doctrine cache.
$ttlVariation
Variation (plus and minus; percentage) of input value.
$statPrefix
Set to something to stat all cache requests (hit/miss).
$_busyTtl
Lifetime for cache slam prevention (default = current timeout).
$defaultTtl
Default lifetime.
$busyPlaceholder
Placeholder for preventing cache slam.
save($id, $data, $ttl=null)
Save data to the cache.
$_params
Parameters (arguments) for this type of cache.
publish($property, $visibility=self::READABLE)
Publish a property (or hide it again).
__call($func_name, $params)
See vendor/doctrine/cache/lib/Doctrine/Common/Cache/Cache.php for the available functions (save...
createProvider($type, $params=null)
component($name)
Get a component (local or default).