Cache
[ class tree: Cache ] [ index: Cache ] [ all elements ]

Class: Cache

Source Location: /Cache-1.5.7/Cache.php

Class Overview

PEAR
   |
   --Cache

Cache is a base class for cache implementations.


Author(s):

Version:

  • $Id: Cache.php 316194 2011-09-05 20:35:25Z cweiske $

Variables

Methods


Child classes:

Cache_Application
Cache is a base class for cache implementations.
Cache_Graphics
Graphics disk cache.
Cache_HTTP_Request
HTTP_Request Cache
Cache_Output
Class to cache the output of a script using the output buffering functions
Cache_Function
Function_Cache

Inherited Variables

Inherited Methods


Class Details

[line 66]
Cache is a base class for cache implementations.

The pear cache module is a generic data cache which can be used to cache script runs. The idea behind the cache is quite simple. If you have the same input parameters for whatever tasks/algorithm you use you'll usually get the same output. So why not caching templates, functions calls, graphic generation etc. Caching certain actions e.g. XSLT tranformations saves you lots of time.

The design of the cache reminds of PHPLibs session implementation. A (PHPLib: session) controller uses storage container (PHPLib: ct_*.inc) to save certain data (PHPLib: session data). In contrast to the session stuff it's up to you to generate an ID for the data to cache. If you're using the output cache you might use the script name as a seed for cache::generateID(), if your using the function cache you'd use an array with all function parameters.

Usage example of the generic data cache:

require_once('Cache.php');

$cache = new Cache('file', array('cache_dir' => 'cache/') ); $id = $cache->generateID('testentry');

if ($data = $cache->get($id)) { print "Cache hit.
Data: $data";

} else { $data = 'data of any kind'; $cache->save($id, $data); print 'Cache miss.
'; }

WARNING: No File/DB-Table-Row locking is implemented yet, it's possible, that you get corrupted data-entries under bad circumstances (especially with the file container)

  • Author: Ulf Wendel <ulf.wendel@phpdoc.de>
  • Version: $Id: Cache.php 316194 2011-09-05 20:35:25Z cweiske $
  • Access: public


[ Top ]


Class Variables

$container =

[line 119]

Storage container if sucess in load it and a Cache_Error if not



[ Top ]

$gc_maxlifetime =  86400

[line 111]

Garbage collection: delete all entries not use for n seconds.

Default is one day, 60 * 60 * 24 = 86400 seconds.


Type:   integer


[ Top ]

$gc_probability =  1

[line 101]

Garbage collection: probability in percent

TODO: Add an explanation.


Type:   integer


[ Top ]

$gc_time =  1

[line 90]

Garbage collection: probability in seconds

If set to a value above 0 a garbage collection will flush all cache entries older than the specified number of seconds.


Type:   integer


[ Top ]



Method Detail

Cache (Constructor)   [line 134]

Cache Cache( string $container, [array $container_options = null])

this Constructor set the Container Property as a Cache_Container|Cache_Error Object.

it Depends if a container is sucessfully load if not, a Cache_Error is set


Parameters:

string   $container   —  Name of container class
array   $container_options   —  Array with container class options

[ Top ]

_Cache (Destructor)   [line 158]

void _Cache( )


[ Top ]

extSave   [line 236]

boolean extSave( string $id, mixed $cachedata, string $userdata, [mixed $expires = 0], [string $group = 'default'])

Stores a dataset with additional userdefined data.

Overridden in child classes as:

Cache_OutputCompression::extSave()
Saves the given data to the cache.

Parameters:

string   $id   —  dataset ID
mixed   $cachedata   —  data to store
string   $userdata   —  additional userdefined data
mixed   $expires   —  userdefined expire date
string   $group   —  cache group

[ Top ]

flush   [line 299]

integer flush( [string $group = 'default'])

Flushes the cache - removes all data from it
  • Return: number of removed datasets

Parameters:

string   $group   —  cache group, if empty all groups will be flashed

[ Top ]

garbageCollection   [line 369]

void garbageCollection( [boolean $force = false])

Calls the garbage collector of the storage object with a certain probability

Parameters:

boolean   $force   —  Force a garbage collection run?

[ Top ]

generateID   [line 357]

string generateID( mixed $variable)

Generates a "unique" ID for the given value

This is a quick but dirty hack to get a "unique" ID for a any kind of variable. ID clashes might occur from time to time although they are extreme unlikely!

  • Return: "unique" ID
  • Access: public

Overridden in child classes as:

Cache_Graphics::generateID()
Cache_OutputCompression::generateID()

Parameters:

mixed   $variable   —  variable to generate a ID for

[ Top ]

get   [line 193]

mixed get( string $id, [string $group = 'default'])

Returns the requested dataset it if exists and is not expired
  • Return: cached data or null on failure
  • Access: public

Overridden in child classes as:

Cache_OutputCompression::get()

Parameters:

string   $id   —  dataset ID
string   $group   —  cache group

[ Top ]

getCaching   [line 169]

boolean getCaching( )

Returns the current caching state.
  • Return: The current caching state.
  • Access: public

[ Top ]

getUserdata   [line 269]

string getUserdata( string $id, [string $group = 'default'])

Returns the userdata field of a cached data set.

Parameters:

string   $id   —  dataset ID
string   $group   —  cache group

[ Top ]

isCached   [line 317]

boolean isCached( string $id, [string $group = 'default'])

Checks if a dataset exists.

Note: this does not say that the cached data is not expired!

  • Access: public

Parameters:

string   $id   —  dataset ID
string   $group   —  cache group

[ Top ]

isExpired   [line 339]

boolean isExpired( string $id, [string $group = 'default'], [integer $max_age = 0])

Checks if a dataset is expired
  • Access: public

Parameters:

string   $id   —  dataset ID
string   $group   —  cache group
integer   $max_age   —  maximum age for the cached data in seconds - 0 for endless If the cached data is older but the given lifetime it will be removed from the cache. You don't have to provide this argument if you call isExpired(). Every dataset knows it's expire date and will be removed automatically. Use this only if you know what you're doing...

[ Top ]

load   [line 252]

mixed load( string $id, [string $group = 'default'])

Loads the given ID from the cache.
  • Return: cached data or null on failure
  • Access: public

Parameters:

string   $id   —  dataset ID
string   $group   —  cache group

[ Top ]

remove   [line 285]

boolean remove( string $id, [string $group = 'default'])

Removes the specified dataset from the cache.
  • Access: public

Parameters:

string   $id   —  dataset ID
string   $group   —  cache group

[ Top ]

save   [line 215]

boolean save( string $id, mixed $data, [integer $expires = 0], [string $group = 'default'])

Stores the given data in the cache.
  • Access: public

Parameters:

string   $id   —  dataset ID used as cache identifier
mixed   $data   —  data to cache
integer   $expires   —  lifetime of the cached data in seconds - 0 for endless
string   $group   —  cache group

[ Top ]

setCaching   [line 180]

void setCaching( boolean $state)

Enables or disables caching.
  • Access: public

Parameters:

boolean   $state   —  The new caching state.

[ Top ]


Documentation generated on Mon, 11 Mar 2019 15:44:49 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.