Return to Snippet

Revision: 51047
at December 1, 2011 00:58 by TimoZachi


Updated Code
<?php
class SessCacher
{
	//constant with self-documented names that holds this class error codes num
	const MAX_USES_TOO_LOW = 9000;
	
	const INFINITE = -1;
	
	/**
	 * If false (Default), this class will apply md5 php function to compress the cache key. 
	 */
	public static $useRawKey = false;
	
	private static $_sessKey = 'SessCacher';
	
	//Constructor private, no instantiation. Use this class staticaly
	private function __construct()
	{}
	
	/**
	 * This function is used to cache any data through php's SESSION. 
	 * To reuse the cached data use see function GetCache.
	 * 
	 * $datakey: A key (any string) to uniquely identify the data you are caching. 
	 * You will need this key to retrieve the cached data later.
	 * 
	 * $data: The data you want to cache (any data).
	 * 
	 * $maxCacheUses: This will limit how many times you want to retreive the data.
	 * The default value is SessCacher::INFINITE times, wich means the cached data will only be 
	 * deleted after the session ends.
	 * 
	 * $ifNotExists: If you are trying to cache a data that is already cached, the 
	 * data you are trying to chache will not override the previous data if 
	 * $ifNotExists = true (Default value).
	 */
	public static function SetCache($datakey, $data, $maxCacheUses = self::INFINITE, $ifNotExists = true)
	{
		if($maxCacheUses != self::INFINITE && $maxCacheUses < 1)
		{
			throw new Exception('param $maxCacheUses: ' . $maxCacheUses . 
								' cannot be lower than 1. Use SessCacher::DelCache() to delete cache.',
								self::MAX_USES_TOO_LOW);
		}
		self::CheckKey();
		
		$real_key = self::$useRawKey ? $datakey : md5($datakey);
		if(!$ifNotExists || !array_key_exists($real_key, $_SESSION[self::$_sessKey]))
		{
			$_SESSION[self::$_sessKey][$real_key] = array($data, 0, $maxCacheUses);
		}
	}
	/**
	 * Retrieves a previous cached data using SetCache. If a cache doesn't exists, 
	 * this function retrieves null.
	 * 
	 * $datakey: A key (any string) that uniquely identifyes the data you cached. 
	 * This has to be the same string that you used on the function SetCache.
	 */
	public static function GetCache($datakey)
	{
		$return = null;
		self::CheckKey();
		
		$real_key = self::$useRawKey ? $datakey : md5($datakey);
		if(!array_key_exists($real_key, $_SESSION[self::$_sessKey])) return $return;
		
		$data = $_SESSION[self::$_sessKey][$real_key];
		if($data[2] == self::INFINITE) $return = $data[0];
		else
		{
			$return = $data[0];
			$_SESSION[self::$_sessKey][$real_key][1] = $data[1] = $data[1] + 1;
			if($data[1] >= $data[2]) unset($_SESSION[self::$_sessKey][$real_key]);
		}
		return $return;
	}
	/**
	 * Deletes a cached data if exists (if it was cached using SetCache).
	 * 
	 * $datakey: A key (any string) that uniquely identifyes the data you cached. 
	 * This has to be the same string that you used on the function SetCache.
	 */
	public static function DelCache($datakey)
	{
		self::CheckKey();
		unset($_SESSION[self::$_sessKey][self::$useRawKey ? $datakey : md5($datakey)]);
	}
	/**
	 * Deletes all cached data using this class.
	 */
	public static function DelAllCache()
	{
		self::CheckKey();
		$_SESSION[self::$_sessKey] = array();
	}
	/**
	 * Returns the $_SESSION key that its beeing used to cache the data, 
	 * that by default will be 'SessCacher' (unless you changed it).
	 */
	public static function GetSessKey()
	{
		return self::$_sessKey;
	}
	/**
	 * Sets the $_SESSION key that its beeing used to cache the data.
	 * No cached data will be lost by changing the key.
	 * 
	 * $newKey: The new $_SESSION key
	 */
	public static function SetSessKey($newKey)
	{
		$newKey = (string)$newKey;
		if($newKey == '' || self::$_sessKey == $newKey) return;
		
		self::CheckKey();
		
		$_SESSION[$newKey] = $_SESSION[self::$_sessKey];
		unset($_SESSION[self::$_sessKey]);
		self::$_sessKey = $newKey;
	}
	////////////////////////////////////////////////////////////////////
	//PRIVATES
	////////////////////////////////////////////////////////////////////
	private static function CheckKey()
	{
		if(!isset($_SESSION)) session_start();
		if(!array_key_exists(self::$_sessKey, $_SESSION)) $_SESSION[self::$_sessKey] = array();
	}
}

