Created
November 1, 2011 03:43
-
-
Save LouisLandry/1329827 to your computer and use it in GitHub Desktop.
JCache concept.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @package Joomla.Platform | |
* @subpackage Cache | |
* | |
* @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE | |
*/ | |
defined('JPATH_PLATFORM') or die(); | |
/** | |
* Joomla! Caching Class | |
* | |
* @package Joomla.Platform | |
* @subpackage Cache | |
* @since 12.1 | |
*/ | |
class JCache | |
{ | |
/** | |
* @var JRegistry The options for the cache object. | |
* @since 12.1 | |
*/ | |
protected $options; | |
/** | |
* Constructor. | |
* | |
* @param JRegistry $options Caching options object. | |
* | |
* @since 12.1 | |
*/ | |
public function __construct(JRegistry $options = null) | |
{ | |
if ($options instanceof JRegistry) | |
{ | |
$this->options = $options; | |
} | |
else | |
{ | |
$this->options = new JRegistry; | |
} | |
$this->options->def('dsn', 'file:///tmp/caching/'); | |
$this->options->def('lifetime', 900); | |
$this->options->def('locking', true); | |
} | |
/** | |
* Get an option from the JCache instance. | |
* | |
* @param string $key The name of the option to get. | |
* | |
* @return mixed The option value. | |
* | |
* @since 12.1 | |
*/ | |
public function getOption($key) | |
{ | |
return $this->options->get($key); | |
} | |
/** | |
* Set an option for the JCache instance. | |
* | |
* @param string $key The name of the option to set. | |
* @param mixed $value The option value to set. | |
* | |
* @return JCache This object for method chaining. | |
* | |
* @since 12.1 | |
*/ | |
public function setOption($key, $value) | |
{ | |
$this->options->set($key, $value); | |
return $this; | |
} | |
/** | |
* Get cached data by id. If the cached data has expired then the cached data will be removed | |
* and false will be returned. | |
* | |
* @param string $cacheId The cache data id. | |
* | |
* @return mixed False on failure or a cached data string if it exists. | |
* | |
* @since 12.1 | |
*/ | |
public function get($cacheId) | |
{ | |
// If the cached data has expired remove it and return false. | |
if ($this->isExpired($cacheId)) | |
{ | |
$this->remove($cacheId); | |
return false; | |
} | |
// Open the stream resource. | |
$resource = fopen($this->fetchStreamUri($cacheId), 'rb'); | |
// If locking is enabled get a shared lock for reading on the resource. | |
if ($this->options->get('locking')) | |
{ | |
flock($resource, LOCK_SH); | |
} | |
// Read the data from the stream resource. | |
$data = stream_get_contents($resource); | |
// If locking is enabled release the lock on the resource. | |
if ($this->options->get('locking')) | |
{ | |
flock($resource, LOCK_UN); | |
} | |
// Tidy up behind ourselves. | |
fclose($resource); | |
return $data; | |
} | |
/** | |
* Check whether or not the cached data by id has expired. | |
* | |
* @param string $cacheId The cache data id. | |
* | |
* @return boolean True if the data has expired. | |
* | |
* @since 12.1 | |
*/ | |
public function isExpired($cacheId) | |
{ | |
// Check to see if the cached data has expired. | |
if (filemtime($this->fetchStreamUri($cacheId)) < (time() - $this->options->get('lifetime'))) | |
{ | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Store the cached data by id. | |
* | |
* @param mixed $data The data to store | |
* @param string $cacheId The cache data id | |
* | |
* @return boolean True if cache stored. | |
* | |
* @since 12.1 | |
*/ | |
public function store($data, $cacheId) | |
{ | |
return (bool) file_put_contents( | |
$this->fetchStreamUri($cacheId), | |
$data, | |
($this->options->get('locking') ? LOCK_EX : null) | |
); | |
} | |
/** | |
* Remove a cached data entry by id. | |
* | |
* @param string $cacheId The cache data id. | |
* | |
* @return boolean True on success, false otherwise | |
* | |
* @since 12.1 | |
*/ | |
public function remove($cacheId) | |
{ | |
return unlink($this->fetchStreamUri($cacheId)); | |
} | |
/** | |
* Remove a cached data entry by id. | |
* | |
* @param string $cacheId The cache data id. | |
* | |
* @return string The full stream URI for the cache entry. | |
* | |
* @since 12.1 | |
*/ | |
protected function fetchStreamUri($cacheId) | |
{ | |
return $this->options->get('dsn') . $cacheId; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment