CacheApc.class.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. * Apc缓存驱动
  14. * @category Extend
  15. * @package Extend
  16. * @subpackage Driver.Cache
  17. * @author liu21st <liu21st@gmail.com>
  18. */
  19. class CacheApc extends Cache {
  20. /**
  21. * 架构函数
  22. * @param array $options 缓存参数
  23. * @access public
  24. */
  25. public function __construct($options=array()) {
  26. if(!function_exists('apc_cache_info')) {
  27. throw_exception(L('_NOT_SUPPERT_').':Apc');
  28. }
  29. $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
  30. $this->options['length'] = isset($options['length'])? $options['length'] : 0;
  31. $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
  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. return apc_fetch($this->options['prefix'].$name);
  42. }
  43. /**
  44. * 写入缓存
  45. * @access public
  46. * @param string $name 缓存变量名
  47. * @param mixed $value 存储数据
  48. * @param integer $expire 有效时间(秒)
  49. * @return boolen
  50. */
  51. public function set($name, $value, $expire = null) {
  52. N('cache_write',1);
  53. if(is_null($expire)) {
  54. $expire = $this->options['expire'];
  55. }
  56. $name = $this->options['prefix'].$name;
  57. if($result = apc_store($name, $value, $expire)) {
  58. if($this->options['length']>0) {
  59. // 记录缓存队列
  60. $this->queue($name);
  61. }
  62. }
  63. return $result;
  64. }
  65. /**
  66. * 删除缓存
  67. * @access public
  68. * @param string $name 缓存变量名
  69. * @return boolen
  70. */
  71. public function rm($name) {
  72. return apc_delete($this->options['prefix'].$name);
  73. }
  74. /**
  75. * 清除缓存
  76. * @access public
  77. * @return boolen
  78. */
  79. public function clear() {
  80. return apc_clear_cache();
  81. }
  82. }