steffen: server/kolab-horde-framework/kolab-horde-framework/Cache/Cache file.php, NONE, 1.1 zps.php, NONE, 1.1
cvs at intevation.de
cvs at intevation.de
Fri Oct 14 16:33:05 CEST 2005
Author: steffen
Update of /kolabrepository/server/kolab-horde-framework/kolab-horde-framework/Cache/Cache
In directory doto:/tmp/cvs-serv28903/kolab-horde-framework/kolab-horde-framework/Cache/Cache
Added Files:
file.php zps.php
Log Message:
Separated Horde Framework from kolab-resource-handlers
--- NEW FILE: file.php ---
<?php
/**
* The Horde_Cache_file:: class provides a filesystem implementation of the
* Horde caching system.
*
* Optional values for $params:
* 'dir' The directory to store the cache files in.
* 'prefix' The filename prefix to use for the cache files.
*
* $Horde: framework/Cache/Cache/file.php,v 1.26 2004/04/29 15:16:41 jan Exp $
*
* Copyright 1999-2004 Anil Madhavapeddy <anil at recoil.org>
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
* @author Anil Madhavapeddy <anil at recoil.org>
* @version $Revision: 1.1 $
* @since Horde 1.3
* @package Horde_Cache
*/
class Horde_Cache_file extends Horde_Cache {
/**
* The location of the temp directory.
*
* @var string $dir
*/
var $_dir;
/**
* The filename prefix for cache files.
*
* @var string $prefix
*/
var $_prefix = 'cache_';
/**
* Construct a new Horde_Cache_file object.
*
* @access public
*
* @param array $params Parameter array.
*/
function Horde_Cache_file($params = array())
{
if (array_key_exists('dir', $params) && @is_dir($params['dir'])) {
$this->_dir = $params['dir'];
} else {
require_once 'Horde/Util.php';
$this->_dir = Util::getTempDir();
}
if (array_key_exists('prefix', $params)) {
$this->_prefix = $params['prefix'];
}
}
/**
* Attempts to store an object to the filesystem.
*
* @access protected
*
* @param string $oid Object ID used as the caching key.
* @param mixed $data Data to store in the cache.
*
* @return boolean True on success, false on failure.
*/
function _store($oid, $data)
{
require_once 'Horde/Util.php';
$filename = $this->_oidToFile($oid);
$tmp_file = Util::getTempFile('HordeCache', true, $this->_dir);
if ($fd = fopen($tmp_file, 'w')) {
if (fwrite($fd, $data) < strlen($data)) {
fclose($fd);
return false;
} else {
fclose($fd);
@rename($tmp_file, $filename);
}
}
}
/**
* Attempts to retrieve a cached object from the filesystem and
* return it to the caller.
*
* @access protected
*
* @param string $oid Object ID to query.
* @param integer $lifetime Lifetime of the object in seconds.
*
* @return mixed Cached data, or false if none was found.
*/
function _fetch($oid, $lifetime = 1)
{
$filename = $this->_oidToFile($oid);
if ($this->_isValid($filename, $lifetime)) {
$fd = @fopen($filename, 'r');
if ($fd) {
$data = fread($fd, filesize($filename));
fclose($fd);
return $data;
}
}
/* Nothing cached, return failure. */
return false;
}
/**
* Attempts to directly output a cached object from the
* filesystem.
*
* @access protected
*
* @param string $oid Object ID to query.
* @param integer $lifetime Lifetime of the object in seconds.
*
* @return mixed Cached data, or false if none was found.
*/
function _output($oid, $lifetime = 1)
{
$filename = $this->_oidToFile($oid);
if ($this->_isValid($filename, $lifetime)) {
$fd = @fopen($filename, 'r');
if ($fd) {
fpassthru($fd);
return true;
}
}
/* Nothing cached, return failure. */
return false;
}
/**
* Check to see if the object exists in the cache, and whether or
* not it's expired. If it has expired, delete it.
*
* @param string $filename The file to check.
* @param integer $lifetime The max age (in seconds) of the object.
*
* @return boolean Whether or not there is a current copy of the object
* in the cache.
*/
function _isValid($filename, $lifetime)
{
/* An object exists in the cache */
if (file_exists($filename)) {
/* 0 means no expire. */
if ($lifetime == 0) {
return true;
}
$lastmod = filemtime($filename);
/* If the object has been created after the supplied
* value, the object is valid. */
if (time() - $lifetime <= $lastmod) {
return true;
} else {
@unlink($filename);
}
}
return false;
}
/**
* Map an object ID to a unique filename.
*
* @access private
*
* @param string $oid Object ID
*
* @return string Fully qualified filename.
*/
function _oidToFile($oid)
{
return $this->_dir . '/' . $this->_prefix . md5($oid);
}
}
--- NEW FILE: zps.php ---
<?php
/**
* The Cache_zps:: class provides a Zps Performance Suite
* implementation of the Horde caching system.
*
* $Horde: framework/Cache/Cache/zps.php,v 1.4 2004/01/01 15:14:08 jan Exp $
*
* Copyright 2003-2004 Chuck Hagenbuch <chuck at horde.org>
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
* @author Chuck Hagenbuch <chuck at horde.org>
* @version $Revision: 1.1 $
* @since Horde 3.0
* @package Horde_Cache
*/
class Horde_Cache_zps extends Horde_Cache {
/**
* Returns the result of a cacheable function or method that
* returns its results, only actually calling it if there isn't a
* cached version or the cache has expired.
*
* @param string $oid The cache key.
* @param string $code The code to execute if the value isn't cached.
* @param integer $lifetime The lifetime of the object in the cache.
*
* Example:
*
* $foo = $cache->getData('myContent', 'function($arg1, $arg2)', $date);
*
* @return mixed The return value of the function or method.
* @access public
*/
function getData($oid, $code, $lifetime)
{
return output_cache_fetch($oid, $code, $lifetime);
}
/**
* Outputs the result of a function that returns its results, only
* actually calling it if there isn't a cached version or the
* cache has expired.
*
* @param string $oid The cache key.
* @param string $code The code to execute if the value isn't cached.
* @param integer $lifetime The lifetime of the object in the cache.
*
* Example:
*
* $cache->printData('myContent', 'function($arg1, $arg2)', $date);
*
* @return mixed The return value of the function or method.
* @access public
*/
function printData($oid, $code, $lifetime)
{
echo output_cache_fetch($oid, $code, $lifetime);
}
/**
* Returns the result of a cacheable function or method that
* prints its results, only actually calling it if there isn't a
* cached version or the cache has expired.
*
* @param string $oid The cache key.
* @param string $code The code to execute if the value isn't cached.
* @param integer $lifetime The lifetime of the object in the cache.
*
* Example:
*
* $foo = $cache->getOutput('myContent', 'function($arg1, $arg2)', $date);
*
* @return mixed Any return status.
* @access public
*/
function getOutput($oid, $code, $lifetime)
{
ob_start();
output_cache_output($oid, $code, $lifetime);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
/**
* Outputs the result of a cacheable function or method that
* prints its results, only actually calling it if there isn't a
* cached version or the cache has expired.
*
* @param string $oid The cache key.
* @param string $code The code to execute if the value isn't cached.
* @param integer $lifetime The lifetime of the object in the cache.
*
* Example:
*
* $cache->printOutput('myContent', 'function($arg1, $arg2)', $date);
*
* @return mixed Any return status.
* @access public
*/
function printOutput($oid, $code, $lifetime)
{
return output_cache_output($oid, $code, $lifetime);
}
}
More information about the commits
mailing list