//How to use
$init_time = microtime(true);//Init measuring the total time taken to get the results

$in_cache = true;

//COUNT queries might take a long time
$query = "SELECT COUNT(*) FROM huge_table";

//Here i am using the query as a unique key(string) to identify the cached data
$results = SessCacher::GetCache($query);
if($results === null)
{
	$in_cache = false;
	
	//If the SessCacher returned null it means that 
	//the result is not cached an we have to retreive
	//it from the database
	
	//$mysqli_link: the mysqli database link
	$mysqli_result = mysqli_query($mysqli_link, $query);
	
	//We will put the fetched results into an array to cache them.
	$results = array();
	while($row = mysqli_fetch_assoc($mysqli_result))
	{
		array_push($results, $row);
	}
	
	//Here i cache the results using the query as a key
	//and specifing that i want to reuse the cache 
	//no more than 5 times
	SessCacher::SetCache($query, $results, 5);
}

//The total time taken to retreive the results from cache or database in milliseconds
$total_time = (string)round((microtime(true) - $init_time) * 1000, 2);


//Echoing for testing purposes:

//Total time taken to get results
echo 'total time taken ' . 
	 ($in_cache ? '(results from cache): ' : '(results from database): ') . 
	 $total_time . ' milliseconds';
echo '<br />';
echo '<pre>';
print_r($results);
echo '</pre>';
?>

Revision: 51046
at November 30, 2011 23:43 by TimoZachi


Updated Code
<?php
class SessCacher
{
	//constant with self-documented names that holds this class error codes num
	const MAX_USES_TOO_LOW = 9000;
	
	const INFINITE = -1;
	
	/**
	 * If false (Default), this class will apply md5 php function to compress the cache key. 
	 */
	public static $useRawKey = false;
	
	private static $_sessKey = 'SessCacher';
	
	//Constructor private, no instantiation. Use this class staticaly
	private function __construct()
	{}
	
	/**
	 * This function is used to cache any data through php's SESSION. 
	 * To reuse the cached data use see function GetCache.
	 * 
	 * $datakey: A key (any string) to uniquely identify the data you are caching. 
	 * You will need this key to retrieve the cached data later.
	 * 
	 * $data: The data you want to cache (any data).
	 * 
	 * $maxCacheUses: This will limit how many times you want to retreive the data.
	 * The default value is SessCacher::INFINITE times, wich means the cached data will only be 
	 * deleted after the session ends.
	 * 
	 * $ifNotExists: If you are trying to cache a data that is already cached, the 
	 * data you are trying to chache will not override the previous data if 
	 * $ifNotExists = true (Default value).
	 */
	public static function SetCache($datakey, $data, $maxCacheUses = self::INFINITE, $ifNotExists = true)
	{
		if($maxCacheUses != self::INFINITE && $maxCacheUses < 1)
		{
			throw new Exception('param $maxCacheUses: ' . $maxCacheUses . 
								' cannot be lower than 1. Use SessCacher::DelCache() to delete cache.',
								self::MAX_USES_TOO_LOW);
		}
		self::CheckKey();
		
		$real_key = self::$useRawKey ? $datakey : md5($datakey);
		if(!$ifNotExists || !array_key_exists($real_key, $_SESSION[self::$_sessKey]))
		{
			$_SESSION[self::$_sessKey][$real_key] = array($data, 0, $maxCacheUses);
		}
	}
	/**
	 * Retrieves a previous cached data using SetCache. If a cache doesn't exists, 
	 * this function retrieves null.
	 * 
	 * $datakey: A key (any string) that uniquely identifyes the data you cached. 
	 * This has to be the same string that you used on the function SetCache.
	 */
	public static function GetCache($datakey)
	{
		$return = null;
		self::CheckKey();
		
		$real_key = self::$useRawKey ? $datakey : md5($datakey);
		if(!array_key_exists($real_key, $_SESSION[self::$_sessKey])) return $return;
		
		$data = $_SESSION[self::$_sessKey][$real_key];
		if($data[1] == self::INFINITE) $return = $data[0];
		else
		{
			$return = $data[0];
			$_SESSION[self::$_sessKey][$real_key][1] = $data[1] = $data[1] + 1;
			if($data[1] >= $data[2]) unset($_SESSION[self::$_sessKey][$real_key]);
		}
		return $return;
	}
	/**
	 * Deletes a cached data if exists (if it was cached using SetCache).
	 * 
	 * $datakey: A key (any string) that uniquely identifyes the data you cached. 
	 * This has to be the same string that you used on the function SetCache.
	 */
	public static function DelCache($datakey)
	{
		self::CheckKey();
		unset($_SESSION[self::$_sessKey][self::$useRawKey ? $datakey : md5($datakey)]);
	}
	/**
	 * Deletes all cached data using this class.
	 */
	public static function DelAllCache()
	{
		self::CheckKey();
		$_SESSION[self::$_sessKey] = array();
	}
	/**
	 * Returns the $_SESSION key that its beeing used to cache the data, 
	 * that by default will be 'SessCacher' (unless you changed it).
	 */
	public static function GetSessKey()
	{
		return self::$_sessKey;
	}
	/**
	 * Sets the $_SESSION key that its beeing used to cache the data.
	 * No cached data will be lost by changing the key.
	 * 
	 * $newKey: The new $_SESSION key
	 */
	public static function SetSessKey($newKey)
	{
		$newKey = (string)$newKey;
		if($newKey == '' || self::$_sessKey == $newKey) return;
		
		self::CheckKey();
		
		$_SESSION[$newKey] = $_SESSION[self::$_sessKey];
		unset($_SESSION[self::$_sessKey]);
		self::$_sessKey = $newKey;
	}
	////////////////////////////////////////////////////////////////////
	//PRIVATES
	////////////////////////////////////////////////////////////////////
	private static function CheckKey()
	{
		if(!isset($_SESSION)) session_start();
		if(!array_key_exists(self::$_sessKey, $_SESSION)) $_SESSION[self::$_sessKey] = array();
	}
}

