You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.4 KiB

3 years ago
  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. defined('THINK_PATH') or exit();
  14. /**
  15. * Eaccelerator缓存驱动
  16. */
  17. class Eaccelerator extends Cache {
  18. /**
  19. * 架构函数
  20. * @param array $options 缓存参数
  21. * @access public
  22. */
  23. public function __construct($options=array()) {
  24. $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
  25. $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
  26. $this->options['length'] = isset($options['length'])? $options['length'] : 0;
  27. }
  28. /**
  29. * 读取缓存
  30. * @access public
  31. * @param string $name 缓存变量名
  32. * @return mixed
  33. */
  34. public function get($name) {
  35. N('cache_read',1);
  36. return eaccelerator_get($this->options['prefix'].$name);
  37. }
  38. /**
  39. * 写入缓存
  40. * @access public
  41. * @param string $name 缓存变量名
  42. * @param mixed $value 存储数据
  43. * @param integer $expire 有效时间(秒)
  44. * @return boolean
  45. */
  46. public function set($name, $value, $expire = null) {
  47. N('cache_write',1);
  48. if(is_null($expire)) {
  49. $expire = $this->options['expire'];
  50. }
  51. $name = $this->options['prefix'].$name;
  52. eaccelerator_lock($name);
  53. if(eaccelerator_put($name, $value, $expire)) {
  54. if($this->options['length']>0) {
  55. // 记录缓存队列
  56. $this->queue($name);
  57. }
  58. return true;
  59. }
  60. return false;
  61. }
  62. /**
  63. * 删除缓存
  64. * @access public
  65. * @param string $name 缓存变量名
  66. * @return boolean
  67. */
  68. public function rm($name) {
  69. return eaccelerator_rm($this->options['prefix'].$name);
  70. }
  71. }