CacheXcache.class.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  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. * Xcache缓存驱动
  14. * @category Extend
  15. * @package Extend
  16. * @subpackage Driver.Cache
  17. * @author liu21st <liu21st@gmail.com>
  18. */
  19. class CacheXcache extends Cache {
  20. /**
  21. * 架构函数
  22. * @param array $options 缓存参数
  23. * @access public
  24. */
  25. public function __construct($options=array()) {
  26. if ( !function_exists('xcache_info') ) {
  27. throw_exception(L('_NOT_SUPPERT_').':Xcache');
  28. }
  29. $this->options['expire'] = isset($options['expire'])?$options['expire']:C('DATA_CACHE_TIME');
  30. $this->options['prefix'] = isset($options['prefix'])?$options['prefix']:C('DATA_CACHE_PREFIX');
  31. $this->options['length'] = isset($options['length'])?$options['length']:0;
  32. }
  33. /**
  34. * 读取缓存
  35. * @access public
  36. * @param string $name 缓存变量名
  37. * @return mixed
  38. */
  39. public function get($name) {
  40. N('cache_read',1);
  41. $name = $this->options['prefix'].$name;
  42. if (xcache_isset($name)) {
  43. return xcache_get($name);
  44. }
  45. return false;
  46. }
  47. /**
  48. * 写入缓存
  49. * @access public
  50. * @param string $name 缓存变量名
  51. * @param mixed $value 存储数据
  52. * @param integer $expire 有效时间(秒)
  53. * @return boolen
  54. */
  55. public function set($name, $value,$expire=null) {
  56. N('cache_write',1);
  57. if(is_null($expire)) {
  58. $expire = $this->options['expire'] ;
  59. }
  60. $name = $this->options['prefix'].$name;
  61. if(xcache_set($name, $value, $expire)) {
  62. if($this->options['length']>0) {
  63. // 记录缓存队列
  64. $this->queue($name);
  65. }
  66. return true;
  67. }
  68. return false;
  69. }
  70. /**
  71. * 删除缓存
  72. * @access public
  73. * @param string $name 缓存变量名
  74. * @return boolen
  75. */
  76. public function rm($name) {
  77. return xcache_unset($this->options['prefix'].$name);
  78. }
  79. }