CacheApachenote.class.php 3.7 KB

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