<?php

/**
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package fusionLib
 * @copyright Copyright (c) 2007 - 2013 fusionLib. All rights reserved.
 * @link http://fusionlib.com
 */

/**
 * flCache Class
 *
 * The base class for all cache classes.
 *
 * @package fusionLib
 */
abstract class flCache {

	/**
	 * The default cache lifetime.
	 *
	 * @var int
	 */
	protected $lifetime;

	/**
	 * Prefix for cache keys.
	 *
	 * @var string
	 */
	protected $prefix;

	/**
	 * Construct the object and save the default lifetime.
	 *
	 * @param int $lifetime The default lifetime in seconds, 0 to never expire.
	 * @param string $prefix Prefix for cache keys.
	 */
	function __construct($lifetime = 3600, $prefix = '') {
		$this->lifetime = $lifetime;
		$this->prefix = $prefix;
	}

	/**
	 * Store an element in the cache.
	 *
	 * @param string $id The id to store the element as.
	 * @param mixed $data The data to store.
	 * @param int $lifetime The lifetime of the object in seconds, null to use the default or 0 to never expire.
	 * @return bool true if the data is cached or false if it could not be cached.
	 */
	abstract function store($id, $data, $lifetime = null);

	/**
	 * Get the element from the cache.
	 *
	 * @param string $id The id of the element to get.
	 * @return mixed false if the element is not cached or has expired; else the cached data.
	 */
	abstract function get($id);

	/**
	 * Remove the given element from the cache.
	 *
	 * @param string $id The id of the element to remove.
	 */
	abstract function remove($id);

	/**
	 * Flush the cache.
	 */
	abstract function flush();

	/**
	 * Get the UNIX timestamp according to the server.
	 *
	 * @return int The timestamp.
	 */
	function getServerTime() {
		return time();
	}
}