//How to use
$init_time = microtime(true);//Init measuring the total time taken to get the results

$in_cache = true;

//COUNT queries might take a long time
$query = "SELECT COUNT(*) FROM huge_table";

//Here i am using the query as a unique key(string) to identify the cached data
$results = SessCacher::GetCache($query);
if($results === null)
{
	$in_cache = false;
	
	//If the SessCacher returned null it means that 
	//the result is not cached an we have to retreive
	//it from the database
	
	//$mysqli_link: the mysqli database link
	$mysqli_result = mysqli_query($mysqli_link, $query);
	
	//We will put the fetched results into an array to cache them.
	$results = array();
	while($row = mysqli_fetch_assoc($mysqli_result))
	{
		array_push($results, $row);
	}
	
	//Here i cache the results using the query as a key
	//and specifing that i want to reuse the cache 
	//no more than 5 times
	SessCacher::SetCache($query, $results, 5);
}

//The total time taken to retreive the results from cache or database in milliseconds
$total_time = (string)round((microtime(true) - $init_time) * 1000, 2);


//Echoing for testing purposes:

//Total time taken to get results
echo 'total time taken ' . 
	 ($in_cache ? '(results from cache): ' : '(results from database): ') . 
	 $total_time . ' milliseconds';
echo '<br />';
echo '<pre>';
print_r($results);
echo '</pre>';
?>

Revision: 51045
at September 21, 2011 03:23 by TimoZachi


Updated Code
<?php
class SessCacher
{
	//constant with self-documented names that holds this class error codes num
	const MAX_USES_TOO_LOW = 9000;
	
	const INFINITE = -1;
	
	/**
	 * If false (Default), this class will apply md5 php function to compress the cache key. 
	 */
	public static $useRawKey = false;
	
	private static $_sessKey = 'SessCacher';
	
	//Constructor private, no instantiation. Use this class staticaly
	private function __construct()
	{}
	
