<?php

	class SjBaseCache
	{
		protected $cache = array();
		protected $dirty = array();

		protected $enabled = true;
		protected $persist = true;
		protected $maxttl  = 3600;

		protected $np_groups = array();
		protected $global_groups = array();

		/**
		 * @desc To stay compatible with SimpleTags
		 * @access readonly
		 */
		public $cache_enabled = true;

		public static function instance($data, $enabled = true, $persist = true, $maxttl = 3600)
		{
			static $self = false;

			if (!$self) {
				$self = new SjBaseCache($data, $enabled, $persist, $maxttl);
			}

			return $self;
		}

		protected function __construct($data = array(), $enabled = true, $persist = true, $maxttl = 3600)
		{
			$this->enabled = $enabled;
			$this->persist = $persist && $enabled;
			$this->maxttl  = $maxttl;

			if (!$this->persist) {
				global $_wp_using_ext_object_cache;
				$_wp_using_ext_object_cache = false;
				$this->cache_enabled = false;
			}
		}

		public function reset()
		{
			$this->close();
			if ($this->cache) {
				foreach ($this->cache as $group => &$x) {
					if (!in_array($group, $this->global_groups)) {
						unset($this->cache[$group]);
					}
				}

				unset($x);
			}
		}

		public function add($key, $data, $group, $ttl = 0)
		{
			if (!isset($this->cache[$group][$key])) {
				return $this->set($key, $data, $group, $ttl);
			}

			return false;
		}

		public function close()
		{
		}

		public function delete($key, $group)
		{
			unset($this->cache[$group][$key]);
			return true;
		}

		public function flush()
		{
			$this->cache = array();
			$this->dirty = array();
		}

		public function set($key, $data, $group, $ttl = 0)
		{
			if (!$this->enabled) {
				return false;
			}

			if (!$ttl && $this->maxttl) {
				$ttl = $this->maxttl;
			}

			$this->dirty[$group][$key] = $ttl;
			$this->fast_set($key, $data, $group);
			return true;
		}

		protected function fast_set($key, $data, $group)
		{
			if (is_object($data)) {
				$data = clone($data);
			}

			$this->cache[$group][$key] = $data;
		}

		public function get($key, $group, $ttl = 3600)
		{
			if (!$this->enabled) {
				return false;
			}

			return $this->fast_get($key, $group);
		}

		protected function fast_get($key, $group)
		{
			if (isset($this->cache[$group][$key])) {
				$result = $this->cache[$group][$key];
				return is_object($result) ? clone($result) : $result;
			}

			return false;
		}

		protected function has_group($group)
		{
			return isset($this->cache[$group]);
		}

		public function replace($key, $data, $group, $ttl = 0)
		{
			if ($this->enabled && isset($this->cache[$group][$key])) {
				return $this->set($key, $data, $group, $ttl);
			}

			return false;
		}

		public function addNonPersistentGroups(array $groups)
		{
			$this->np_groups = array_merge(
				$this->np_groups,
				$groups
			);
		}

		public function addGlobalGroups(array $groups)
		{
			$this->global_groups = array_merge(
				$this->global_groups,
				$groups
			);

			$this->global_groups = array_unique($this->global_groups);
		}

		public function clearGlobalGroups()
		{
			$this->global_groups = array();
		}

		public function clearNonPersistentGroups()
		{
			$this->np_groups = array();
		}
	}
?>