CacheEaccelerator.class.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  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. * Eaccelerator缓存驱动
  14. * @category Extend
  15. * @package Extend
  16. * @subpackage Driver.Cache
  17. * @author liu21st <liu21st@gmail.com>
  18. */
  19. class CacheEaccelerator extends Cache {
  20. /**
  21. * 架构函数
  22. * @param array $options 缓存参数
  23. * @access public
  24. */
  25. public function __construct($options=array()) {
  26. $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
  27. $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
  28. $this->options['length'] = isset($options['length'])? $options['length'] : 0;
  29. }
  30. /**
  31. * 读取缓存
  32. * @access public
  33. * @param string $name 缓存变量名
  34. * @return mixed
  35. */
  36. public function get($name) {
  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 boolen
  47. */
  48. public function set($name, $value, $expire = null) {
  49. N('cache_write',1);
  50. if(is_null($expire)) {
  51. $expire = $this->options['expire'];
  52. }
  53. $name = $this->options['prefix'].$name;
  54. eaccelerator_lock($name);
  55. if(eaccelerator_put($name, $value, $expire)) {
  56. if($this->options['length']>0) {
  57. // 记录缓存队列
  58. $this->queue($name);
  59. }
  60. return true;
  61. }
  62. return false;
  63. }
  64. /**
  65. * 删除缓存
  66. * @access public
  67. * @param string $name 缓存变量名
  68. * @return boolen
  69. */
  70. public function rm($name) {
  71. return eaccelerator_rm($this->options['prefix'].$name);
  72. }
  73. }