KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
file_cache.php
Ir a la documentación de este archivo.
1 <?php
29 class FileCache extends Cache
30 {
31 
39  protected function _getFilename($id, $group)
40  {
41  return 'cache_' . md5($id) . '.' . md5($group);
42  }
43 
51  public function get($id, $group='default')
52  {
53  $this->_id = $id;
54  $this->_group = $group;
55 
56  $filename = APP_PATH . 'temp/cache/' . $this->_getFilename($id, $group);
57  if (file_exists($filename)) {
58  $fh = fopen($filename, 'r');
59 
60  $lifetime = trim(fgets($fh));
61  if ($lifetime == 'undefined' || $lifetime >= time()) {
62  $data = stream_get_contents($fh);
63  } else {
64  $data = null;
65  }
66 
67  fclose($fh);
68  return $data;
69  }
70  return null;
71  }
72 
82  public function save($value, $lifetime=null, $id=false, $group='default')
83  {
84  if (!$id) {
85  $id = $this->_id;
86  $group = $this->_group;
87  }
88 
89  if ($lifetime) {
90  $lifetime = strtotime($lifetime);
91  } else {
92  $lifetime = 'undefined';
93  }
94 
95  return file_put_contents(APP_PATH . 'temp/cache/' . $this->_getFilename($id, $group), "$lifetime\n$value");
96  }
97 
104  public function clean($group=false)
105  {
106  $pattern = $group ? APP_PATH . 'temp/cache/' . '*.' . md5($group) : APP_PATH . 'temp/cache/*';
107  foreach (glob($pattern) as $filename) {
108  if (!unlink($filename)) {
109  return false;
110  }
111  }
112  return true;
113  }
114 
122  public function remove($id, $group='default')
123  {
124  return unlink(APP_PATH . 'temp/cache/' . $this->_getFilename($id, $group));
125  }
126 
127 }