You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
560 lines
16 KiB
PHTML
560 lines
16 KiB
PHTML
14 years ago
|
<?php
|
||
|
|
||
|
/*
|
||
|
+-----------------------------------------------------------------------+
|
||
|
| program/include/rcube_cache.php |
|
||
|
| |
|
||
|
| This file is part of the Roundcube Webmail client |
|
||
|
| Copyright (C) 2011, The Roundcube Dev Team |
|
||
|
| Copyright (C) 2011, Kolab Systems AG |
|
||
13 years ago
|
| |
|
||
|
| Licensed under the GNU General Public License version 3 or |
|
||
|
| any later version with exceptions for skins & plugins. |
|
||
|
| See the README file for a full license statement. |
|
||
14 years ago
|
| |
|
||
|
| PURPOSE: |
|
||
|
| Caching engine |
|
||
|
| |
|
||
|
+-----------------------------------------------------------------------+
|
||
|
| Author: Thomas Bruederli <roundcube@gmail.com> |
|
||
|
| Author: Aleksander Machniak <alec@alec.pl> |
|
||
|
+-----------------------------------------------------------------------+
|
||
|
*/
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Interface class for accessing Roundcube cache
|
||
|
*
|
||
12 years ago
|
* @package Framework
|
||
|
* @subpackage Cache
|
||
14 years ago
|
* @author Thomas Bruederli <roundcube@gmail.com>
|
||
|
* @author Aleksander Machniak <alec@alec.pl>
|
||
|
*/
|
||
|
class rcube_cache
|
||
|
{
|
||
|
/**
|
||
13 years ago
|
* Instance of database handler
|
||
14 years ago
|
*
|
||
13 years ago
|
* @var rcube_db|Memcache|bool
|
||
14 years ago
|
*/
|
||
|
private $db;
|
||
|
private $type;
|
||
|
private $userid;
|
||
|
private $prefix;
|
||
14 years ago
|
private $ttl;
|
||
14 years ago
|
private $packed;
|
||
14 years ago
|
private $index;
|
||
14 years ago
|
private $cache = array();
|
||
|
private $cache_changes = array();
|
||
|
private $cache_sums = array();
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Object constructor.
|
||
|
*
|
||
14 years ago
|
* @param string $type Engine type ('db' or 'memcache' or 'apc')
|
||
14 years ago
|
* @param int $userid User identifier
|
||
|
* @param string $prefix Key name prefix
|
||
13 years ago
|
* @param string $ttl Expiration time of memcache/apc items
|
||
14 years ago
|
* @param bool $packed Enables/disabled data serialization.
|
||
|
* It's possible to disable data serialization if you're sure
|
||
|
* stored data will be always a safe string
|
||
14 years ago
|
*/
|
||
14 years ago
|
function __construct($type, $userid, $prefix='', $ttl=0, $packed=true)
|
||
14 years ago
|
{
|
||
13 years ago
|
$rcube = rcube::get_instance();
|
||
|
$type = strtolower($type);
|
||
14 years ago
|
|
||
14 years ago
|
if ($type == 'memcache') {
|
||
14 years ago
|
$this->type = 'memcache';
|
||
13 years ago
|
$this->db = $rcube->get_memcache();
|
||
14 years ago
|
}
|
||
14 years ago
|
else if ($type == 'apc') {
|
||
|
$this->type = 'apc';
|
||
|
$this->db = function_exists('apc_exists'); // APC 3.1.4 required
|
||
|
}
|
||
14 years ago
|
else {
|
||
|
$this->type = 'db';
|
||
13 years ago
|
$this->db = $rcube->get_dbh();
|
||
14 years ago
|
}
|
||
|
|
||
13 years ago
|
// convert ttl string to seconds
|
||
|
$ttl = get_offset_sec($ttl);
|
||
|
if ($ttl > 2592000) $ttl = 2592000;
|
||
|
|
||
14 years ago
|
$this->userid = (int) $userid;
|
||
13 years ago
|
$this->ttl = $ttl;
|
||
14 years ago
|
$this->packed = $packed;
|
||
|
$this->prefix = $prefix;
|
||
14 years ago
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Returns cached value.
|
||
|
*
|
||
14 years ago
|
* @param string $key Cache key name
|
||
14 years ago
|
*
|
||
|
* @return mixed Cached value
|
||
|
*/
|
||
|
function get($key)
|
||
|
{
|
||
14 years ago
|
if (!array_key_exists($key, $this->cache)) {
|
||
|
return $this->read_record($key);
|
||
|
}
|
||
14 years ago
|
|
||
|
return $this->cache[$key];
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Sets (add/update) value in cache.
|
||
|
*
|
||
14 years ago
|
* @param string $key Cache key name
|
||
|
* @param mixed $data Cache data
|
||
14 years ago
|
*/
|
||
|
function set($key, $data)
|
||
|
{
|
||
|
$this->cache[$key] = $data;
|
||
|
$this->cache_changed = true;
|
||
|
$this->cache_changes[$key] = true;
|
||
|
}
|
||
|
|
||
|
|
||
14 years ago
|
/**
|
||
|
* Returns cached value without storing it in internal memory.
|
||
|
*
|
||
|
* @param string $key Cache key name
|
||
|
*
|
||
|
* @return mixed Cached value
|
||
|
*/
|
||
|
function read($key)
|
||
|
{
|
||
|
if (array_key_exists($key, $this->cache)) {
|
||
|
return $this->cache[$key];
|
||
|
}
|
||
|
|
||
|
return $this->read_record($key, true);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Sets (add/update) value in cache and immediately saves
|
||
|
* it in the backend, no internal memory will be used.
|
||
|
*
|
||
|
* @param string $key Cache key name
|
||
|
* @param mixed $data Cache data
|
||
|
*
|
||
|
* @param boolean True on success, False on failure
|
||
|
*/
|
||
|
function write($key, $data)
|
||
|
{
|
||
|
return $this->write_record($key, $this->packed ? serialize($data) : $data);
|
||
|
}
|
||
|
|
||
|
|
||
14 years ago
|
/**
|
||
|
* Clears the cache.
|
||
|
*
|
||
14 years ago
|
* @param string $key Cache key name or pattern
|
||
|
* @param boolean $prefix_mode Enable it to clear all keys starting
|
||
|
* with prefix specified in $key
|
||
14 years ago
|
*/
|
||
14 years ago
|
function remove($key=null, $prefix_mode=false)
|
||
14 years ago
|
{
|
||
14 years ago
|
// Remove all keys
|
||
14 years ago
|
if ($key === null) {
|
||
|
$this->cache = array();
|
||
|
$this->cache_changed = false;
|
||
|
$this->cache_changes = array();
|
||
12 years ago
|
$this->cache_sums = array();
|
||
14 years ago
|
}
|
||
14 years ago
|
// Remove keys by name prefix
|
||
14 years ago
|
else if ($prefix_mode) {
|
||
14 years ago
|
foreach (array_keys($this->cache) as $k) {
|
||
14 years ago
|
if (strpos($k, $key) === 0) {
|
||
|
$this->cache[$k] = null;
|
||
14 years ago
|
$this->cache_changes[$k] = false;
|
||
12 years ago
|
unset($this->cache_sums[$k]);
|
||
14 years ago
|
}
|
||
|
}
|
||
|
}
|
||
14 years ago
|
// Remove one key by name
|
||
|
else {
|
||
|
$this->cache[$key] = null;
|
||
|
$this->cache_changes[$key] = false;
|
||
12 years ago
|
unset($this->cache_sums[$key]);
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
14 years ago
|
// Remove record(s) from the backend
|
||
|
$this->remove_record($key, $prefix_mode);
|
||
14 years ago
|
}
|
||
|
|
||
|
|
||
13 years ago
|
/**
|
||
|
* Remove cache records older than ttl
|
||
|
*/
|
||
|
function expunge()
|
||
|
{
|
||
|
if ($this->type == 'db' && $this->db) {
|
||
|
$this->db->query(
|
||
13 years ago
|
"DELETE FROM ".$this->db->table_name('cache').
|
||
13 years ago
|
" WHERE user_id = ?".
|
||
|
" AND cache_key LIKE ?".
|
||
|
" AND " . $this->db->unixtimestamp('created')." < ?",
|
||
|
$this->userid,
|
||
|
$this->prefix.'.%',
|
||
|
time() - $this->ttl);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
14 years ago
|
/**
|
||
|
* Writes the cache back to the DB.
|
||
|
*/
|
||
|
function close()
|
||
|
{
|
||
|
if (!$this->cache_changed) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
14 years ago
|
foreach ($this->cache as $key => $data) {
|
||
|
// The key has been used
|
||
|
if ($this->cache_changes[$key]) {
|
||
|
// Make sure we're not going to write unchanged data
|
||
|
// by comparing current md5 sum with the sum calculated on DB read
|
||
14 years ago
|
$data = $this->packed ? serialize($data) : $data;
|
||
14 years ago
|
|
||
|
if (!$this->cache_sums[$key] || $this->cache_sums[$key] != md5($data)) {
|
||
|
$this->write_record($key, $data);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$this->write_index();
|
||
14 years ago
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
14 years ago
|
* Reads cache entry.
|
||
|
*
|
||
14 years ago
|
* @param string $key Cache key name
|
||
|
* @param boolean $nostore Enable to skip in-memory store
|
||
14 years ago
|
*
|
||
|
* @return mixed Cached value
|
||
14 years ago
|
*/
|
||
14 years ago
|
private function read_record($key, $nostore=false)
|
||
14 years ago
|
{
|
||
|
if (!$this->db) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
13 years ago
|
if ($this->type != 'db') {
|
||
|
if ($this->type == 'memcache') {
|
||
|
$data = $this->db->get($this->ckey($key));
|
||
|
}
|
||
|
else if ($this->type == 'apc') {
|
||
|
$data = apc_fetch($this->ckey($key));
|
||
12 years ago
|
}
|
||
14 years ago
|
|
||
13 years ago
|
if ($data) {
|
||
|
$md5sum = md5($data);
|
||
|
$data = $this->packed ? unserialize($data) : $data;
|
||
14 years ago
|
|
||
13 years ago
|
if ($nostore) {
|
||
|
return $data;
|
||
|
}
|
||
14 years ago
|
|
||
13 years ago
|
$this->cache_sums[$key] = $md5sum;
|
||
|
$this->cache[$key] = $data;
|
||
|
}
|
||
13 years ago
|
else {
|
||
13 years ago
|
$this->cache[$key] = null;
|
||
|
}
|
||
14 years ago
|
}
|
||
13 years ago
|
else {
|
||
14 years ago
|
$sql_result = $this->db->limitquery(
|
||
12 years ago
|
"SELECT data, cache_key".
|
||
13 years ago
|
" FROM ".$this->db->table_name('cache').
|
||
14 years ago
|
" WHERE user_id = ?".
|
||
14 years ago
|
" AND cache_key = ?".
|
||
|
// for better performance we allow more records for one key
|
||
|
// get the newer one
|
||
|
" ORDER BY created DESC",
|
||
|
0, 1, $this->userid, $this->prefix.'.'.$key);
|
||
14 years ago
|
|
||
14 years ago
|
if ($sql_arr = $this->db->fetch_assoc($sql_result)) {
|
||
14 years ago
|
$key = substr($sql_arr['cache_key'], strlen($this->prefix)+1);
|
||
|
$md5sum = $sql_arr['data'] ? md5($sql_arr['data']) : null;
|
||
14 years ago
|
if ($sql_arr['data']) {
|
||
|
$data = $this->packed ? unserialize($sql_arr['data']) : $sql_arr['data'];
|
||
|
}
|
||
|
|
||
|
if ($nostore) {
|
||
|
return $data;
|
||
|
}
|
||
|
|
||
14 years ago
|
$this->cache[$key] = $data;
|
||
12 years ago
|
$this->cache_sums[$key] = $md5sum;
|
||
14 years ago
|
}
|
||
13 years ago
|
else {
|
||
13 years ago
|
$this->cache[$key] = null;
|
||
|
}
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
return $this->cache[$key];
|
||
14 years ago
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
14 years ago
|
* Writes single cache record into DB.
|
||
14 years ago
|
*
|
||
14 years ago
|
* @param string $key Cache key name
|
||
|
* @param mxied $data Serialized cache data
|
||
14 years ago
|
*
|
||
|
* @param boolean True on success, False on failure
|
||
14 years ago
|
*/
|
||
14 years ago
|
private function write_record($key, $data)
|
||
14 years ago
|
{
|
||
|
if (!$this->db) {
|
||
|
return false;
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
|
if ($this->type == 'memcache' || $this->type == 'apc') {
|
||
14 years ago
|
return $this->add_record($this->ckey($key), $data);
|
||
|
}
|
||
14 years ago
|
|
||
12 years ago
|
$key_exists = array_key_exists($key, $this->cache_sums);
|
||
14 years ago
|
$key = $this->prefix . '.' . $key;
|
||
14 years ago
|
|
||
14 years ago
|
// Remove NULL rows (here we don't need to check if the record exist)
|
||
|
if ($data == 'N;') {
|
||
|
$this->db->query(
|
||
13 years ago
|
"DELETE FROM ".$this->db->table_name('cache').
|
||
14 years ago
|
" WHERE user_id = ?".
|
||
|
" AND cache_key = ?",
|
||
|
$this->userid, $key);
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
// update existing cache record
|
||
|
if ($key_exists) {
|
||
14 years ago
|
$result = $this->db->query(
|
||
13 years ago
|
"UPDATE ".$this->db->table_name('cache').
|
||
14 years ago
|
" SET created = ". $this->db->now().", data = ?".
|
||
|
" WHERE user_id = ?".
|
||
|
" AND cache_key = ?",
|
||
|
$data, $this->userid, $key);
|
||
|
}
|
||
|
// add new cache record
|
||
|
else {
|
||
|
// for better performance we allow more records for one key
|
||
|
// so, no need to check if record exist (see rcube_cache::read_record())
|
||
14 years ago
|
$result = $this->db->query(
|
||
13 years ago
|
"INSERT INTO ".$this->db->table_name('cache').
|
||
14 years ago
|
" (created, user_id, cache_key, data)".
|
||
|
" VALUES (".$this->db->now().", ?, ?, ?)",
|
||
|
$this->userid, $key, $data);
|
||
|
}
|
||
14 years ago
|
|
||
14 years ago
|
return $this->db->affected_rows($result);
|
||
14 years ago
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Deletes the cache record(s).
|
||
|
*
|
||
|
* @param string $key Cache key name or pattern
|
||
|
* @param boolean $prefix_mode Enable it to clear all keys starting
|
||
|
* with prefix specified in $key
|
||
|
*
|
||
|
*/
|
||
|
private function remove_record($key=null, $prefix_mode=false)
|
||
|
{
|
||
|
if (!$this->db) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if ($this->type != 'db') {
|
||
|
$this->load_index();
|
||
|
|
||
|
// Remove all keys
|
||
|
if ($key === null) {
|
||
|
foreach ($this->index as $key) {
|
||
|
$this->delete_record($key, false);
|
||
|
}
|
||
|
$this->index = array();
|
||
|
}
|
||
|
// Remove keys by name prefix
|
||
|
else if ($prefix_mode) {
|
||
|
foreach ($this->index as $k) {
|
||
|
if (strpos($k, $key) === 0) {
|
||
|
$this->delete_record($k);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
// Remove one key by name
|
||
|
else {
|
||
|
$this->delete_record($key);
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
// Remove all keys (in specified cache)
|
||
|
if ($key === null) {
|
||
|
$where = " AND cache_key LIKE " . $this->db->quote($this->prefix.'.%');
|
||
|
}
|
||
|
// Remove keys by name prefix
|
||
|
else if ($prefix_mode) {
|
||
|
$where = " AND cache_key LIKE " . $this->db->quote($this->prefix.'.'.$key.'%');
|
||
|
}
|
||
|
// Remove one key by name
|
||
|
else {
|
||
|
$where = " AND cache_key = " . $this->db->quote($this->prefix.'.'.$key);
|
||
|
}
|
||
|
|
||
|
$this->db->query(
|
||
13 years ago
|
"DELETE FROM ".$this->db->table_name('cache').
|
||
14 years ago
|
" WHERE user_id = ?" . $where,
|
||
|
$this->userid);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Adds entry into memcache/apc DB.
|
||
14 years ago
|
*
|
||
|
* @param string $key Cache key name
|
||
|
* @param mxied $data Serialized cache data
|
||
|
* @param bollean $index Enables immediate index update
|
||
|
*
|
||
|
* @param boolean True on success, False on failure
|
||
14 years ago
|
*/
|
||
14 years ago
|
private function add_record($key, $data, $index=false)
|
||
14 years ago
|
{
|
||
|
if ($this->type == 'memcache') {
|
||
14 years ago
|
$result = $this->db->replace($key, $data, MEMCACHE_COMPRESSED, $this->ttl);
|
||
14 years ago
|
if (!$result)
|
||
14 years ago
|
$result = $this->db->set($key, $data, MEMCACHE_COMPRESSED, $this->ttl);
|
||
14 years ago
|
}
|
||
14 years ago
|
else if ($this->type == 'apc') {
|
||
14 years ago
|
if (apc_exists($key))
|
||
|
apc_delete($key);
|
||
14 years ago
|
$result = apc_store($key, $data, $this->ttl);
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
|
// Update index
|
||
|
if ($index && $result) {
|
||
|
$this->load_index();
|
||
|
|
||
|
if (array_search($key, $this->index) === false) {
|
||
|
$this->index[] = $key;
|
||
|
$data = serialize($this->index);
|
||
|
$this->add_record($this->ikey(), $data);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return $result;
|
||
14 years ago
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Deletes entry from memcache/apc DB.
|
||
|
*/
|
||
14 years ago
|
private function delete_record($key, $index=true)
|
||
14 years ago
|
{
|
||
12 years ago
|
if ($this->type == 'memcache') {
|
||
|
// #1488592: use 2nd argument
|
||
|
$this->db->delete($this->ckey($key), 0);
|
||
|
}
|
||
|
else {
|
||
14 years ago
|
apc_delete($this->ckey($key));
|
||
12 years ago
|
}
|
||
14 years ago
|
|
||
|
if ($index) {
|
||
|
if (($idx = array_search($key, $this->index)) !== false) {
|
||
|
unset($this->index[$idx]);
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
|
|
||
14 years ago
|
/**
|
||
|
* Writes the index entry into memcache/apc DB.
|
||
|
*/
|
||
|
private function write_index()
|
||
|
{
|
||
|
if (!$this->db) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if ($this->type == 'db') {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
$this->load_index();
|
||
|
|
||
|
// Make sure index contains new keys
|
||
|
foreach ($this->cache as $key => $value) {
|
||
|
if ($value !== null) {
|
||
|
if (array_search($key, $this->index) === false) {
|
||
|
$this->index[] = $key;
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
$data = serialize($this->index);
|
||
|
$this->add_record($this->ikey(), $data);
|
||
14 years ago
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
14 years ago
|
* Gets the index entry from memcache/apc DB.
|
||
14 years ago
|
*/
|
||
14 years ago
|
private function load_index()
|
||
14 years ago
|
{
|
||
|
if (!$this->db) {
|
||
14 years ago
|
return;
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
if ($this->index !== null) {
|
||
|
return;
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
|
$index_key = $this->ikey();
|
||
|
if ($this->type == 'memcache') {
|
||
|
$data = $this->db->get($index_key);
|
||
|
}
|
||
|
else if ($this->type == 'apc') {
|
||
|
$data = apc_fetch($index_key);
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
|
$this->index = $data ? unserialize($data) : array();
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Creates per-user cache key name (for memcache and apc)
|
||
|
*
|
||
|
* @param string $key Cache key name
|
||
|
*
|
||
|
* @return string Cache key
|
||
|
*/
|
||
|
private function ckey($key)
|
||
|
{
|
||
|
return sprintf('%d:%s:%s', $this->userid, $this->prefix, $key);
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
|
||
|
/**
|
||
14 years ago
|
* Creates per-user index cache key name (for memcache and apc)
|
||
14 years ago
|
*
|
||
14 years ago
|
* @return string Cache key
|
||
14 years ago
|
*/
|
||
14 years ago
|
private function ikey()
|
||
14 years ago
|
{
|
||
14 years ago
|
// This way each cache will have its own index
|
||
|
return sprintf('%d:%s%s', $this->userid, $this->prefix, 'INDEX');
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|