<?php
//This file will be placed to /wp-content/
	if (defined('WP_PLUGIN_DIR')) {
		$result = @include_once(WP_PLUGIN_DIR . '/sj-object-cache/lib/class.CacheFactory.php');
	}
	else {
		$result = @include_once(WP_CONTENT_DIR . '/plugins/sj-object-cache/lib/class.CacheFactory.php');
	}

	if (false === $result) {
		unset($result);
		require_once(ABSPATH . WPINC . '/cache.php');
	}
	else {
		unset($result);

		SjCacheFactory::registerEngine('basecache',      'SjBaseCache',         'BaseCache',         'array_merge',           0, 'BaseCache');
		SjCacheFactory::registerEngine('apc',            'SjApcCache',          'ApcCache',          'apc_store',             1, 'APC');
		SjCacheFactory::registerEngine('eaccelerator',   'SjEAcceleratorCache', 'eAcceleratorCache', 'eaccelerator_put',      1, 'eAccelerator');
		SjCacheFactory::registerEngine('filecache',      'SjFileCache',         'FileCache',         'file_get_contents',     1, 'FileCache');
		SjCacheFactory::registerEngine('filegroupcache', 'SjFileGroupCache',    'FileGroupCache',    'file_get_contents',     1, 'FileGroupCache');
		SjCacheFactory::registerEngine('xcache',         'SjxCache',            'xCache',            'xcache_set',            1, 'xCache');
		SjCacheFactory::registerEngine('zend_disk',      'SjZendDiskCache',     'ZendDiskCache',     'zend_disk_cache_store', 1, 'ZendDisk');
		SjCacheFactory::registerEngine('zend_shm',       'SjZendShmCache',      'ZendShmCache',      'zend_shm_cache_store',  1, 'ZendShm');

		$GLOBALS['__sjfc_options'] = 'a:6:{s:7:"enabled";i:1;s:6:"maxttl";i:3600;s:7:"persist";i:1;s:7:"options";a:0:{}s:13:"nonpersistent";s:0:"";s:6:"engine";s:9:"basecache";}';

		/**
		 * wp_cache_add() - Adds data to the cache, if the cache key doesn't aleady exist
		 *
		 * @param int|string $key The cache ID to use for retrieval later
		 * @param mixed $data The data to add to the cache store
		 * @param string $flag The group to add the cache to
		 * @param int $expire When the cache data should be expired
		 * @return unknown
		 */
		function wp_cache_add($key, $data, $flag = '', $expire = 0)
		{
			global $wp_object_cache;
			if (empty($flag)) { $flag = 'default'; }
			return $wp_object_cache->add($key, $data, $flag, $expire);
		}

		/**
		 * wp_cache_close() - Closes the cache
		 *
		 * @return bool Always returns True
		 */
		function wp_cache_close()
		{
			global $wp_object_cache;
			$wp_object_cache->close();
			return true;
		}

		/**
		 * wp_cache_delete() - Removes the cache contents matching ID and flag
		 *
		 * @param int|string $id What the contents in the cache are called
		 * @param string $flag Where the cache contents are grouped
		 * @return bool True on successful removal, false on failure
		 */
		function wp_cache_delete($id, $flag = '')
		{
			global $wp_object_cache;
			if (empty($flag)) { $flag = 'default'; }
			return $wp_object_cache->delete($id, $flag);
		}

		/**
		 * wp_cache_flush() - Removes all cache items
		 *
		 * @return bool Always returns true
		 */
		function wp_cache_flush()
		{
			global $wp_object_cache;
			$wp_object_cache->flush();
			return true;
		}

		/**
		 * wp_cache_get() - Retrieves the cache contents from the cache by ID and flag
		 *
		 * @param int|string $id What the contents in the cache are called
		 * @param string $flag Where the cache contents are grouped
		 * @return bool|mixed False on failure to retrieve contents or the cache contents on success
		 */
		function wp_cache_get($id, $flag = '')
		{
			global $wp_object_cache;
			if (empty($flag)) { $flag = 'default'; }
			return $wp_object_cache->get($id, $flag);
		}

		function wp_cache_init()
		{
			global $__sjfc_options;
			$options = unserialize($__sjfc_options);

			if (!isset($_SERVER['HTTP_HOST'])) {
				$_SERVER['HTTP_HOST'] = null;
				$options['persist'] = false;
			}

			$GLOBALS['wp_object_cache'] = SjCacheFactory::get($options);

			if (!empty($options['nonpersistent'])) {
				$np = explode(',', $options['nonpersistent']);
				if (!empty($np)) {
					wp_cache_add_non_persistent_groups($np);
				}
			}
		}

		/**
		 * Reset internal cache keys and structures. If the cache backend uses global blog or site IDs as part of its cache keys,
		 * this function instructs the backend to reset those keys and perform any cleanup since blog or site IDs have changed since cache init.
		 */
		function wp_cache_reset()
		{
			global $wp_object_cache;
			$wp_object_cache->reset();
		}

		/**
		 * wp_cache_replace() - Replaces the contents of the cache with new data
		 *
		 * @param int|string $id What to call the contents in the cache
		 * @param mixed $data The contents to store in the cache
		 * @param string $flag Where to group the cache contents
		 * @param int $expire When to expire the cache contents
		 * @return bool False if cache ID and group already exists, true on success
		 */
		function wp_cache_replace($key, $data, $flag = '', $expire = 0)
		{
			global $wp_object_cache;
			if (empty($flag)) { $flag = 'default'; }
			return $wp_object_cache->replace($key, $data, $flag, $expire);
		}

		/**
		 * wp_cache_set() - Saves the data to the cache
		 *
		 * @param int|string $id What to call the contents in the cache
		 * @param mixed $data The contents to store in the cache
		 * @param string $flag Where to group the cache contents
		 * @param int $expire When to expire the cache contents
		 * @return bool False if cache ID and group already exists, true on success
		 */
		function wp_cache_set($key, $data, $flag = '', $expire = 0)
		{
			global $wp_object_cache;
			if (empty($flag)) { $flag = 'default'; }
			return $wp_object_cache->set($key, $data, $flag, $expire);
		}

		/**
		 * Adds a group or set of groups to the list of global groups.
		 *
		 * @param string|array $groups A group or an array of groups to add
		 */
		function wp_cache_add_global_groups($groups)
		{
			global $wp_object_cache;
			if (!is_array($groups)) {
				$groups = array($groups);
			}

			$wp_object_cache->addGlobalGroups($groups);
		}

		/**
		 * Adds a group or set of groups to the list of non-persistent groups.
		 *
		 * @param string|array $groups A group or an array of groups to add
		 */
		function wp_cache_add_non_persistent_groups($groups)
		{
			global $wp_object_cache;
			if (!is_array($groups)) {
				$groups = array($groups);
			}

			$wp_object_cache->addNonPersistentGroups($groups);
		}
	}

?>