Cacher: Add XCache support (partial)
authorDan Fuhry <dan@enanocms.org>
Mon, 19 Nov 2012 11:32:41 -0500
changeset 1365 a52cac1b190d
parent 1355 12c23b83c79d
child 1366 23f89f989922
Cacher: Add XCache support (partial)
includes/cache.php
includes/common.php
includes/common_cli.php
--- a/includes/cache.php	Fri Dec 02 01:15:55 2011 -0500
+++ b/includes/cache.php	Mon Nov 19 11:32:41 2012 -0500
@@ -21,6 +21,59 @@
 
 class CacheManager
 {
+	public static function factory($backend = 'auto')
+	{
+		if ( function_exists('xcache_set') )
+			return new CacheManager_XCache();
+		else
+			return new CacheManager_File();
+	}
+}
+
+class CacheManager_XCache
+{
+	private $prefix;
+	
+	/**
+	 * Constructor. Merely generates the prefix that will be prepended to cache objects.
+	 */
+	
+	public function __construct()
+	{
+		global $crypto_key;
+		$this->prefix = 'enano' . substr(md5($crypto_key), 0, 8) . '_';
+	}
+	
+	/**
+	 * @see CacheManager_File::fetch()
+	 */
+	
+	public function fetch($cache_id)
+	{
+		return xcache_isset($this->prefix . $cache_id) ? xcache_get($this->prefix . $cache_id) : false;
+	}
+	
+	/**
+	 * @see CacheManager_File::store()
+	 */
+	
+	public function store($cache_id, $data, $ttl = 20)
+	{
+		return xcache_set($this->prefix . $cache_id, $data, $ttl * 60);
+	}
+	
+	/**
+	 * @see CacheManager_File::purge()
+	 */
+	
+	public function purge($cache_id)
+	{
+		return xcache_unset($this->prefix . $cache_id);
+	}
+}
+
+class CacheManager_File
+{
 	/**
  	* Fetch a cached piece of data.
  	* @param string Cache ID. The timestamp is checked automatically.
--- a/includes/common.php	Fri Dec 02 01:15:55 2011 -0500
+++ b/includes/common.php	Mon Nov 19 11:32:41 2012 -0500
@@ -355,7 +355,7 @@
 profiler_log('Ran checks');
 
 // Init cache
-$cache = new CacheManager();
+$cache = CacheManager::factory();
 
 // Load plugin manager
 $plugins = new pluginLoader();
--- a/includes/common_cli.php	Fri Dec 02 01:15:55 2011 -0500
+++ b/includes/common_cli.php	Mon Nov 19 11:32:41 2012 -0500
@@ -125,7 +125,7 @@
 profiler_log('Ran checks');
 
 // Init cache
-$cache = new CacheManager();
+$cache = CacheManager::factory();
 
 // Load plugin manager
 $plugins = new pluginLoader();