/usr/share/php/Cache/Container.php is in php-cache 1.5.6-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | <?php
// +----------------------------------------------------------------------+
// | PEAR :: Cache |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Ulf Wendel <ulf.wendel@phpdoc.de> |
// | Sebastian Bergmann <sb@sebastian-bergmann.de> |
// | Christian Stocker <chregu@phant.ch> |
// +----------------------------------------------------------------------+
//
// $Id: Container.php 293863 2010-01-23 03:46:52Z clockwerx $
require_once 'Cache/Error.php';
/**
* Common base class of all cache storage container.
*
* To speed up things we do a preload you should know about, otherwise it might
* play you a trick. The Cache controller classes (Cache/Cache, Cache/Output, ...)
* usually do something like is (isCached($id) && !isExpired($id)) return $container->load($id).
* if you implement isCached(), isExpired() and load() straight ahead, each of this
* functions will result in a storage medium (db, file,...) access. This generates too much load.
* Now, a simple speculative preload should saves time in most cases. Whenever
* one of the mentioned methods is invoked we preload the cached dataset into class variables.
* That means that we have only one storage medium access for the sequence
* (isCached($id) && !isExpired($id)) return $container->load($id).
* The bad thing is that the preloaded data might be outdated meanwhile, which is
* unlikely but for you power users, be warned. If you do not want the preload
* you should switch it off by setting the class variable $preload to false. Anyway, this is
* not recommended!
*
* @author Ulf Wendel <ulf.wendel@phpdoc.de>
* @version $Id: Container.php 293863 2010-01-23 03:46:52Z clockwerx $
* @package Cache
* @access public
* @abstract
*/
class Cache_Container
{
/**
* Flag indicating wheter to preload datasets.
*
* See the class description for more details.
*
* @var boolean
*/
var $preload = true;
/**
* ID of a preloaded dataset
*
* @var string
*/
var $id = '';
/**
* Cache group of a preloaded dataset
*
* @var string
*/
var $group = '';
/**
* Expiration timestamp of a preloaded dataset.
*
* @var integer 0 means never, endless
*/
var $expires = 0;
/**
* Value of a preloaded dataset.
*
* @var string
*/
var $cachedata = '';
/**
* Preloaded userdata field.
*
* @var string
*/
var $userdata = '';
/**
* Flag indicating that the dataset requested for preloading is unknown.
*
* @var boolean
*/
var $unknown = true;
/**
* Encoding mode for cache data: base64 or addslashes() (slash).
*
* @var string base64 or slash
*/
var $encoding_mode = 'base64';
/**
* Highwater mark - maximum space required by all cache entries.
*
* Whenever the garbage collection runs it checks the amount of space
* required by all cache entries. If it's more than n (highwater) bytes
* the garbage collection deletes as many entries as necessary to reach the
* lowwater mark.
*
* @var int
* @see lowwater
*/
var $highwater = 2048000;
/**
* Lowwater mark
*
* @var int
* @see highwater
*/
var $lowwater = 1536000;
/**
* Options that can be set in every derived class using it's constructor.
*
* @var array
*/
var $allowed_options = array('encoding_mode', 'highwater', 'lowwater');
/**
* Loads a dataset from the cache.
*
* @param string dataset ID
* @param string cache group
* @return mixed dataset value or null on failure
* @access public
*/
function load($id, $group)
{
if ($this->preload) {
if ($this->id != $id || $this->group != $group) {
$this->preload($id, $group);
}
return $this->cachedata;
}
$ret = $this->fetch($id, $group);
if (PEAR::isError($ret)) {
return $ret;
}
list( , $data, ) = $ret;
return $data;
} // end func load
/**
* Returns the userdata field of a cached data set.
*
* @param string dataset ID
* @param string cache group
* @return string userdata
* @access public
*/
function getUserdata($id, $group)
{
if ($this->preload) {
if ($this->id != $id || $this->group != $group) {
$this->preload($id, $group);
}
return $this->userdata;
}
$ret = $this->fetch($id, $group);
if (PEAR::isError($ret)) {
return $ret;
}
list( , , $userdata) = $ret;
return $userdata;
} // end func getUserdata
/**
* Checks if a dataset is expired.
*
* @param string dataset ID
* @param string cache group
* @param integer maximum age timestamp
* @return boolean
* @access public
*/
function isExpired($id, $group, $max_age)
{
if ($this->preload) {
if ($this->id != $id || $this->group != $group) {
$this->preload($id, $group);
}
if ($this->unknown) {
return false;
}
} else {
// check if at all it is cached
if (!$this->isCached($id, $group)) {
return false;
}
// I'm lazy...
$ret = $this->fetch($id, $group);
if (PEAR::isError($ret)) {
return $ret;
}
list($this->expires, , ) = $ret;
}
// endless
if (0 == $this->expires) {
return false;
}
// you feel fine, Ulf?
if ($expired = ($this->expires <= time() || ($max_age && ($this->expires <= $max_age))) ) {
$this->remove($id, $group);
$this->flushPreload();
}
return $expired;
} // end func isExpired
/**
* Checks if a dataset is cached.
*
* @param string dataset ID
* @param string cache group
* @return boolean
*/
function isCached($id, $group)
{
if ($this->preload) {
if ($this->id != $id || $this->group != $group) {
$this->preload($id, $group);
}
return !($this->unknown);
}
return $this->idExists($id, $group);
} // end func isCached
//
// abstract methods
//
/**
* Fetches a dataset from the storage medium.
*
* @param string dataset ID
* @param string cache group
* @return array format: [expire date, cached data, user data]
* @throws Cache_Error
* @abstract
*/
function fetch($id, $group)
{
return array(null, null, null);
} // end func fetch
/**
* Stores a dataset.
*
* @param string dataset ID
* @param mixed data to store
* @param mixed userdefined expire date
* @param string cache group
* @param string additional userdefined data
* @return boolean
* @throws Cache_Error
* @access public
* @abstract
*/
function save($id, $data, $expire, $group, $userdata)
{
// QUESTION: Should we update the preload buffer instead?
// Don't think so as the sequence save()/load() is unlikely.
$this->flushPreload($id, $group);
return null;
} // end func save
/**
* Removes a dataset.
*
* @param string dataset ID
* @param string cache group
* @return boolean
* @access public
* @abstract
*/
function remove($id, $group)
{
$this->flushPreload($id, $group);
return null;
} // end func remove
/**
* Flushes the cache - removes all caches datasets from the cache.
*
* @param string If a cache group is given only the group will be flushed
* @return integer Number of removed datasets, -1 on failure
* @access public
* @abstract
*/
function flush($group)
{
$this->flushPreload();
return null;
} // end func flush
/**
* Checks if a dataset exists.
*
* @param string dataset ID
* @param string cache group
* @return boolean
* @access public
* @abstract
*/
function idExists($id, $group)
{
return null;
} // end func idExists
/**
* Starts the garbage collection.
*
* @param int $gc_maxlifetime The maximum lifetime (seconds) for a cache
* entry. Implemented by containers,
*
* @access public
* @abstract
*/
function garbageCollection($gc_maxlifetime)
{
$this->flushPreload();
} // end func garbageCollection
/**
* Does a speculative preload of a dataset
*
* @param string dataset ID
* @param string cache group
* @return boolean
*/
function preload($id, $group)
{
// whatever happens, remember the preloaded ID
$this->id = $id;
$this->group = $group;
$ret = $this->fetch($id, $group);
if (PEAR::isError($ret)) {
return $ret;
}
list($this->expires, $this->cachedata, $this->userdata) = $ret;
if ($this->expires === null) {
// Uuups, unknown ID
$this->flushPreload();
return false;
}
$this->unknown = false;
return true;
} // end func preload
/**
* Flushes the internal preload buffer.
*
* save(), remove() and flush() must call this method
* to preevent differences between the preloaded values and
* the real cache contents.
*
* @param string dataset ID, if left out the preloaded values will be flushed.
* If given the preloaded values will only be flushed if they are
* equal to the given id and group
* @param string cache group
* @see preload()
*/
function flushPreload($id = '', $group = 'default')
{
if (!$id || ($this->id == $id && $this->group == $group)) {
// clear the internal preload values
$this->id = '';
$this->group = '';
$this->cachedata = '';
$this->userdata = '';
$this->expires = -1;
$this->unknown = true;
}
} // end func flushPreload
/**
* Imports the requested datafields as object variables if allowed
*
* @param array List of fields to be imported as object variables
* @param array List of allowed datafields
*/
function setOptions($requested, $allowed)
{
foreach ($allowed as $k => $field) {
if (isset($requested[$field])) {
$this->$field = $requested[$field];
}
}
} // end func setOptions
/**
* Encodes the data for the storage container.
*
* @var mixed data to encode
*/
function encode($data)
{
if ($this->encoding_mode == 'base64') {
return base64_encode(serialize($data));
} else {
return serialize($data);
}
} // end func encode
/**
* Decodes the data from the storage container.
*
* @var mixed
*/
function decode($data)
{
if ($this->encoding_mode == 'base64') {
return unserialize(base64_decode($data));
} else {
return unserialize($data);
}
} // end func decode
/**
* Translates human readable/relative times in unixtime
*
* @param mixed can be in the following formats:
* human readable : yyyymmddhhmm[ss]] eg: 20010308095100
* relative in seconds (1) : +xx eg: +10
* relative in seconds (2) : x < 946681200 eg: 10
* absolute unixtime : x < 2147483648 eg: 2147483648
* see comments in code for details
* @return integer unix timestamp
*/
function getExpiresAbsolute($expires)
{
if (!$expires) {
return 0;
}
//for api-compatibility, one has not to provide a "+",
// if integer is < 946681200 (= Jan 01 2000 00:00:00)
if ($expires[0] == '+' || $expires < 946681200) {
return(time() + $expires);
} elseif ($expires < 100000000000) {
//if integer is < 100000000000 (= in 3140 years),
// it must be an absolut unixtime
// (since the "human readable" definition asks for a higher number)
return $expires;
} else {
// else it's "human readable";
$year = substr($expires, 0, 4);
$month = substr($expires, 4, 2);
$day = substr($expires, 6, 2);
$hour = substr($expires, 8, 2);
$minute = substr($expires, 10, 2);
$second = substr($expires, 12, 2);
return mktime($hour, $minute, $second, $month, $day, $year);
}
} // end func getExpireAbsolute
} // end class Container
?>
|