GFCache

Notes: 1.

Defined (1)

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

/common.php  
  1. class GFCache { 
  2. private static $_transient_prefix = 'GFCache_'; 
  3. private static $_cache = array(); 
  4.  
  5. public static function get( $key, &$found = null, $is_persistent=true ) { 
  6. global $blog_id; 
  7. if ( is_multisite() ) { 
  8. $key = $blog_id . ':' . $key; 
  9.  
  10. if ( isset( self::$_cache[ $key ] ) ) { 
  11. $found = true; 
  12. $data = rgar( self::$_cache[ $key ], 'data' ); 
  13.  
  14. return $data; 
  15.  
  16. //If set to not persistent, do not check transient for performance reasons 
  17. if (! $is_persistent ) { 
  18. $found = false; 
  19. return false; 
  20.  
  21. $data = self::get_transient( $key ); 
  22.  
  23. if ( false === ( $data ) ) { 
  24. $found = false; 
  25.  
  26. return false; 
  27. } else { 
  28. self::$_cache[ $key ] = array( 'data' => $data, 'is_persistent' => true ); 
  29. $found = true; 
  30.  
  31. return $data; 
  32.  
  33.  
  34. public static function set( $key, $data, $is_persistent = false, $expiration = 0 ) { 
  35. global $blog_id; 
  36. $success = true; 
  37.  
  38. if ( is_multisite() ) { 
  39. $key = $blog_id . ':' . $key; 
  40.  
  41. if ( $is_persistent ) { 
  42. $success = self::set_transient( $key, $data, $expiration ); 
  43.  
  44. self::$_cache[ $key ] = array( 'data' => $data, 'is_persistent' => $is_persistent ); 
  45.  
  46. return $success; 
  47.  
  48. public static function delete( $key ) { 
  49. global $blog_id; 
  50. $success = true; 
  51.  
  52. if ( is_multisite() ) { 
  53. $key = $blog_id . ':' . $key; 
  54.  
  55. if ( isset( self::$_cache[ $key ] ) ) { 
  56. if ( self::$_cache[ $key ]['is_persistent'] ) { 
  57. $success = self::delete_transient( $key ); 
  58.  
  59. unset( self::$_cache[ $key ] ); 
  60. } else { 
  61. $success = self::delete_transient( $key ); 
  62.  
  63.  
  64. return $success; 
  65.  
  66. public static function flush( $flush_persistent = false ) { 
  67. global $wpdb; 
  68.  
  69. self::$_cache = array(); 
  70.  
  71. if ( false === $flush_persistent ) { 
  72. return true; 
  73.  
  74. if ( is_multisite() ) { 
  75. $sql = " 
  76. DELETE FROM $wpdb->sitemeta 
  77. WHERE meta_key LIKE '_site_transient_timeout_GFCache_%' OR 
  78. meta_key LIKE '_site_transient_GFCache_%' 
  79. "; 
  80. } else { 
  81. $sql = " 
  82. DELETE FROM $wpdb->options 
  83. WHERE option_name LIKE '_transient_timeout_GFCache_%' OR 
  84. option_name LIKE '_transient_GFCache_%' 
  85. "; 
  86.  
  87. $rows_deleted = $wpdb->query( $sql ); 
  88.  
  89. $success = $rows_deleted !== false ? true : false; 
  90.  
  91. return $success; 
  92.  
  93. private static function delete_transient( $key ) { 
  94. $key = self::$_transient_prefix . wp_hash( $key ); 
  95. if ( is_multisite() ) { 
  96. $success = delete_site_transient( $key ); 
  97. } else { 
  98. $success = delete_transient( $key ); 
  99.  
  100. return $success; 
  101.  
  102. private static function set_transient( $key, $data, $expiration ) { 
  103. $key = self::$_transient_prefix . wp_hash( $key ); 
  104. if ( is_multisite() ) { 
  105. $success = set_site_transient( $key, $data, $expiration ); 
  106. } else { 
  107. $success = set_transient( $key, $data, $expiration ); 
  108.  
  109. return $success; 
  110.  
  111. private static function get_transient( $key ) { 
  112. $key = self::$_transient_prefix . wp_hash( $key ); 
  113. if ( is_multisite() ) { 
  114. $data = get_site_transient( $key ); 
  115. } else { 
  116. $data = get_transient( $key ); 
  117.  
  118. return $data; 
  119.