Eaccelerator.class.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  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. * Eaccelerator缓存驱动
  15. */
  16. class Eaccelerator extends Cache
  17. {
  18. /**
  19. * 架构函数
  20. * @param array $options 缓存参数
  21. * @access public
  22. */
  23. public function __construct($options = array())
  24. {
  25. $this->options['expire'] = isset($options['expire']) ? $options['expire'] : C('DATA_CACHE_TIME');
  26. $this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('DATA_CACHE_PREFIX');
  27. $this->options['length'] = isset($options['length']) ? $options['length'] : 0;
  28. }
  29. /**
  30. * 读取缓存
  31. * @access public
  32. * @param string $name 缓存变量名
  33. * @return mixed
  34. */
  35. public function get($name)
  36. {
  37. N('cache_read', 1);
  38. return eaccelerator_get($this->options['prefix'] . $name);
  39. }
  40. /**
  41. * 写入缓存
  42. * @access public
  43. * @param string $name 缓存变量名
  44. * @param mixed $value 存储数据
  45. * @param integer $expire 有效时间(秒)
  46. * @return boolean
  47. */
  48. public function set($name, $value, $expire = null)
  49. {
  50. N('cache_write', 1);
  51. if (is_null($expire)) {
  52. $expire = $this->options['expire'];
  53. }
  54. $name = $this->options['prefix'] . $name;
  55. eaccelerator_lock($name);
  56. if (eaccelerator_put($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. {
  73. return eaccelerator_rm($this->options['prefix'] . $name);
  74. }
  75. }