FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Cache.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class Cache extends \Rsi\Fred\Stream\Handler{
6 
7  public $ttl = 3600;
8 
9  protected function key($handlers,$filename,$params){
10  return md5(implode('-',$handlers) . $filename . print_r($params,true));
11  }
12 
13  protected function cache($handlers,$filename,$params,&$key = null){
14  return $this->_stream->cache->fetch($key = $this->key($handlers,$filename,$params));
15  }
16 
17  public function time($handlers,$filename,$params = null){
18  $cache = $this->cache($handlers,$filename,$params);
19  return $cache ? $cache['time'] : parent::time($handlers,$filename,$params);
20  }
21 
22  public function read($handlers,$filename,$params = null){
23  if($cache = $this->cache($handlers,$filename,$params,$key)) return $cache['data'];
24  $data = parent::read($handlers,$filename,$params);
25  $this->_stream->cache->save($key,['data' => $data,'time' => parent::time($handlers,$filename,$params)],$this->ttl);
26  return $data;
27  }
28 
29  public function save($handlers,$filename,$params,$data){
30  if(parent::save($handlers,$filename,$params,$data))
31  $this->_stream->cache->delete($this->key($handlers,$filename,$params));
32  }
33 
34  public function delete($handlers,$filename,$params = null){
35  if(parent::delete($handlers,$filename,$params))
36  $this->_stream->cache->delete($this->key($handlers,$filename,$params));
37  }
38 
39 }
cache($handlers, $filename, $params, &$key=null)
Definition: Cache.php:13
save($handlers, $filename, $params, $data)
Definition: Cache.php:29
read($handlers, $filename, $params=null)
Definition: Cache.php:22
time($handlers, $filename, $params=null)
Definition: Cache.php:17
key($handlers, $filename, $params)
Definition: Cache.php:9