Wincache.class.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /**
  14. * Wincache缓存驱动
  15. */
  16. class Wincache extends Cache
  17. {
  18. /**
  19. * 架构函数
  20. * @param array $options 缓存参数
  21. * @access public
  22. */
  23. public function __construct($options = array())
  24. {
  25. if (!function_exists('wincache_ucache_info')) {
  26. E(L('_NOT_SUPPORT_') . ':WinCache');
  27. }
  28. $this->options['expire'] = isset($options['expire']) ? $options['expire'] : C('DATA_CACHE_TIME');
  29. $this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('DATA_CACHE_PREFIX');
  30. $this->options['length'] = isset($options['length']) ? $options['length'] : 0;
  31. }
  32. /**
  33. * 读取缓存
  34. * @access public
  35. * @param string $name 缓存变量名
  36. * @return mixed
  37. */
  38. public function get($name)
  39. {
  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 boolean
  51. */
  52. public function set($name, $value, $expire = null)
  53. {
  54. N('cache_write', 1);
  55. if (is_null($expire)) {
  56. $expire = $this->options['expire'];
  57. }
  58. $name = $this->options['prefix'] . $name;
  59. if (wincache_ucache_set($name, $value, $expire)) {
  60. if ($this->options['length'] > 0) {
  61. // 记录缓存队列
  62. $this->queue($name);
  63. }
  64. return true;
  65. }
  66. return false;
  67. }
  68. /**
  69. * 删除缓存
  70. * @access public
  71. * @param string $name 缓存变量名
  72. * @return boolean
  73. */
  74. public function rm($name)
  75. {
  76. return wincache_ucache_delete($this->options['prefix'] . $name);
  77. }
  78. /**
  79. * 清除缓存
  80. * @access public
  81. * @return boolean
  82. */
  83. public function clear()
  84. {
  85. return wincache_ucache_clear();
  86. }
  87. }