Apachenote.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. * Apachenote缓存驱动
  15. */
  16. class Apachenote extends Cache
  17. {
  18. /**
  19. * 架构函数
  20. * @param array $options 缓存参数
  21. * @access public
  22. */
  23. public function __construct($options = array())
  24. {
  25. if (!empty($options)) {
  26. $this->options = $options;
  27. }
  28. if (empty($options)) {
  29. $options = array(
  30. 'host' => '127.0.0.1',
  31. 'port' => 1042,
  32. 'timeout' => 10,
  33. );
  34. }
  35. $this->options = $options;
  36. $this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('DATA_CACHE_PREFIX');
  37. $this->options['length'] = isset($options['length']) ? $options['length'] : 0;
  38. $this->handler = null;
  39. $this->open();
  40. }
  41. /**
  42. * 读取缓存
  43. * @access public
  44. * @param string $name 缓存变量名
  45. * @return mixed
  46. */
  47. public function get($name)
  48. {
  49. $this->open();
  50. $name = $this->options['prefix'] . $name;
  51. $s = 'F' . pack('N', strlen($name)) . $name;
  52. fwrite($this->handler, $s);
  53. for ($data = '';!feof($this->handler);) {
  54. $data .= fread($this->handler, 4096);
  55. }
  56. N('cache_read', 1);
  57. $this->close();
  58. return '' === $data ? '' : unserialize($data);
  59. }
  60. /**
  61. * 写入缓存
  62. * @access public
  63. * @param string $name 缓存变量名
  64. * @param mixed $value 存储数据
  65. * @return boolean
  66. */
  67. public function set($name, $value)
  68. {
  69. N('cache_write', 1);
  70. $this->open();
  71. $value = serialize($value);
  72. $name = $this->options['prefix'] . $name;
  73. $s = 'S' . pack('NN', strlen($name), strlen($value)) . $name . $value;
  74. fwrite($this->handler, $s);
  75. $ret = fgets($this->handler);
  76. $this->close();
  77. if ("OK\n" === $ret) {
  78. if ($this->options['length'] > 0) {
  79. // 记录缓存队列
  80. $this->queue($name);
  81. }
  82. return true;
  83. }
  84. return false;
  85. }
  86. /**
  87. * 删除缓存
  88. * @access public
  89. * @param string $name 缓存变量名
  90. * @return boolean
  91. */
  92. public function rm($name)
  93. {
  94. $this->open();
  95. $name = $this->options['prefix'] . $name;
  96. $s = 'D' . pack('N', strlen($name)) . $name;
  97. fwrite($this->handler, $s);
  98. $ret = fgets($this->handler);
  99. $this->close();
  100. return "OK\n" === $ret;
  101. }
  102. /**
  103. * 关闭缓存
  104. * @access private
  105. */
  106. private function close()
  107. {
  108. fclose($this->handler);
  109. $this->handler = false;
  110. }
  111. /**
  112. * 打开缓存
  113. * @access private
  114. */
  115. private function open()
  116. {
  117. if (!is_resource($this->handler)) {
  118. $this->handler = fsockopen($this->options['host'], $this->options['port'], $_, $_, $this->options['timeout']);
  119. }
  120. }
  121. }