PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/berry/lib/berry/cache.php

http://goodgirl.googlecode.com/
PHP | 177 lines | 113 code | 47 blank | 17 comment | 29 complexity | ba68321caed000deb1a5f0eee77f89ef MD5 | raw file
  1. <?php /* `,
  2. ,\, #
  3. B E R R Y |/ ?
  4. <http://goodgirl.ru/berry> | ~ )\
  5. <http://goodgirl.ru/berry/license> /__/\ \____
  6. / \_/ \
  7. ???? zloy ? ???????? <http://lexa.cutenews.ru> / <_ ____,_-/\ __
  8. ---------------------------------------------------------/___/_____ \--'\|/----
  9. \/|*/
  10. class Cache {
  11. static $file;
  12. protected static $tags;
  13. protected static $scope = array();
  14. ////////////////////////////////////////////////////////////////////////////////
  15. static function get($key, $array = array(), $_array = array()){
  16. self::$scope[$key] = self::$file = file::path('tmp/').$key;
  17. if (!isset(self::$tags)){
  18. if (!is_file($file = file::path('tmp/').'cache.php'))
  19. arr::export($file, array());
  20. self::$tags = include $file;
  21. }
  22. if (is_object($array))
  23. return new self($key, $array, $_array);
  24. $is_array = (substr($key, -4) == '.php');
  25. if (self::expired($key, $array))
  26. return ($is_array ? array() : null);
  27. unset(self::$scope[$key]);
  28. return ($is_array ? include self::$file : file_get_contents(self::$file));
  29. }
  30. ////////////////////////////////////////////////////////////////////////////////
  31. static function get_path($key, $array = array(), $_array = array()){
  32. self::$scope[$key] = self::$file = file::path('tmp/').$key;
  33. if (!isset(self::$tags)){
  34. if (!is_file($file = file::path('tmp/').'cache.php'))
  35. arr::export($file, array());
  36. self::$tags = include $file;
  37. }
  38. if (self::expired($key, $array))
  39. return;
  40. unset(self::$scope[$key]);
  41. return self::$file;
  42. }
  43. ////////////////////////////////////////////////////////////////////////////////
  44. static function set($value, $tags = array()){ $file = end(self::$scope);
  45. $name = str_replace(file::path('tmp/'), '', $file);
  46. $array = (isset(self::$tags[$name]) ? self::$tags[$name] : array());
  47. self::$tags[$name] = array_merge($array, $tags);
  48. arr::export(file::path('tmp/').'cache.php', self::$tags);
  49. file::mkdir(dirname($dir = $file));
  50. b::call((is_array($value) ? 'arr::export' : 'file_put_contents'), $file, $value);
  51. array_pop(self::$scope);
  52. return $dir;
  53. }
  54. ////////////////////////////////////////////////////////////////////////////////
  55. static function remove($key){
  56. if (!is_array($key)){
  57. if ($file = self::exists($key))
  58. return unlink($file);
  59. return;
  60. }
  61. $result = array();
  62. foreach (self::$tags as $k => $v)
  63. if (array_intersect($v, $key) and is_file($file = file::path('tmp/').$k)){
  64. unset(self::$tags[$k]);
  65. $result[$k] = unlink($file);
  66. }
  67. arr::export(file::path('tmp/').'cache.php', self::$tags);
  68. return $result;
  69. }
  70. ////////////////////////////////////////////////////////////////////////////////
  71. static function exists($key){
  72. if (is_file($file = file::path('tmp/').$key))
  73. return $file;
  74. }
  75. ////////////////////////////////////////////////////////////////////////////////
  76. static function expired($key, $array){
  77. if (!$file = self::exists($key))
  78. return true;
  79. $mtime = filemtime($file);
  80. $result = array();
  81. foreach ($array as $k => $tmp){
  82. if (!$tmp)
  83. continue;
  84. foreach ((is_array($tmp) ? $tmp : array($tmp)) as $v){
  85. if ($k == 'file' and file_exists($v))
  86. $result[] = (filemtime($v) > $mtime);
  87. if ($k == 'db'){ if (is_object($v)){ $class = get_class($v);
  88. $vars = get_class_vars($class);
  89. $v = ($vars['table'] ? $vars['table'] : inflector::tableize($class));
  90. }
  91. if ($pos = strpos($v, '.')){
  92. $tmp = substr($v, 0, $pos);
  93. $v = substr($v, ($pos + 1));
  94. } else { $tmp = SQL::SKIP;
  95. }
  96. $query = sql::query('show table status { from ?_ } like "?_"', $tmp, $v)->fetch_row();
  97. $result[] = (strtotime($query['Update_time']) > $mtime);
  98. }
  99. if ($k == 'url' and ($headers = get_headers($v, true)))
  100. $result[] = (strtotime($headers['Last-Modified']) > $mtime);
  101. if ($k == 'time')
  102. $result[] = ((date::time($v) - time()) < (time() - $mtime));
  103. }
  104. }
  105. return in_array(true, $result);
  106. }
  107. ////////////////////////////////////////////////////////////////////////////////
  108. function __construct($key, $object, $array){
  109. $this->key = $key;
  110. $this->object = $object;
  111. $this->array = (array)$array;
  112. }
  113. ////////////////////////////////////////////////////////////////////////////////
  114. function __call($method, $params){
  115. if (!self::expired($this->key, $this->array))
  116. return include self::$file;
  117. $tags = array();
  118. if (isset($this->array['tag'])){
  119. $tags = (array)$this->array['tag'];
  120. } else {
  121. foreach ($this->array as $k => $v)
  122. if (is_int($k))
  123. $tags[] = $v;
  124. }
  125. $data = call_user_func_array(array($this->object, $method), $params);
  126. self::set($data, $tags);
  127. return $data;
  128. }
  129. ////////////////////////////////////////////////////////////////////////////////
  130. }