Apachenote.class.php 3.6 KB

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