<?php
/*****************************************************************************
 * Cache.php
 * Contains key class Cache.
 *
 * Kabuki CMS (C) 2013-2015, Aaron van Geffen
 *****************************************************************************/

class Cache
{
	public static $hits = 0;
	public static $misses = 0;
	public static $puts = 0;
	public static $removals = 0;

	public static function put($key, $value, $ttl = 3600)
	{
		// If the cache is unavailable, don't bother.
		if (!CACHE_ENABLED || !function_exists('apcu_store'))
			return false;

		// Keep track of the amount of cache puts.
		self::$puts++;

		// Store the data in serialized form.
		return apcu_store(CACHE_KEY_PREFIX . $key, serialize($value), $ttl);
	}

	// Get some data from the cache.
	public static function get($key)
	{
		// If the cache is unavailable, don't bother.
		if (!CACHE_ENABLED || !function_exists('apcu_fetch'))
			return false;

		// Try to fetch it!
		$value = apcu_fetch(CACHE_KEY_PREFIX . $key);

		// Were we successful?
		if (!empty($value))
		{
			self::$hits++;
			return unserialize($value);
		}
		// Otherwise, it's a miss.
		else
		{
			self::$misses++;
			return null;
		}
	}

	public static function remove($key)
	{
		if (!CACHE_ENABLED || !function_exists('apcu_delete'))
			return false;

		self::$removals++;
		return apcu_delete(CACHE_KEY_PREFIX . $key);
	}
}