Apc.class.php 2.5 KB

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