Source for file shm.php
Documentation is available at shm.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | 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> |
// +----------------------------------------------------------------------+
// $Id: shm.php 316194 2011-09-05 20:35:25Z cweiske $
require_once 'Cache/Container.php';
* Stores cache data into shared memory.
* Well, this is not a very efficient implementation. Indeed it's much
* slower than the file container as far as my tests showed. Files are
* cached by most operating systems and it will be hard to write a faster
* caching algorithm using PHP.
* @author Ulf Wendel <ulf.wendel@phpdoc.de>
* @version $Id: shm.php 316194 2011-09-05 20:35:25Z cweiske $
* Key of the semaphore used to sync the SHM access
* Permissions of the semaphore used to sync the SHM access
* Key of the shared memory block used to store cache data
* Size of the shared memory block used
* Note: the container does only use _one_ shm block no more!
* Permissions of the shared memory block
* Used by the garbage collection to find old entries.
* Number of bytes consumed by the cache
* Creates a shared memory container
* @param array shm_key, sem_key, shm_size, sem_perm, shm_perm
array ('shm_key', 'sem_key',
// Cache::Container high- and lowwater defaults should be overridden if
// not already done by the user
//get SHM and Semaphore handles
new Cache_Error("Can't get semaphore '{ $this->sem_key}' using perms '{ $this->sem_perm}'. ",
function fetch($id, $group)
$cachedata = $this->decode($cachedata);
if (!isset ($cachedata[$group][$id])) {
return array(null, null, null);
$cachedata = $cachedata[$group][$id];
return array($cachedata['expire'],
function save($id, $data, $expire, $group, $userdata)
shmop_write($this->shm_id, $newdata, 0 );
function remove($id, $group)
unset ($cachedata[$group][$id]);
function flush($group = '')
function idExists($id, $group)
$cachedata = $this->decode($cachedata);
return isset ($cachedata[$group][$id]);
function garbageCollection($maxlifetime, $cachedata = array())
if ($lock = empty($cachedata)) {
while (list($k, $entry) = each($entries)) {
unset($cachedata[$entry['group']][$entry['id']]);
} // end func garbageCollection
function doGarbageCollection($maxlifetime, &$cachedata)
$changed = time() - $maxlifetime;
while (list($group, $groupdata) = each($cachedata)) {
while (list($id, $data) = each($groupdata)) {
if ($data['expire'] < time() || $data['changed'] < $changed) {
unset($cachedata[$group][$id]);
// ugly but simple to implement :/
$size = strlen($this->encode($data));
} // end func doGarbageCollection
} // end class Cache_Container_shm
Documentation generated on Mon, 11 Mar 2019 15:44:50 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|