<?php
/**
* A Cache API
*/
error_reporting(E_ALL);
Interface Cache
{
public static function put($key, $value, $duration);
public static function get($key);
public static function delete($key);
public static function flush();
}
Class CacheElement
{
public $key;
public $value;
public $expiry;
public function __construct($key, $value, $duration)
{
$this->key = $key;
$this->value = $value;
$this->expiry = time() + $duration;
}
}
Class CacheDemo implements Cache
{
public static function put($key, $value, $duration)
{
$_SESSION['cache_data'][$key] = new CacheElement($key, $value, $duration);
}
public static function get($key)
{
if (empty($_SESSION['cache_data'][$key])) return FALSE;
if ($_SESSION['cache_data'][$key]->expiry > time()) return $_SESSION['cache_data'][$key]->value;
self::delete($key);
return FALSE;
}
public static function delete($key)
{
unset($_SESSION['cache_data'][$key]);
}
public static function flush()
{
foreach ($_SESSION['cache_data'] as $key => $element)
{
self::delete($key);
}
}
}
<?php // demo/EE_cache_demo.php
/**
* A Cache API
*/
error_reporting(E_ALL);
Interface Cache
{
public static function put($key, $value, $duration);
public static function get($key);
public static function delete($key);
public static function flush();
}
Class CacheElement
{
public $key;
public $expiry;
public $value;
public function __construct($key, $value, $duration)
{
$this->key = $key;
$this->value = $value;
$this->expiry = time() + $duration;
}
}
Class CacheDemo implements Cache
{
public static function put($key, $value, $duration)
{
$_SESSION['cache_data'][$key] = new CacheElement($key, $value, $duration);
}
public static function get($key)
{
if (empty($_SESSION['cache_data'][$key])) return FALSE;
if ($_SESSION['cache_data'][$key]->expiry > time()) return $_SESSION['cache_data'][$key]->value;
self::delete($key);
return FALSE;
}
public static function delete($key)
{
unset($_SESSION['cache_data'][$key]);
}
public static function flush()
{
foreach ($_SESSION['cache_data'] as $key => $element)
{
self::delete($key);
}
}
}
// WE USE THE PHP SESSION FOR THE CACHE
session_start();
// THE ACTIONS THAT CAN BE PERFORMED BY THIS SCRIPT
$key = (!empty($_GET['key'])) ? strtolower($_GET['key']) : NULL;
// SPECIAL CASE KEY: OMITTED
if (!$key) die('No key');
// SPECIAL CASE KEY: FLUSH THE CACHE
if ($key == 'flush')
{
CacheDemo::flush();
$response = 'At ' . date('H:i:s') . ', the cache was flushed';
die($response);
}
// TRY THE CACHE FOR THIS KEY
$response = CacheDemo::get($key);
// IF THE RESPONSE HAS BEEN CACHED
if ($response)
{
die('Retrieved from cache at ' . date('H:i:s ') . $response);
}
// IF NO CACHED REPRESENTATION IS AVAILABLE YET
else
{
// PRETEND IT TAKES A LONG TIME TO GENERATE THIS COMPLICATED RESPONSE
sleep(3);
// CREATE THE RESPONSE AND CACHE IT FOR HALF A MINUTE
$response = 'This response to the request for: ' . $key . ' was created at ' . date('H:i:s');
CacheDemo::put($key, $response, 30);
// RETURN THE NEWLY CREATED RESPONSE
die($response);
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)