	/**
	 * This function is used to cache any data through php's SESSION. 
	 * To reuse the cached data use see function GetCache.
	 * 
	 * $datakey: A key (any string) to uniquely identify the data you are caching. 
	 * You will need this key to retrieve the cached data later.
	 * 
	 * $data: The data you want to cache (any data).
	 * 
	 * $maxCacheUses: This will limit how many times you want to retreive the data.
	 * The default value is SessCacher::INFINITE times, wich means the cached data will only be 
	 * deleted after the session ends.
	 * 
	 * $ifNotExists: If you are trying to cache a data that is already cached, the 
	 * data you are trying to chache will not override the previous data if 
	 * $ifNotExists = true (Default value).
	 */
	public static function SetCache($datakey, $data, $maxCacheUses = self::INFINITE, $ifNotExists = true)
	{
		if($maxCacheUses != self::INFINITE && $maxCacheUses < 1)
		{
			throw new Exception('param $maxCacheUses: ' . $maxCacheUses . 
								' cannot be lower than 1. Use SessCacher::DelCache() to delete cache.',
								self::MAX_USES_TOO_LOW);
		}
		self::CheckKey();
		
		$real_key = self::$useRawKey ? $datakey : md5($datakey);
		if(!$ifNotExists || !array_key_exists($real_key, $_SESSION[self::$_sessKey]))
		{
			$_SESSION[self::$_sessKey][$real_key] = array($data, 0, $maxCacheUses);
		}
	}
	/**
	 * Retrieves a previous cached data using SetCache. If a cache doesn't exists, 
	 * this function retrieves null.
	 * 
	 * $datakey: A key (any string) that uniquely identifyes the data you cached. 
	 * This has to be the same string that you used on the function SetCache.
	 */
	public static function GetCache($datakey)
	{
		$return = null;
		self::CheckKey();
		
		$real_key = self::$useRawKey ? $datakey : md5($datakey);
		if(!array_key_exists($real_key, $_SESSION[self::$_sessKey])) return $return;
		
		$data = $_SESSION[self::$_sessKey][$real_key];
		if($data[1] == self::INFINITE) $return = $data[0];
		else
		{
			$return = $data[0];
			$_SESSION[self::$_sessKey][$real_key][1] = $data[1] = $data[1] + 1;
			if($data[1] >= $data[2]) unset($_SESSION[self::$_sessKey][$real_key]);
		}
		return $return;
	}
	/**
	 * Deletes a cached data if exists (if it was cached using SetCache).
	 * 
	 * $datakey: A key (any string) that uniquely identifyes the data you cached. 
	 * This has to be the same string that you used on the function SetCache.
	 */
	public static function DelCache($datakey)
	{
		self::CheckKey();
		unset($_SESSION[self::$_sessKey][self::$useRawKey ? $datakey : md5($datakey)]);
	}
	/**
	 * Deletes all cached data using this class.
	 */
	public static function DelAllCache()
	{
		self::CheckKey();
		$_SESSION[self::$_sessKey] = array();
	}
	/**
	 * Returns the $_SESSION key that its beeing used to cache the data, 
	 * that by default will be 'SessCacher' (unless you changed it).
	 */
	public static function GetSessKey()
	{
		return self::$_sessKey;
	}
	/**
	 * Sets the $_SESSION key that its beeing used to cache the data.
	 * No cached data will be lost by changing the key.
	 * 
	 * $newKey: The new $_SESSION key
	 */
	public static function SetSessKey($newKey)
	{
		$newKey = (string)$newKey;
		if($newKey == '' || self::$_sessKey == $newKey) return;
		
		self::CheckKey();
		
		$_SESSION[$newKey] = $_SESSION[self::$_sessKey];
		unset($_SESSION[self::$_sessKey]);
		self::$_sessKey = $newKey;
	}
	////////////////////////////////////////////////////////////////////
	//PRIVATES
	////////////////////////////////////////////////////////////////////
	private static function CheckKey()
	{
		if(!isset($_SESSION)) session_start();
		if(!array_key_exists(self::$_sessKey, $_SESSION)) $_SESSION[self::$_sessKey] = array();
	}
}

//How to use
$init_time = microtime(true);//Init measuring the total time taken to get the results

$in_cache = true;

//COUNT queries might take a long time
$query = "SELECT COUNT(*) FROM huge_table";

//Here i am using the query as a unique key(string) to identify the cached data
$results = SessCacher::GetCache($query);
if($results == null)
{
	$in_cache = false;
	
	//If the SessCacher returned null it means that 
	//the result is not cached an we have to retreive
	//it from the database
	
	//$mysqli_link: the mysqli database link
	$mysqli_result = mysqli_query($mysqli_link, $query);
	
	//We will put the fetched results into an array to cache them.
	$results = array();
	while($row = mysqli_fetch_assoc($mysqli_result))
	{
		array_push($results, $row);
	}
	
	//Here i cache the results using the query as a key
	//and specifing that i want to reuse the cache 
	//no more than 5 times
	SessCacher::SetCache($query, $results, 5);
}

