Apc.class.php 2.6 KB

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