/ Published in: PHP
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?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(); { } } /** * 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(); $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; } 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(); } /** * Deletes all cached data using this class. */ public static function DelAllCache() { self::CheckKey(); } /** * 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]; self::$_sessKey = $newKey; } //////////////////////////////////////////////////////////////////// //PRIVATES //////////////////////////////////////////////////////////////////// private static function CheckKey() { } } //How to use $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 //We will put the fetched results into an array to cache them. { } //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 //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>'; echo '</pre>'; ?>