Wincache.class.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 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. namespace Think\Cache\Driver;
  12. use Think\Cache;
  13. defined('THINK_PATH') or exit();
  14. /**
  15. * Wincache缓存驱动
  16. */
  17. class Wincache extends Cache {
  18. /**
  19. * 架构函数
  20. * @param array $options 缓存参数
  21. * @access public
  22. */
  23. public function __construct($options=array()) {
  24. if ( !function_exists('wincache_ucache_info') ) {
  25. E(L('_NOT_SUPPORT_').':WinCache');
  26. }
  27. $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
  28. $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
  29. $this->options['length'] = isset($options['length'])? $options['length'] : 0;
  30. }
  31. /**
  32. * 读取缓存
  33. * @access public
  34. * @param string $name 缓存变量名
  35. * @return mixed
  36. */
  37. public function get($name) {
  38. N('cache_read',1);
  39. $name = $this->options['prefix'].$name;
  40. return wincache_ucache_exists($name)? wincache_ucache_get($name) : false;
  41. }
  42. /**
  43. * 写入缓存
  44. * @access public
  45. * @param string $name 缓存变量名
  46. * @param mixed $value 存储数据
  47. * @param integer $expire 有效时间(秒)
  48. * @return boolean
  49. */
  50. public function set($name, $value,$expire=null) {
  51. N('cache_write',1);
  52. if(is_null($expire)) {
  53. $expire = $this->options['expire'];
  54. }
  55. $name = $this->options['prefix'].$name;
  56. if(wincache_ucache_set($name, $value, $expire)) {
  57. if($this->options['length']>0) {
  58. // 记录缓存队列
  59. $this->queue($name);
  60. }
  61. return true;
  62. }
  63. return false;
  64. }
  65. /**
  66. * 删除缓存
  67. * @access public
  68. * @param string $name 缓存变量名
  69. * @return boolean
  70. */
  71. public function rm($name) {
  72. return wincache_ucache_delete($this->options['prefix'].$name);
  73. }
  74. /**
  75. * 清除缓存
  76. * @access public
  77. * @return boolean
  78. */
  79. public function clear() {
  80. return wincache_ucache_clear();
  81. }
  82. }