FS_Cache_Manager

The NextGEN Gallery FS Cache Manager class.

Defined (1)

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

/freemius/includes/managers/class-fs-cache-manager.php  
  1. class FS_Cache_Manager { 
  2. /** 
  3. * @var FS_Option_Manager 
  4. */ 
  5. private $_options; 
  6. /** 
  7. * @var FS_Logger 
  8. */ 
  9. private $_logger; 
  10.  
  11. /** 
  12. * @var FS_Cache_Manager[] 
  13. */ 
  14. private static $_MANAGERS = array(); 
  15.  
  16. /** 
  17. * @author Vova Feldman (@svovaf) 
  18. * @since 1.1.3 
  19. * @param string $id 
  20. */ 
  21. private function __construct( $id ) { 
  22. $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_cach_mngr_' . $id, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK ); 
  23.  
  24. $this->_logger->entrance(); 
  25. $this->_logger->log( 'id = ' . $id ); 
  26.  
  27. $this->_options = FS_Option_Manager::get_manager( $id, true ); 
  28.  
  29. /** 
  30. * @author Vova Feldman (@svovaf) 
  31. * @since 1.1.6 
  32. * @param $id 
  33. * @return FS_Cache_Manager 
  34. */ 
  35. static function get_manager( $id ) { 
  36. $id = strtolower( $id ); 
  37.  
  38. if ( ! isset( self::$_MANAGERS[ $id ] ) ) { 
  39. self::$_MANAGERS[ $id ] = new FS_Cache_Manager( $id ); 
  40.  
  41. return self::$_MANAGERS[ $id ]; 
  42.  
  43. /** 
  44. * @author Vova Feldman (@svovaf) 
  45. * @since 1.1.6 
  46. * @return bool 
  47. */ 
  48. function is_empty() { 
  49. $this->_logger->entrance(); 
  50.  
  51. return $this->_options->is_empty(); 
  52.  
  53. /** 
  54. * @author Vova Feldman (@svovaf) 
  55. * @since 1.1.6 
  56. */ 
  57. function clear() { 
  58. $this->_logger->entrance(); 
  59.  
  60. $this->_options->clear( true ); 
  61.  
  62. /** 
  63. * Delete cache manager from DB. 
  64. * @author Vova Feldman (@svovaf) 
  65. * @since 1.0.9 
  66. */ 
  67. function delete() { 
  68. $this->_options->delete(); 
  69.  
  70. /** 
  71. * Check if there's a cached item. 
  72. * @author Vova Feldman (@svovaf) 
  73. * @since 1.1.6 
  74. * @param string $key 
  75. * @return bool 
  76. */ 
  77. function has( $key ) { 
  78. $cache_entry = $this->_options->get_option( $key, false ); 
  79.  
  80. return ( is_object( $cache_entry ) && 
  81. isset( $cache_entry->timestamp ) && 
  82. is_numeric( $cache_entry->timestamp ) 
  83. ); 
  84.  
  85. /** 
  86. * Check if there's a valid cached item. 
  87. * @author Vova Feldman (@svovaf) 
  88. * @since 1.1.6 
  89. * @param string $key 
  90. * @return bool 
  91. */ 
  92. function has_valid( $key ) { 
  93. $cache_entry = $this->_options->get_option( $key, false ); 
  94.  
  95. return ( is_object( $cache_entry ) && 
  96. isset( $cache_entry->timestamp ) && 
  97. is_numeric( $cache_entry->timestamp ) && 
  98. $cache_entry->timestamp > WP_FS__SCRIPT_START_TIME 
  99. ); 
  100.  
  101. /** 
  102. * @author Vova Feldman (@svovaf) 
  103. * @since 1.1.6 
  104. * @param string $key 
  105. * @param mixed $default 
  106. * @return mixed 
  107. */ 
  108. function get( $key, $default = null ) { 
  109. $this->_logger->entrance( 'key = ' . $key ); 
  110.  
  111. $cache_entry = $this->_options->get_option( $key, false ); 
  112.  
  113. if ( is_object( $cache_entry ) && 
  114. isset( $cache_entry->timestamp ) && 
  115. is_numeric( $cache_entry->timestamp ) 
  116. ) { 
  117. return $cache_entry->result; 
  118.  
  119. return $default; 
  120.  
  121. /** 
  122. * @author Vova Feldman (@svovaf) 
  123. * @since 1.1.6 
  124. * @param string $key 
  125. * @param mixed $default 
  126. * @return mixed 
  127. */ 
  128. function get_valid( $key, $default = null ) { 
  129. $this->_logger->entrance( 'key = ' . $key ); 
  130.  
  131. $cache_entry = $this->_options->get_option( $key, false ); 
  132.  
  133. if ( is_object( $cache_entry ) && 
  134. isset( $cache_entry->timestamp ) && 
  135. is_numeric( $cache_entry->timestamp ) && 
  136. $cache_entry->timestamp > WP_FS__SCRIPT_START_TIME 
  137. ) { 
  138. return $cache_entry->result; 
  139.  
  140. return $default; 
  141.  
  142. /** 
  143. * @author Vova Feldman (@svovaf) 
  144. * @since 1.1.6 
  145. * @param string $key 
  146. * @param mixed $value 
  147. * @param int $expiration 
  148. */ 
  149. function set( $key, $value, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) { 
  150. $this->_logger->entrance( 'key = ' . $key ); 
  151.  
  152. $cache_entry = new stdClass(); 
  153. $cache_entry->result = $value; 
  154. $cache_entry->timestamp = WP_FS__SCRIPT_START_TIME + $expiration; 
  155. $this->_options->set_option( $key, $cache_entry, true ); 
  156.  
  157. /** 
  158. * Get cached record expiration, or false if not cached or expired. 
  159. * @author Vova Feldman (@svovaf) 
  160. * @since 1.1.7.3 
  161. * @param string $key 
  162. * @return bool|int 
  163. */ 
  164. function get_record_expiration( $key ) { 
  165. $this->_logger->entrance( 'key = ' . $key ); 
  166.  
  167. $cache_entry = $this->_options->get_option( $key, false ); 
  168.  
  169. if ( is_object( $cache_entry ) && 
  170. isset( $cache_entry->timestamp ) && 
  171. is_numeric( $cache_entry->timestamp ) && 
  172. $cache_entry->timestamp > WP_FS__SCRIPT_START_TIME 
  173. ) { 
  174. return $cache_entry->timestamp; 
  175.  
  176. return false; 
  177.  
  178. /** 
  179. * Purge cached item. 
  180. * @author Vova Feldman (@svovaf) 
  181. * @since 1.1.6 
  182. * @param string $key 
  183. */ 
  184. function purge( $key ) { 
  185. $this->_logger->entrance( 'key = ' . $key ); 
  186.  
  187. $this->_options->unset_option( $key, true );