Cache.class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 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. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * 缓存管理类
  15. +------------------------------------------------------------------------------
  16. * @category Think
  17. * @package Think
  18. * @subpackage Util
  19. * @author liu21st <liu21st@gmail.com>
  20. * @version $Id$
  21. +------------------------------------------------------------------------------
  22. */
  23. class Cache extends Think
  24. {//类定义开始
  25. /**
  26. +----------------------------------------------------------
  27. * 是否连接
  28. +----------------------------------------------------------
  29. * @var string
  30. * @access protected
  31. +----------------------------------------------------------
  32. */
  33. protected $connected ;
  34. /**
  35. +----------------------------------------------------------
  36. * 操作句柄
  37. +----------------------------------------------------------
  38. * @var string
  39. * @access protected
  40. +----------------------------------------------------------
  41. */
  42. protected $handler ;
  43. /**
  44. +----------------------------------------------------------
  45. * 缓存存储前缀
  46. +----------------------------------------------------------
  47. * @var string
  48. * @access protected
  49. +----------------------------------------------------------
  50. */
  51. protected $prefix='~@';
  52. /**
  53. +----------------------------------------------------------
  54. * 缓存连接参数
  55. +----------------------------------------------------------
  56. * @var integer
  57. * @access protected
  58. +----------------------------------------------------------
  59. */
  60. protected $options = array();
  61. /**
  62. +----------------------------------------------------------
  63. * 缓存类型
  64. +----------------------------------------------------------
  65. * @var integer
  66. * @access protected
  67. +----------------------------------------------------------
  68. */
  69. protected $type ;
  70. /**
  71. +----------------------------------------------------------
  72. * 缓存过期时间
  73. +----------------------------------------------------------
  74. * @var integer
  75. * @access protected
  76. +----------------------------------------------------------
  77. */
  78. protected $expire ;
  79. /**
  80. +----------------------------------------------------------
  81. * 连接缓存
  82. +----------------------------------------------------------
  83. * @access public
  84. +----------------------------------------------------------
  85. * @param string $type 缓存类型
  86. * @param array $options 配置数组
  87. +----------------------------------------------------------
  88. * @return object
  89. +----------------------------------------------------------
  90. * @throws ThinkExecption
  91. +----------------------------------------------------------
  92. */
  93. public function connect($type='',$options=array())
  94. {
  95. if(empty($type)) $type = C('DATA_CACHE_TYPE');
  96. $cachePath = dirname(__FILE__).'/Cache/';
  97. $cacheClass = 'Cache'.ucwords(strtolower(trim($type)));
  98. require_cache($cachePath.$cacheClass.'.class.php');
  99. if(class_exists($cacheClass))
  100. $cache = new $cacheClass($options);
  101. else
  102. throw_exception(L('_CACHE_TYPE_INVALID_').':'.$type);
  103. return $cache;
  104. }
  105. public function __get($name) {
  106. return $this->get($name);
  107. }
  108. public function __set($name,$value) {
  109. return $this->set($name,$value);
  110. }
  111. public function __unset($name) {
  112. $this->rm($name);
  113. }
  114. public function setOptions($name,$value) {
  115. $this->options[$name] = $value;
  116. }
  117. public function getOptions($name) {
  118. return $this->options[$name];
  119. }
  120. /**
  121. +----------------------------------------------------------
  122. * 取得缓存类实例
  123. +----------------------------------------------------------
  124. * @static
  125. * @access public
  126. +----------------------------------------------------------
  127. * @return mixed
  128. +----------------------------------------------------------
  129. */
  130. static function getInstance()
  131. {
  132. $param = func_get_args();
  133. return get_instance_of(__CLASS__,'connect',$param);
  134. }
  135. // 读取缓存次数
  136. public function Q($times='') {
  137. static $_times = 0;
  138. if(empty($times))
  139. return $_times;
  140. else
  141. $_times++;
  142. }
  143. // 写入缓存次数
  144. public function W($times='') {
  145. static $_times = 0;
  146. if(empty($times))
  147. return $_times;
  148. else
  149. $_times++;
  150. }
  151. }//类定义结束
  152. ?>