Cache.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. // +--------------------------------------------------------------------------
  3. // | Senthot [ DEVELOPED BY ME ]
  4. // +--------------------------------------------------------------------------
  5. // | Copyright (c) 2005-2013 http://www.senthot.com All rights reserved.
  6. // | License ( http://www.apache.org/licenses/LICENSE-2.0 )
  7. // | Author: ms134n ( [email protected] )
  8. // +--------------------------------------------------------------------------
  9. /**
  10. * Cache Management
  11. * @category Sen
  12. * @package Sen
  13. * @subpackage Core
  14. * @author ms134n <[email protected]>
  15. */
  16. class Cache {
  17. /**
  18. * Operating handle
  19. * @var string
  20. * @access protected
  21. */
  22. protected $handler ;
  23. /**
  24. * Cache connection parameters
  25. * @var integer
  26. * @access protected
  27. */
  28. protected $options = array();
  29. /**
  30. * Connection cache
  31. * @access public
  32. * @param string $type Cache type
  33. * @param array $options Configuration Array
  34. * @return object
  35. */
  36. public function connect($type='',$options=array()) {
  37. if(empty($type)) $type = C('DATA_CACHE_TYPE');
  38. $type = strtolower(trim($type));
  39. $class = 'Cache'.ucwords($type);
  40. if(class_exists($class))
  41. $cache = new $class($options);
  42. else
  43. throw_exception(L('_CACHE_TYPE_INVALID_').':'.$type);
  44. return $cache;
  45. }
  46. public function __get($name) {
  47. return $this->get($name);
  48. }
  49. public function __set($name,$value) {
  50. return $this->set($name,$value);
  51. }
  52. public function __unset($name) {
  53. $this->rm($name);
  54. }
  55. public function setOptions($name,$value) {
  56. $this->options[$name] = $value;
  57. }
  58. public function getOptions($name) {
  59. return $this->options[$name];
  60. }
  61. /**
  62. * Obtain class instance cache
  63. * @static
  64. * @access public
  65. * @return mixed
  66. */
  67. static function getInstance() {
  68. $param = func_get_args();
  69. return get_instance_of(__CLASS__,'connect',$param);
  70. }
  71. /**
  72. * Queue cache
  73. * @access protected
  74. * @param string $key Queue name
  75. * @return mixed
  76. */
  77. //
  78. protected function queue($key) {
  79. static $_handler = array(
  80. 'file' => array('F','F'),
  81. 'xcache'=> array('xcache_get','xcache_set'),
  82. 'apc' => array('apc_fetch','apc_store'),
  83. );
  84. $queue = isset($this->options['queue'])?$this->options['queue']:'file';
  85. $fun = isset($_handler[$queue])?$_handler[$queue]:$_handler['file'];
  86. $queue_name=isset($this->options['queue_name'])?$this->options['queue_name']:'sen_queue';
  87. $value = $fun[0]($queue_name);
  88. if(!$value) {
  89. $value = array();
  90. }
  91. // Into the column
  92. if(false===array_search($key, $value)) array_push($value,$key);
  93. if(count($value) > $this->options['length']) {
  94. // Dequeuing
  95. $key = array_shift($value);
  96. // Deleting the cache
  97. $this->rm($key);
  98. if(APP_DEUBG){
  99. //Debug mode, recording the number of columns
  100. N($queue_name.'_out_times',1,true);
  101. }
  102. }
  103. return $fun[1]($queue_name,$value);
  104. }
  105. public function __call($method,$args){
  106. //Own method called cache type
  107. if(method_exists($this->handler, $method)){
  108. return call_user_func_array(array($this->handler,$method), $args);
  109. }else{
  110. throw_exception(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
  111. return;
  112. }
  113. }
  114. }