//The total time taken to retreive the results from cache or database in milliseconds
$total_time = (string)round((microtime(true) - $init_time) * 1000, 2);


//Echoing for testing purposes:

//Total time taken to get results
echo 'total time taken ' . 
	 ($in_cache ? '(results from cache): ' : '(results from database): ') . 
	 $total_time . ' milliseconds';
echo '<br />';
echo '<pre>';
print_r($results);
echo '</pre>';
?>

Revision: 51044
at September 13, 2011 05:52 by TimoZachi


Updated Code
<?php
class SessCacher
{
	//constant with self-documented names that holds this class error codes num
	const MAX_USES_TOO_LOW = 9000;
	
	const INFINITE = -1;
	
	/**
	 * If false (Default), this class will apply md5 php function to compress the cache key. 
	 */
	public static $useRawKey = false;
	
	private static $_sessKey = 'SessCacher';
	
	//Constructor private, no instantiation. Use this class staticaly
	private function __construct()
	{}
	
	/**
	 * This function is used to cache any data through php's SESSION. 
	 * To reuse the cached data use see function GetCache.
	 * 
	 * $datakey: A key (any string) to uniquely identify the data you are caching. 
	 * You will need this key to retrieve the cached data later.
	 * 
	 * $data: The data you want to cache (any data).
	 * 
	 * $maxCacheUses: This will limit how many times you want to retreive the data.
	 * The default value is SessCacher::INFINITE times, wich means the cached data will only be 
	 * deleted after the session ends.
	 * 
	 * $ifNotExists: If you are trying to cache a data that is already cached, the 
	 * data you are trying to chache will not override the previous data if 
	 * $ifNotExists = true (Default value).
	 */
	public static function SetCache($datakey, $data, $maxCacheUses = self::INFINITE, $ifNotExists = true)
	{
		if($maxCacheUses != self::INFINITE && $maxCacheUses < 1)
		{
			throw new Exception('param $maxCacheUses: ' . $maxCacheUses . 
								' cannot be lower than 1. Use SessCacher::DelCache() to delete cache.',
								self::MAX_USES_TOO_LOW);
		}
		self::CheckKey();
		
		$real_key = self::$useRawKey ? $datakey : md5($datakey);
		if(!$ifNotExists || !array_key_exists($real_key, $_SESSION[self::$_sessKey]))
		{
			$_SESSION[self::$_sessKey][$real_key] = array($data, 0, $maxCacheUses);
		}
	}
	/**
	 * Retrieves a previous cached data using SetCache. If a cache doesn't exists, 
	 * this function retrieves null.
	 * 
	 * $datakey: A key (any string) that uniquely identifyes the data you cached. 
	 * This has to be the same string that you used on the function SetCache.
	 */
	public static function GetCache($datakey)
	{
		$return = null;
		self::CheckKey();
		
		$real_key = self::$useRawKey ? $datakey : md5($datakey);
		if(!array_key_exists($real_key, $_SESSION[self::$_sessKey])) return $return;
		
		$data = $_SESSION[self::$_sessKey][$real_key];
		if($data[1] == self::INFINITE) $return = $data[0];
		else
		{
			$return = $data[0];
			$_SESSION[self::$_sessKey][$real_key][1] = $data[1] = $data[1] + 1;
			if($data[1] >= $data[2]) unset($_SESSION[self::$_sessKey][$real_key]);
		}
		return $return;
	}
	/**
	 * Deletes a cached data if exists (if it was cached using SetCache).
	 * 
	 * $datakey: A key (any string) that uniquely identifyes the data you cached. 
	 * This has to be the same string that you used on the function SetCache.
	 */
	public static function DelCache($datakey)
	{
		self::CheckKey();
		unset($_SESSION[self::$_sessKey][self::$useRawKey ? $datakey : md5($datakey)]);
	}
	/**
	 * Deletes all cached data using this class.
	 */
	public static function DelAllCache()
	{
		self::CheckKey();
		$_SESSION[self::$_sessKey] = array();
	}
	/**
	 * Returns the $_SESSION key that its beeing used to cache the data, 
	 * that by default will be 'SessCacher' (unless you changed it).
	 */
	public static function GetSessKey()
	{
		return self::$_sessKey;
	}
	/**
	 * Sets the $_SESSION key that its beeing used to cache the data.
	 * No cached data will be lost by changing the key.
	 * 
	 * $newKey: The new $_SESSION key
	 */
	public static function SetSessKey($newKey)
	{
		$newKey = (string)$newKey;
		if($newKey == '' || self::$_sessKey == $newKey) return;
		
		self::CheckKey();
		
		$_SESSION[$newKey] = $_SESSION[self::$_sessKey];
		unset($_SESSION[self::$_sessKey]);
		self::$_sessKey = $newKey;
	}
	////////////////////////////////////////////////////////////////////
	//PRIVATES
	////////////////////////////////////////////////////////////////////
	private static function CheckKey()
	{
		if(!isset($_SESSION)) session_start();
		if(!array_key_exists(self::$_sessKey, $_SESSION)) $_SESSION[self::$_sessKey] = array();
	}
}

