C_Pope_Cache_MultiFile

The NextGEN Gallery C Pope Cache MultiFile class.

Defined (1)

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

/pope/lib/class.pope_cache.php  
  1. class C_Pope_Cache_MultiFile implements I_Pope_Cache_Driver 
  2. static $initialized = FALSE; 
  3. static $cache_dir = NULL; 
  4. static $use_cache_subdir = TRUE; 
  5.  
  6. public static function initialize() 
  7. if (self::$initialized) 
  8. return; 
  9.  
  10. if (is_null(self::$cache_dir)) 
  11. self::set_cache_dir(); 
  12.  
  13. self::$initialized = TRUE; 
  14.  
  15. public static function add_key_prefix($prefix) 
  16. self::set_cache_dir(); 
  17.  
  18. public static function lookup($key, $default=NULL) 
  19. self::initialize(); 
  20.  
  21. $filename = self::get_filename_from_key($key); 
  22.  
  23. if (@file_exists($filename)) 
  24. return json_decode(@file_get_contents($filename)); 
  25. else 
  26. return $default; 
  27.  
  28. public static function update($key, $value) 
  29. self::initialize(); 
  30.  
  31. // TODO: log/warn users their cache dir can't be used 
  32. if (@file_exists(self::$cache_dir) && !@is_dir(self::$cache_dir)) 
  33. return; 
  34.  
  35. $filename = self::get_filename_from_key($key); 
  36.  
  37. if (@file_exists($filename) && C_Pope_Cache::$force_update == FALSE) 
  38. return; 
  39.  
  40. @file_put_contents($filename, json_encode($value)); 
  41.  
  42. public static function flush() 
  43. self::initialize(); 
  44.  
  45. $dir = self::$cache_dir; 
  46. if (@file_exists($dir) && @is_dir($dir)) 
  47. foreach (@scandir($dir) as $file) { 
  48. if ($file == '.' || $file == '..') 
  49. continue; 
  50. $file = self::join_paths($dir, $file); 
  51. if (is_dir($file) && self::$use_cache_subdir) 
  52. self::flush($dir); 
  53. else { 
  54. if (!self::$use_cache_subdir && strpos(basename($file), implode('_', C_Pope_Cache::$key_prefix) . '_') === 0) 
  55. @unlink($file); 
  56. elseif (self::$use_cache_subdir) 
  57. @unlink($file); 
  58.  
  59. public static function set_cache_dir() 
  60. if (defined('POPE_CACHE_DIR')) 
  61. self::$cache_dir = POPE_CACHE_DIR; 
  62. if (!@file_exists(self::$cache_dir)) 
  63. @mkdir(self::$cache_dir, 0777, TRUE); 
  64. else { 
  65. if (self::$use_cache_subdir) 
  66. self::$cache_dir = self::join_paths(sys_get_temp_dir(), C_Pope_cache::$key_prefix); 
  67. if (!@file_exists(self::$cache_dir)) 
  68. // if we can't create a subdirectory we fallback to prefixing filenames (eg /tmp/pope_$key) 
  69. $mkdir_result = @mkdir(self::$cache_dir, 0777, TRUE); 
  70. if (FALSE === $mkdir_result) 
  71. self::$use_cache_subdir = FALSE; 
  72. self::$cache_dir = self::join_paths(sys_get_temp_dir()); 
  73. else { 
  74. self::$cache_dir = self::join_paths(sys_get_temp_dir()); 
  75.  
  76. $func = function_exists('wp_is_writable') ? 'wp_is_writable' : 'is_writable'; 
  77. if (!@$func(self::$cache_dir)) 
  78. C_Pope_Cache::$enabled = FALSE; 
  79.  
  80. public static function get_filename_from_key($key) 
  81. if (self::$use_cache_subdir) 
  82. $filename = self::join_paths(self::$cache_dir, $key); 
  83. else 
  84. $filename = self::join_paths(self::$cache_dir) . DIRECTORY_SEPARATOR . implode('_', C_Pope_Cache::$key_prefix) . '_' . $key; 
  85. return $filename; 
  86.  
  87. public static function join_paths() 
  88. $args = func_get_args(); 
  89. foreach ($args as &$arg) { 
  90. if (is_array($arg)) 
  91. $arg = implode(DIRECTORY_SEPARATOR, $arg); 
  92. return implode(DIRECTORY_SEPARATOR, $args);