PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/ecmall2/includes/libraries/cache.lib.php

https://gitlab.com/BGCX261/zhou3liu-svn-to-git
PHP | 223 lines | 129 code | 18 blank | 76 comment | 12 complexity | d4421280b130619bb7e19ffe49155069 MD5 | raw file
  1. <?php
  2. /**
  3. * 基础缓存类接口
  4. *
  5. * @author Garbin
  6. * @usage none
  7. */
  8. class CacheServer extends Object
  9. {
  10. var $_options = null;
  11. function __construct($options = null)
  12. {
  13. $this->CacheServer($options);
  14. }
  15. function CacheServer($options = null)
  16. {
  17. $this->_options = $options;
  18. }
  19. /**
  20. * 获取缓存的数据
  21. *
  22. * @author Garbin
  23. * @param string $key
  24. * @return mixed
  25. */
  26. function &get($key){}
  27. /**
  28. * 设置缓存
  29. *
  30. * @author Garbin
  31. * @param string $key
  32. * @param mixed $value
  33. * @param int $ttl
  34. * @return bool
  35. */
  36. function set($key, $value, $ttl = 0){}
  37. /**
  38. * 清空缓存
  39. *
  40. * @author Garbin
  41. * @return bool
  42. */
  43. function clear(){}
  44. /**
  45. * 删除一个缓存
  46. *
  47. * @author Garbin
  48. * @param string $key
  49. * @return bool
  50. */
  51. function delete($key){}
  52. }
  53. /**
  54. * 普通PHP文件缓存
  55. *
  56. * @author Garbin
  57. * @usage none
  58. */
  59. class PhpCacheServer extends CacheServer
  60. {
  61. /* 缓存目录 */
  62. var $_cache_dir = './';
  63. function set($key, $value, $ttl = 0)
  64. {
  65. if (!$key)
  66. {
  67. return false;
  68. }
  69. $cache_file = $this->_get_cache_path($key);
  70. $cache_data = "<?php\r\n/**\r\n * @Created By ECMall PhpCacheServer\r\n * @Time:" . date('Y-m-d H:i:s') . "\r\n */";
  71. $cache_data .= $this->_get_expire_condition(intval($ttl));
  72. $cache_data .= "\r\nreturn " . var_export($value, true) . ";\r\n";
  73. $cache_data .= "\r\n?>";
  74. return file_put_contents($cache_file, $cache_data, LOCK_EX);
  75. }
  76. function &get($key)
  77. {
  78. $cache_file = $this->_get_cache_path($key);
  79. if (!is_file($cache_file))
  80. {
  81. return false;
  82. }
  83. $data = include($cache_file);
  84. return $data;
  85. }
  86. function clear()
  87. {
  88. $dir = dir($this->_cache_dir);
  89. while (false !== ($item = $dir->read()))
  90. {
  91. if ($item == '.' || $item == '..' || substr($item, 0, 1) == '.')
  92. {
  93. continue;
  94. }
  95. $item_path = $this->_cache_dir . '/' . $item;
  96. if (is_dir($item_path))
  97. {
  98. ecm_rmdir($item_path);
  99. }
  100. else
  101. {
  102. @unlink($item_path);
  103. }
  104. }
  105. return true;
  106. }
  107. function delete($key)
  108. {
  109. $cache_file = $this->_get_cache_path($key);
  110. return @unlink($cache_file);
  111. }
  112. function set_cache_dir($path)
  113. {
  114. $this->_cache_dir = $path;
  115. }
  116. function _get_expire_condition($ttl = 0)
  117. {
  118. if (!$ttl)
  119. {
  120. return '';
  121. }
  122. return "\r\n\r\n" . 'if(filemtime(__FILE__) + ' . $ttl . ' < time())return false;' . "\r\n";
  123. }
  124. function _get_cache_path($key)
  125. {
  126. return $this->_cache_dir . '/' . $this->_get_file_name($key);
  127. }
  128. function _get_file_name($key)
  129. {
  130. return md5($key) . '.cache.php';
  131. }
  132. }
  133. /**
  134. * Memcached
  135. *
  136. * @author Garbin
  137. * @usage none
  138. */
  139. class MemcacheServer extends CacheServer
  140. {
  141. var $_memcache = null;
  142. function __construct($options)
  143. {
  144. $this->MemcacheServer($options);
  145. }
  146. function MemcacheServer($options)
  147. {
  148. parent::__construct($options);
  149. /* 连接到缓存服务器 */
  150. $this->connect($this->_options);
  151. }
  152. /**
  153. * 连接到缓存服务器
  154. *
  155. * @author Garbin
  156. * @param array $options
  157. * @return bool
  158. */
  159. function connect($options)
  160. {
  161. if (empty($options))
  162. {
  163. return false;
  164. }
  165. $this->_memcache = new Memcache;
  166. return $this->_memcache->connect($options['host'], $options['port']);
  167. }
  168. /**
  169. * 写入缓存
  170. *
  171. * @author Garbin
  172. * @param none
  173. * @return void
  174. */
  175. function set($key, $value, $ttl = null)
  176. {
  177. return $this->_memcache->set($key, $value, $ttl);
  178. }
  179. /**
  180. * 获取缓存
  181. *
  182. * @author Garbin
  183. * @param string $key
  184. * @return mixed
  185. */
  186. function &get($key)
  187. {
  188. return $this->_memcache->get($key);
  189. }
  190. /**
  191. * 清空缓存
  192. *
  193. * @author Garbin
  194. * @return bool
  195. */
  196. function clear()
  197. {
  198. return $this->_memcache->flush();
  199. }
  200. function delete($key)
  201. {
  202. return $this->_memcache->delete($key);
  203. }
  204. }
  205. ?>