W3TCCache_Xcache

XCache class.

Defined (1)

The class is defined in the following location(s).

/Cache_Xcache.php  
  1. class Cache_Xcache extends Cache_Base { 
  2.  
  3. /** 
  4. * Used for faster flushing 
  5. * @var integer $_key_version 
  6. */ 
  7. private $_key_version = array(); 
  8.  
  9. /** 
  10. * Adds data 
  11. * @param string $key 
  12. * @param mixed $var 
  13. * @param integer $expire 
  14. * @param string $group Used to differentiate between groups of cache values 
  15. * @return boolean 
  16. */ 
  17. function add( $key, &$var, $expire = 0, $group = '' ) { 
  18. if ( $this->get( $key, $group ) === false ) { 
  19. return $this->set( $key, $var, $expire, $group ); 
  20.  
  21. return false; 
  22.  
  23. /** 
  24. * Sets data 
  25. * @param string $key 
  26. * @param mixed $var 
  27. * @param integer $expire 
  28. * @param string $group Used to differentiate between groups of cache values 
  29. * @return boolean 
  30. */ 
  31. function set( $key, $var, $expire = 0, $group = '' ) { 
  32. $var['key_version'] = $this->_get_key_version( $group ); 
  33.  
  34. $storage_key = $this->get_item_key( $key ); 
  35. return xcache_set( $storage_key, serialize( $var ), $expire ); 
  36.  
  37. /** 
  38. * Returns data 
  39. * @param string $key 
  40. * @param string $group Used to differentiate between groups of cache values 
  41. * @return mixed 
  42. */ 
  43. function get_with_old( $key, $group = '' ) { 
  44. $has_old_data = false; 
  45.  
  46. $storage_key = $this->get_item_key( $key ); 
  47.  
  48. $v = @unserialize( xcache_get( $storage_key ) ); 
  49. if ( !is_array( $v ) || !isset( $v['key_version'] ) ) 
  50. return array( null, $has_old_data ); 
  51.  
  52. $key_version = $this->_get_key_version( $group ); 
  53. if ( $v['key_version'] == $key_version ) 
  54. return array( $v, $has_old_data ); 
  55.  
  56. if ( $v['key_version'] > $key_version ) { 
  57. $this->_set_key_version( $v['key_version'], $group ); 
  58. return array( $v, $has_old_data ); 
  59.  
  60. // key version is old 
  61. if ( !$this->_use_expired_data ) 
  62. return array( null, $has_old_data ); 
  63.  
  64. // if we have expired data - update it for future use and let 
  65. // current process recalculate it 
  66. $expires_at = isset( $v['expires_at'] ) ? $v['expires_at'] : null; 
  67. if ( $expires_at == null || time() > $expires_at ) { 
  68. $v['expires_at'] = time() + 30; 
  69. xcache_set( $storage_key, serialize( $v ), 0 ); 
  70. $has_old_data = true; 
  71.  
  72. return array( null, $has_old_data ); 
  73.  
  74. // return old version 
  75. return array( $v, $has_old_data ); 
  76.  
  77. /** 
  78. * Replaces data 
  79. * @param string $key 
  80. * @param mixed $var 
  81. * @param integer $expire 
  82. * @param string $group Used to differentiate between groups of cache values 
  83. * @return boolean 
  84. */ 
  85. function replace( $key, &$var, $expire = 0, $group = '' ) { 
  86. if ( $this->get( $key, $group ) !== false ) { 
  87. return $this->set( $key, $var, $expire, $group ); 
  88.  
  89. return false; 
  90.  
  91. /** 
  92. * Deletes data 
  93. * @param string $key 
  94. * @param string $group 
  95. * @return boolean 
  96. */ 
  97. function delete( $key, $group = '' ) { 
  98. $storage_key = $this->get_item_key( $key ); 
  99.  
  100. if ( $this->_use_expired_data ) { 
  101. $v = @unserialize( xcache_get( $storage_key ) ); 
  102. if ( is_array( $v ) ) { 
  103. $v['key_version'] = 0; 
  104. xcache_set( $storage_key, serialize( $v ), 0 ); 
  105. return true; 
  106.  
  107. return xcache_unset( $storage_key ); 
  108.  
  109. /** 
  110. * Key to delete, deletes _old and primary if exists. 
  111. * @param unknown $key 
  112. * @return bool 
  113. */ 
  114. function hard_delete( $key ) { 
  115. $storage_key = $this->get_item_key( $key ); 
  116. return xcache_unset( $storage_key ); 
  117.  
  118. /** 
  119. * Flushes all data 
  120. * @param string $group Used to differentiate between groups of cache values 
  121. * @return boolean 
  122. */ 
  123. function flush( $group = '' ) { 
  124. $this->_get_key_version( $group ); // initialize $this->_key_version 
  125. $this->_key_version[$group]++; 
  126. $this->_set_key_version( $this->_key_version[$group], $group ); 
  127. return true; 
  128.  
  129. /** 
  130. * Checks if engine can function properly in this environment 
  131. * @return bool 
  132. */ 
  133. public function available() { 
  134. return function_exists( 'xcache_set' ); 
  135.  
  136. /** 
  137. * Returns key postfix 
  138. * @param string $group Used to differentiate between groups of cache values 
  139. * @return integer 
  140. */ 
  141. private function _get_key_version( $group = '' ) { 
  142. if ( !isset( $this->_key_version[$group] ) || $this->_key_version[$group] <= 0 ) { 
  143. $v = xcache_get( $this->_get_key_version_key( $group ) ); 
  144. $v = intval( $v ); 
  145. $this->_key_version[$group] = ( $v > 0 ? $v : 1 ); 
  146.  
  147. return $this->_key_version[$group]; 
  148.  
  149. /** 
  150. * Sets new key version 
  151. * @param unknown $v 
  152. * @param string $group Used to differentiate between groups of cache values 
  153. * @return boolean 
  154. */ 
  155. private function _set_key_version( $v, $group = '' ) { 
  156. xcache_set( $this->_get_key_version_key( $group ), $v, 0 ); 
  157.  
  158. /** 
  159. * Used to replace as atomically as possible known value to new one 
  160. */ 
  161. public function set_if_maybe_equals( $key, $old_value, $new_value ) { 
  162. // cant guarantee atomic action here, filelocks fail often 
  163. $value = $this->get( $key ); 
  164. if ( isset( $old_value['content'] ) && 
  165. $value['content'] != $old_value['content'] ) 
  166. return false; 
  167.  
  168. return $this->set( $key, $new_value ); 
  169.  
  170. /** 
  171. * Use key as a counter and add integet value to it 
  172. */ 
  173. public function counter_add( $key, $value ) { 
  174. if ( $value == 0 ) 
  175. return true; 
  176.  
  177. $storage_key = $this->get_item_key( $key ); 
  178. $r = xcache_inc( $storage_key, $value ); 
  179. if ( !$r ) // it doesnt initialize counter by itself 
  180. $this->counter_set( $key, 0 ); 
  181.  
  182. return $r; 
  183.  
  184. /** 
  185. * Use key as a counter and add integet value to it 
  186. */ 
  187. public function counter_set( $key, $value ) { 
  188. $storage_key = $this->get_item_key( $key ); 
  189. return xcache_set( $storage_key, $value ); 
  190.  
  191. /** 
  192. * Get counter's value 
  193. */ 
  194. public function counter_get( $key ) { 
  195. $storage_key = $this->get_item_key( $key ); 
  196. $v = (int)xcache_get( $storage_key ); 
  197.  
  198. return $v;