CacheWincache.class.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. defined('THINK_PATH') or exit();
  12. /**
  13. * Wincache缓存驱动
  14. * @category Extend
  15. * @package Extend
  16. * @subpackage Driver.Cache
  17. * @author liu21st <liu21st@gmail.com>
  18. */
  19. class CacheWincache extends Cache {
  20. /**
  21. * 架构函数
  22. * @param array $options 缓存参数
  23. * @access public
  24. */
  25. public function __construct($options=array()) {
  26. if ( !function_exists('wincache_ucache_info') ) {
  27. throw_exception(L('_NOT_SUPPERT_').':WinCache');
  28. }
  29. $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
  30. $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
  31. $this->options['length'] = isset($options['length'])? $options['length'] : 0;
  32. }
  33. /**
  34. * 读取缓存
  35. * @access public
  36. * @param string $name 缓存变量名
  37. * @return mixed
  38. */
  39. public function get($name) {
  40. N('cache_read',1);
  41. $name = $this->options['prefix'].$name;
  42. return wincache_ucache_exists($name)? wincache_ucache_get($name) : false;
  43. }
  44. /**
  45. * 写入缓存
  46. * @access public
  47. * @param string $name 缓存变量名
  48. * @param mixed $value 存储数据
  49. * @param integer $expire 有效时间(秒)
  50. * @return boolen
  51. */
  52. public function set($name, $value,$expire=null) {
  53. N('cache_write',1);
  54. if(is_null($expire)) {
  55. $expire = $this->options['expire'];
  56. }
  57. $name = $this->options['prefix'].$name;
  58. if(wincache_ucache_set($name, $value, $expire)) {
  59. if($this->options['length']>0) {
  60. // 记录缓存队列
  61. $this->queue($name);
  62. }
  63. return true;
  64. }
  65. return false;
  66. }
  67. /**
  68. * 删除缓存
  69. * @access public
  70. * @param string $name 缓存变量名
  71. * @return boolen
  72. */
  73. public function rm($name) {
  74. return wincache_ucache_delete($this->options['prefix'].$name);
  75. }
  76. }