//How to use
$init_time = microtime(true);//Init measuring the total time taken to get the results

$in_cache = true;

//Count queries might take a long time
$query = "SELECT COUNT(*) FROM huge_table";

//Here i am using the query as a unique key(string) to identify the cached data
$results = SessCacher::GetCache($query);
if($results == null)
{
	$in_cache = false;
	
	//If the SessCacher returned null it means that 
	//the result is not cached an we have to retreive
	//it from the database
	
	//$mysqli_link: the mysqli database link
	$mysqli_result = mysqli_query($mysqli_link, $query);
	
	//We will put the fetched results into an array to cache them.
	$results = array();
	while($row = mysqli_fetch_assoc($mysqli_result))
	{
		array_push($results, $row);
	}
	
	//Here i cache the results using the query as a key
	//and specifing that i want to reuse the cache 
	//no more than 5 times
	SessCacher::SetCache($query, $results, 5);
}

//The total time taken to retreive the results from cache or database in milliseconds
$total_time = (string)round((microtime(true) - $init_time) * 1000, 2);


//Echoing for testing purposes:

//Total time taken to get results
echo 'total time taken ' . 
	 ($in_cache ? '(results from cache): ' : '(results from database): ') . 
	 $total_time . ' milliseconds';
echo '<br />';
echo '<pre>';
print_r($results);
echo '</pre>';
?>

Revision: 51043
at September 13, 2011 05:47 by TimoZachi


Initial Code
<?php
class SessCacher
{
	//constant with self-documented names that holds this class error codes num
	const MAX_USES_TOO_LOW = 9000;
	
	const INFINITE = -1;
	
	/**
	 * If false (Default), this class will apply md5 php function to compress the cache key. 
	 */
	public static $useRawKey = false;
	
	private static $_sessKey = 'SessCacher';
	
	//Constructor private, no instantiation. Use this class staticaly
	private function __construct()
	{}
	
