C_Photocrati_Transient_Manager

The NextGEN Gallery C Photocrati Transient Manager class.

Defined (1)

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

/non_pope/class.photocrati_transient_manager.php  
  1. class C_Photocrati_Transient_Manager 
  2. private $_groups = array(); 
  3. static $_instance = NULL; 
  4.  
  5. /** 
  6. * @return C_Photocrati_Transient_Manager 
  7. */ 
  8. static function get_instance() 
  9. if (!self::$_instance) { 
  10. $klass = get_class(); 
  11. self::$_instance = new $klass; 
  12. return self::$_instance; 
  13.  
  14. function __construct() 
  15. global $_wp_using_ext_object_cache; 
  16.  
  17. $this->_groups = get_option('ngg_transient_groups', array('__counter' => 1)); 
  18. if ($_wp_using_ext_object_cache) $this->_tracker = get_option('photocrati_cache_tracker', array()); 
  19. register_shutdown_function(array(&$this, '_update_tracker')); 
  20.  
  21. function delete_tracked($group=NULL) 
  22. global $_wp_using_ext_object_cache; 
  23. if ($_wp_using_ext_object_cache) { 
  24. if ($group) { 
  25. if (is_array($this->_tracker) && isset($this->_tracker[$this->get_group_id($group)])) { 
  26. foreach ($this->_tracker[$this->get_group_id($group)] as $key) { 
  27. delete_transient($this->get_group_id($group).'__'.$key); 
  28. unset($this->_tracker[$this->get_group_id($group)]); 
  29. else foreach($this->_groups as $group => $data) $this->delete_tracked($group); 
  30.  
  31. function _update_tracker() 
  32. global $_wp_using_ext_object_cache; 
  33. if ($_wp_using_ext_object_cache) { 
  34. delete_option('photocrati_cache_tracker'); 
  35. add_option('photocrati_cache_tracker', $this->_tracker, '', 'no'); 
  36.  
  37. function add_group($group_or_groups) 
  38. $updated = FALSE; 
  39. $groups = is_array($group_or_groups) ? $group_or_groups : array($group_or_groups); 
  40. foreach ($groups as $group) { 
  41. if (!isset($this->_groups[$group])) { 
  42. $id = $this->_groups['__counter'] += 1; 
  43. $this->_groups[$group] = array('id' => $id, 'enabled' => TRUE); 
  44. $updated = TRUE; 
  45. if ($updated) update_option('ngg_transient_groups', $this->_groups); 
  46.  
  47.  
  48. function get_group_id($group_name) 
  49. $this->add_group($group_name); 
  50.  
  51. return $this->_groups[$group_name]['id']; 
  52.  
  53. function generate_key($group, $params=array()) 
  54. if (is_object($params)) $params = (array) $params; 
  55. if (is_array($params)) { 
  56. foreach ($params as &$param) $param = @json_encode($param); 
  57. $params = implode('', $params); 
  58.  
  59. return $this->get_group_id($group).'__'.str_replace('-', '_', crc32($params)); 
  60.  
  61. function get($key, $default=NULL, $lookup=NULL) 
  62. $retval = $default; 
  63.  
  64. if (is_null($lookup)) { 
  65. if (defined('PHOTOCRATI_CACHE')) { 
  66. $lookup = PHOTOCRATI_CACHE; 
  67.  
  68. if ($lookup) { 
  69. $retval = json_decode(get_transient($key)); 
  70. if (is_object($retval)) $retval = (array) $retval; 
  71. if (is_null($retval)) $retval = $default; 
  72.  
  73. return $retval; 
  74.  
  75. function _track_key($key) 
  76. global $_wp_using_ext_object_cache; 
  77. if ($_wp_using_ext_object_cache) { 
  78. $parts = explode('__', $key); 
  79. $group = $parts[0]; 
  80. $id = $parts[1]; 
  81. if (!isset($this->_tracker[$group])) $this->_tracker[$group] = array(); 
  82. $this->_tracker[$group][] = $id; 
  83.  
  84. function set($key, $value, $ttl=0) 
  85. $retval = FALSE; 
  86. $enabled = TRUE; 
  87. if (defined('PHOTOCRATI_CACHE')) $enabled = PHOTOCRATI_CACHE; 
  88. if (defined('PHOTOCRATI_CACHE_TTL') && !$ttl) $ttl = PHOTOCRATI_CACHE_TTL; 
  89. if ($enabled) { 
  90. $retval = set_transient($key, json_encode($value), $ttl); 
  91. if ($retval) $this->_track_key($key); 
  92.  
  93. return $retval; 
  94.  
  95. function delete($key) 
  96. return delete_transient($key); 
  97.  
  98. function clear($group=NULL) 
  99. if (is_string($group) && !empty($group)) 
  100. global $wpdb; 
  101. $query = "DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient%' AND option_name LIKE '%{$this->get_group_id($group)}__%'"; 
  102. $wpdb->query($query); 
  103. $this->delete_tracked($group); 
  104. else foreach ($this->_groups as $name => $params) { 
  105. $this->clear($name); 
  106.  
  107. static function update($key, $value, $ttl=NULL) 
  108. return self::get_instance()->set($key, $value, $ttl); 
  109.  
  110. static function fetch($key, $default=NULL) 
  111. return self::get_instance()->get($key, $default); 
  112.  
  113. static function flush($group=NULL) 
  114. return self::get_instance()->clear($group); 
  115.  
  116. static function create_key($group, $params=array()) 
  117. return self::get_instance()->generate_key($group, $params);