From a80d0ccdcfdd09a68af263dbaeaa38d5b84978f6 Mon Sep 17 00:00:00 2001 From: Tim Gunter Date: Fri, 18 Jun 2010 17:35:05 -0400 Subject: [PATCH] Change FileCache to LibraryMap. Final commit on this. --- bootstrap.php | 1 - library/core/class.filecache.php | 237 ------------------------------ library/core/class.librarymap.php | 236 ++++++++++++++++++++++++++++- 3 files changed, 233 insertions(+), 241 deletions(-) delete mode 100644 library/core/class.filecache.php diff --git a/bootstrap.php b/bootstrap.php index 60f0810483b..d3657842e72 100755 --- a/bootstrap.php +++ b/bootstrap.php @@ -36,7 +36,6 @@ require_once(PATH_LIBRARY_CORE . DS . 'class.router.php'); require_once(PATH_LIBRARY_CORE . DS . 'class.dispatcher.php'); require_once(PATH_LIBRARY_CORE . DS . 'class.filesystem.php'); -require_once(PATH_LIBRARY_CORE . DS . 'class.filecache.php'); require_once(PATH_LIBRARY_CORE . DS . 'class.librarymap.php'); require_once(PATH_LIBRARY_CORE . DS . 'class.format.php'); require_once(PATH_LIBRARY_CORE . DS . 'class.model.php'); diff --git a/library/core/class.filecache.php b/library/core/class.filecache.php deleted file mode 100644 index 0aa39db8b73..00000000000 --- a/library/core/class.filecache.php +++ /dev/null @@ -1,237 +0,0 @@ -. -Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com -*/ - -/** - * Handle the creation, usage, and deletion of file cache entries - * - * @author Tim Gunter - * @copyright 2003 Mark O'Sullivan - * @license http://www.opensource.org/licenses/gpl-2.0.php GPL - * @package Garden - * @version @@GARDEN-VERSION@@ - * @namespace Garden.Core - */ - -class Gdn_FileCache { - - /** - * Sprintf format string that describes the on-disk name of the mapping caches - * - * @const string - */ - const DISK_CACHE_NAME_FORMAT = '%s_mappings.php'; - - /** - * Holds the in-memory array of cache entries - * - * @var array - */ - public static $_Caches; - - /** - * Prepare a cache library for use, either by loading it from file, filling it with - * pre existing data in array form, or leaving it empty an waiting for new entries. - * - * @param string $CacheName name of cache library - * @param array $ExistingCacheArray optional array containing an initial seed cache - * @return void - */ - public static function PrepareCache($CacheName, $ExistingCacheArray=NULL) { - // Onetime initialization of in-memory file cache - if (!is_array(Gdn_LibraryMap::$_Caches)) - Gdn_LibraryMap::$_Caches = array(); - - if (!array_key_exists($CacheName,Gdn_LibraryMap::$_Caches)) { - $OnDiskCacheName = sprintf(Gdn_LibraryMap::DISK_CACHE_NAME_FORMAT,strtolower($CacheName)); - - Gdn_LibraryMap::$_Caches[$CacheName] = array( - 'ondisk' => $OnDiskCacheName, - 'cache' => array() - ); - - // Loading cache for the first time by name+path only... import data now. - if (file_exists(PATH_CACHE.DS.$OnDiskCacheName)) { - require_once(PATH_CACHE.DS.$OnDiskCacheName); - } - } - - // If cache data array is passed in, merge it with our existing cache - if (is_array($ExistingCacheArray)) - Gdn_LibraryMap::Import($CacheName, $ExistingCacheArray); - } - - /** - * Import an existing well formed cache chunk into the supplied library - * - * @param string $CacheName name of cache library - * @param array $CacheContents well formed cache array - * @return void - */ - public static function Import($CacheName, $CacheContents) { - if (!array_key_exists($CacheName,Gdn_LibraryMap::$_Caches)) - return FALSE; - - Gdn_LibraryMap::$_Caches[$CacheName]['cache'] = array_merge(Gdn_LibraryMap::$_Caches[$CacheName]['cache'], $CacheContents); - Gdn_LibraryMap::SaveCache($CacheName); - } - - /** - * Clear the contents of the supplied cache, and remove it from disk - * - * @param string $CacheName name of cache library - * @return void - */ - public static function ClearCache($CacheName) { - if (!array_key_exists($CacheName,Gdn_LibraryMap::$_Caches)) - return Gdn_LibraryMap::PrepareCache($CacheName); - - Gdn_LibraryMap::$_Caches[$CacheName]['cache'] = array(); - @unlink(PATH_CACHE.DS.Gdn_LibraryMap::$_Caches[$CacheName]['ondisk']); - } - - /** - * Detect whether the cache has any items in it - * - * @param string $CacheName name of cache library - * @return bool ready state of cache - */ - public static function CacheReady($CacheName) { - if (!array_key_exists($CacheName,Gdn_LibraryMap::$_Caches)) - return FALSE; - - if (!sizeof(Gdn_LibraryMap::$_Caches[$CacheName]['cache'])) - return FALSE; - - return TRUE; - } - - /** - * Store the provided resource in the appropriate (named) cache - * - * @param string $CacheName name of cache library - * @param string $CacheKey name of cache entry - * @param mixed $CacheContents contents of cache entry - * @param bool $CacheWrite optional, whether or not to perform a disk write after this set. default yes - * @return mixed cache contents - */ - public static function Cache($CacheName, $CacheKey, $CacheContents, $CacheWrite=TRUE) { - if (!array_key_exists($CacheName,Gdn_LibraryMap::$_Caches)) - return FALSE; - - // Set and save cache data to memory and disk - Gdn_LibraryMap::$_Caches[$CacheName]['cache'][$CacheKey] = $CacheContents; - if ($CacheWrite === TRUE) - Gdn_LibraryMap::SaveCache($CacheName); - - return $CacheContents; - } - - public static function SafeCache($CacheName, $CacheKey, $CacheContents, $CacheWrite=TRUE) { - return Gdn_LibraryMap::Cache($CacheName, str_replace('.','__',$CacheKey), $CacheContents, $CacheWrite); - } - - /** - * Append the provided resource in the appropriate (named) cache under the named cache key. - * If the entry is not already an array, convert it to one... then append the new data. - * - * @param string $CacheName name of cache library - * @param string $CacheKey name of cache entry - * @param mixed $CacheContents contents of cache entry - * @param bool $CacheWrite optional, whether or not to perform a disk write after this set. default yes - * @return array cache contents - */ - public static function CacheArray($CacheName, $CacheKey, $CacheContents, $CacheWrite=TRUE) { - $ExistingCacheData = Gdn_LibraryMap::GetCache($CacheName, $CacheKey); - - if ($ExistingCacheData === NULL) - $ExistingCacheData = array(); - - if (!is_array($ExistingCacheData)) - $ExistingCacheData = array($ExistingCacheData); - - $ExistingCacheData[] = $CacheContents; - - // Save cache data to memory - return Gdn_LibraryMap::Cache($CacheName, $CacheKey, $ExistingCacheData, $CacheWrite); - } - - /** - * Retrieve an item from the cache - * - * @param string $CacheName name of cache library - * @param string $CacheKey name of cache entry - * @return mixed cache entry or null on failure - */ - public static function GetCache($CacheName, $CacheKey) { - if (array_key_exists($CacheKey,Gdn_LibraryMap::$_Caches[$CacheName]['cache'])) - return Gdn_LibraryMap::$_Caches[$CacheName]['cache'][$CacheKey]; - - return NULL; - } - - /** - * Save the provided library's data to the on disk location. - * - * @param string $CacheName name of cache library - * @return void - */ - public static function SaveCache($CacheName) { - if (!array_key_exists($CacheName,Gdn_LibraryMap::$_Caches)) - return FALSE; - - $FileName = Gdn_LibraryMap::$_Caches[$CacheName]['ondisk']; - - $CacheContents = " "; - - if (is_array($Cache)) - $CacheStr .= "array(\n"; - - $First = TRUE; - foreach ($Cache as $CacheKey => $CacheValue) { - if (!$First) { $CacheStr .= ",\n"; } - if ($First) { $First = FALSE; } - - if (!is_array($CacheValue)) { - $CacheStr .= str_repeat(' ',$FormatIndentLevel+1); - if (!is_numeric($CacheKey)) - $CacheStr .= "'{$CacheKey}' => "; - $CacheStr .= "'{$CacheValue}'"; - } - else { - Gdn_LibraryMap::RecurseArrayStr($CacheKey, $CacheValue, $CacheStr, $FormatIndentLevel+1); - } - } - if (is_array($Cache)) - $CacheStr .= "\n".str_repeat(' ',$FormatIndentLevel).")"; - } - -} \ No newline at end of file diff --git a/library/core/class.librarymap.php b/library/core/class.librarymap.php index 525f20dc956..fab02857777 100644 --- a/library/core/class.librarymap.php +++ b/library/core/class.librarymap.php @@ -1,7 +1,237 @@ -. +Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com +*/ -class Gdn_LibraryMap extends Gdn_FileCache { +/** + * Handle the creation, usage, and deletion of file cache entries + * + * @author Tim Gunter + * @copyright 2003 Mark O'Sullivan + * @license http://www.opensource.org/licenses/gpl-2.0.php GPL + * @package Garden + * @version @@GARDEN-VERSION@@ + * @namespace Garden.Core + */ +class Gdn_LibraryMap { + + /** + * Sprintf format string that describes the on-disk name of the mapping caches + * + * @const string + */ + const DISK_CACHE_NAME_FORMAT = '%s_mappings.php'; + + /** + * Holds the in-memory array of cache entries + * + * @var array + */ + public static $_Caches; + + /** + * Prepare a cache library for use, either by loading it from file, filling it with + * pre existing data in array form, or leaving it empty an waiting for new entries. + * + * @param string $CacheName name of cache library + * @param array $ExistingCacheArray optional array containing an initial seed cache + * @return void + */ + public static function PrepareCache($CacheName, $ExistingCacheArray=NULL) { + // Onetime initialization of in-memory file cache + if (!is_array(Gdn_LibraryMap::$_Caches)) + Gdn_LibraryMap::$_Caches = array(); + + if (!array_key_exists($CacheName,Gdn_LibraryMap::$_Caches)) { + $OnDiskCacheName = sprintf(Gdn_LibraryMap::DISK_CACHE_NAME_FORMAT,strtolower($CacheName)); + + Gdn_LibraryMap::$_Caches[$CacheName] = array( + 'ondisk' => $OnDiskCacheName, + 'cache' => array() + ); + + // Loading cache for the first time by name+path only... import data now. + if (file_exists(PATH_CACHE.DS.$OnDiskCacheName)) { + require_once(PATH_CACHE.DS.$OnDiskCacheName); + } + } + + // If cache data array is passed in, merge it with our existing cache + if (is_array($ExistingCacheArray)) + Gdn_LibraryMap::Import($CacheName, $ExistingCacheArray); + } + + /** + * Import an existing well formed cache chunk into the supplied library + * + * @param string $CacheName name of cache library + * @param array $CacheContents well formed cache array + * @return void + */ + public static function Import($CacheName, $CacheContents) { + if (!array_key_exists($CacheName,Gdn_LibraryMap::$_Caches)) + return FALSE; + + Gdn_LibraryMap::$_Caches[$CacheName]['cache'] = array_merge(Gdn_LibraryMap::$_Caches[$CacheName]['cache'], $CacheContents); + Gdn_LibraryMap::SaveCache($CacheName); + } + + /** + * Clear the contents of the supplied cache, and remove it from disk + * + * @param string $CacheName name of cache library + * @return void + */ + public static function ClearCache($CacheName) { + if (!array_key_exists($CacheName,Gdn_LibraryMap::$_Caches)) + return Gdn_LibraryMap::PrepareCache($CacheName); + + Gdn_LibraryMap::$_Caches[$CacheName]['cache'] = array(); + @unlink(PATH_CACHE.DS.Gdn_LibraryMap::$_Caches[$CacheName]['ondisk']); + } + + /** + * Detect whether the cache has any items in it + * + * @param string $CacheName name of cache library + * @return bool ready state of cache + */ + public static function CacheReady($CacheName) { + if (!array_key_exists($CacheName,Gdn_LibraryMap::$_Caches)) + return FALSE; + + if (!sizeof(Gdn_LibraryMap::$_Caches[$CacheName]['cache'])) + return FALSE; + + return TRUE; + } + /** + * Store the provided resource in the appropriate (named) cache + * + * @param string $CacheName name of cache library + * @param string $CacheKey name of cache entry + * @param mixed $CacheContents contents of cache entry + * @param bool $CacheWrite optional, whether or not to perform a disk write after this set. default yes + * @return mixed cache contents + */ + public static function Cache($CacheName, $CacheKey, $CacheContents, $CacheWrite=TRUE) { + if (!array_key_exists($CacheName,Gdn_LibraryMap::$_Caches)) + return FALSE; + + // Set and save cache data to memory and disk + Gdn_LibraryMap::$_Caches[$CacheName]['cache'][$CacheKey] = $CacheContents; + if ($CacheWrite === TRUE) + Gdn_LibraryMap::SaveCache($CacheName); + + return $CacheContents; + } + + public static function SafeCache($CacheName, $CacheKey, $CacheContents, $CacheWrite=TRUE) { + return Gdn_LibraryMap::Cache($CacheName, str_replace('.','__',$CacheKey), $CacheContents, $CacheWrite); + } + + /** + * Append the provided resource in the appropriate (named) cache under the named cache key. + * If the entry is not already an array, convert it to one... then append the new data. + * + * @param string $CacheName name of cache library + * @param string $CacheKey name of cache entry + * @param mixed $CacheContents contents of cache entry + * @param bool $CacheWrite optional, whether or not to perform a disk write after this set. default yes + * @return array cache contents + */ + public static function CacheArray($CacheName, $CacheKey, $CacheContents, $CacheWrite=TRUE) { + $ExistingCacheData = Gdn_LibraryMap::GetCache($CacheName, $CacheKey); + + if ($ExistingCacheData === NULL) + $ExistingCacheData = array(); + + if (!is_array($ExistingCacheData)) + $ExistingCacheData = array($ExistingCacheData); + + $ExistingCacheData[] = $CacheContents; + + // Save cache data to memory + return Gdn_LibraryMap::Cache($CacheName, $CacheKey, $ExistingCacheData, $CacheWrite); + } + + /** + * Retrieve an item from the cache + * + * @param string $CacheName name of cache library + * @param string $CacheKey name of cache entry + * @return mixed cache entry or null on failure + */ + public static function GetCache($CacheName, $CacheKey) { + if (array_key_exists($CacheKey,Gdn_LibraryMap::$_Caches[$CacheName]['cache'])) + return Gdn_LibraryMap::$_Caches[$CacheName]['cache'][$CacheKey]; + + return NULL; + } + + /** + * Save the provided library's data to the on disk location. + * + * @param string $CacheName name of cache library + * @return void + */ + public static function SaveCache($CacheName) { + if (!array_key_exists($CacheName,Gdn_LibraryMap::$_Caches)) + return FALSE; + + $FileName = Gdn_LibraryMap::$_Caches[$CacheName]['ondisk']; + + $CacheContents = " "; + + if (is_array($Cache)) + $CacheStr .= "array(\n"; + + $First = TRUE; + foreach ($Cache as $CacheKey => $CacheValue) { + if (!$First) { $CacheStr .= ",\n"; } + if ($First) { $First = FALSE; } + + if (!is_array($CacheValue)) { + $CacheStr .= str_repeat(' ',$FormatIndentLevel+1); + if (!is_numeric($CacheKey)) + $CacheStr .= "'{$CacheKey}' => "; + $CacheStr .= "'{$CacheValue}'"; + } + else { + Gdn_LibraryMap::RecurseArrayStr($CacheKey, $CacheValue, $CacheStr, $FormatIndentLevel+1); + } + } + if (is_array($Cache)) + $CacheStr .= "\n".str_repeat(' ',$FormatIndentLevel).")"; + } + +} \ No newline at end of file