	/**
	 * This function is used to cache any data through php's SESSION. 
	 * To reuse the cached data use see function GetCache.
	 * 
	 * $datakey: A key (any string) to uniquely identify the data you are caching. 
	 * You will need this key to retrieve the cached data later.
	 * 
	 * $data: The data you want to cache (any data).
	 * 
	 * $maxCacheUses: This will limit how many times you want to retreive the data.
	 * The default value is SessCacher::INFINITE times, wich means the cached data will only be 
	 * deleted after the session ends.
	 * 
	 * $ifNotExists: If you are trying to cache a data that is already cached, the 
	 * data you are trying to chache will not override the previous data if 
	 * $ifNotExists = true (Default value).
	 */
	public static function SetCache($datakey, $data, $maxCacheUses = self::INFINITE, $ifNotExists = true)
	{
		if($maxCacheUses != self::INFINITE && $maxCacheUses < 1)
		{
			throw new Exception('param $maxCacheUses: ' . $maxCacheUses . 
								' cannot be lower than 1. Use SessCacher::DelCache() to delete cache.',
								self::MAX_USES_TOO_LOW);
		}
		self::CheckKey();
		
		$real_key = self::$useRawKey ? $datakey : md5($datakey);
		if(!$ifNotExists || !array_key_exists($real_key, $_SESSION[self::$_sessKey]))
		{
			$_SESSION[self::$_sessKey][$real_key] = array($data, 0, $maxCacheUses);
		}
	}
	/**
	 * Retrieves a previous cached data using SetCache. If a cache doesn't exists, 
	 * this function retrieves null.
	 * 
	 * $datakey: A key (any string) that uniquely identifyes the data you cached. 
	 * This has to be the same string that you used on the function SetCache.
	 */
	public static function GetCache($datakey)
	{
		$return = null;
		self::CheckKey();
		
		$real_key = self::$useRawKey ? $datakey : md5($datakey);
		if(!array_key_exists($real_key, $_SESSION[self::$_sessKey])) return $return;
		
		$data = $_SESSION[self::$_sessKey][$real_key];
		if($data[1] == self::INFINITE) $return = $data[0];
		else
		{
			$return = $data[0];
			$_SESSION[self::$_sessKey][$real_key][1] = $data[1] = $data[1] + 1;
			if($data[1] >= $data[2]) unset($_SESSION[self::$_sessKey][$real_key]);
		}
		return $return;
	}
	/**
	 * Deletes a cached data if exists (if it was cached using SetCache).
	 * 
	 * $datakey: A key (any string) that uniquely identifyes the data you cached. 
	 * This has to be the same string that you used on the function SetCache.
	 */
	public static function DelCache($datakey)
	{
		self::CheckKey();
		unset($_SESSION[self::$_sessKey][self::$useRawKey ? $datakey : md5($datakey)]);
	}
	/**
	 * Deletes all cached data using this class.
	 */
	public static function DelAllCache()
	{
		self::CheckKey();
		$_SESSION[self::$_sessKey] = array();
	}
	/**
	 * Returns the $_SESSION key that its beeing used to cache the data, 
	 * that by default will be 'SessCacher' (unless you changed it).
	 */
	public static function GetSessKey()
	{
		return self::$_sessKey;
	}
	/**
	 * Sets the $_SESSION key that its beeing used to cache the data.
	 * No cached data will be lost by changing the key.
	 * 
	 * $newKey: The new $_SESSION key
	 */
	public static function SetSessKey($newKey)
	{
		$newKey = (string)$newKey;
		if($newKey == '' || self::$_sessKey == $newKey) return;
		
		self::CheckKey();
		
		$_SESSION[$newKey] = $_SESSION[self::$_sessKey];
		unset($_SESSION[self::$_sessKey]);
		self::$_sessKey = $newKey;
	}
	////////////////////////////////////////////////////////////////////
	//PRIVATES
	////////////////////////////////////////////////////////////////////
	private static function CheckKey()
	{
		if(!isset($_SESSION)) session_start();
		if(!array_key_exists(self::$_sessKey, $_SESSION)) $_SESSION[self::$_sessKey] = array();
	}
}

//How to use
$init_time = microtime(true);//Init measuring the total time taken to get the results

$in_cache = true;

$query = "SELECT * FROM hit LIMIT 0, 2000";

//Here i am using the query as a unique key(string) to identify the cached data
$results = SessCacher::GetCache($query);
if($results == null)
{
	$in_cache = false;
	
	//If the SessCacher returned null it means that 
	//the result is not cached an we have to retreive
	//it from the database
	
	//$mysqli_link: the mysqli database link
	//$mysqli_result = mysqli_query($mysqli_link, $query);
	$mysqli_result = DBHandler::Execute($query, null);
	
	//We will put the fetched results into an array to cache them.
	$results = array();
	while($row = mysqli_fetch_assoc($mysqli_result))
	{
		array_push($results, $row);
	}
	
	//Here i cache the results using the query as a key
	//and specifing that i want to reuse the cache 
	//no more than 5 times
	SessCacher::SetCache($query, $results, 5);
}

//The total time taken to retreive the results from cache or database in milliseconds
$total_time = (string)round((microtime(true) - $init_time) * 1000, 2);


//Echoing for testing purposes:

//Total time taken to get results
echo 'total time taken ' . 
	 ($in_cache ? '(results from cache): ' : '(results from database): ') . 
	 $total_time . ' milliseconds';
echo '<br />';
echo '<pre>';
print_r($results);
echo '</pre>';
?>

Initial URL


Initial Description
This is a custom class for caching results through php's session. The class can be used to cache anything, but i use it to cache mysql results so i don't have to get all the time the results by querying MySQL (wich may take a long time, especialy if  MySQL's server is not the same as PHP's server), increasing enormously performance. Since the database is in constant actualization, there is an optional parameter to limit the maximun number of times you want to reuse the cached data. The class is well commented with an example on how to use it bellow.

Initial Title
SessCacher: Class for caching data through php session

Initial Tags
mysql, class, data

Initial Language
PHP