/usr/share/php/Horde/Cache/Storage/Sql.php is in php-horde-cache 2.5.2-1build1.
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 | <?php
/**
* Copyright 2007-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2007-2016 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Cache
*/
/**
* Cache storage in a SQL databsae.
*
* The table structure for the cache is as follows:
* <pre>
* CREATE TABLE horde_cache (
* cache_id VARCHAR(32) NOT NULL,
* cache_timestamp BIGINT NOT NULL,
* cache_data LONGBLOB,
* (Or on PostgreSQL:)
* cache_data TEXT,
* (Or on some other DBMS systems:)
* cache_data IMAGE,
*
* PRIMARY KEY (cache_id)
* );
* </pre>
*
* @author Ben Klang <ben@alkaloid.net>
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2007-2016 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Cache
*/
class Horde_Cache_Storage_Sql extends Horde_Cache_Storage_Base
{
/**
* Handle for the current database connection.
*
* @var Horde_Db_Adapter
*/
protected $_db;
/**
* Constructor.
*
* @param array $params Parameters:
* <pre>
* - db: (Horde_Db_Adapter) [REQUIRED] The DB instance.
* - table: (string) The name of the cache table.
* DEFAULT: 'horde_cache'
* </pre>
*/
public function __construct($params = array())
{
if (!isset($params['db'])) {
throw new InvalidArgumentException('Missing db parameter.');
}
parent::__construct(array_merge(array(
'table' => 'horde_cache',
), $params));
}
/**
*/
protected function _initOb()
{
$this->_db = $this->_params['db'];
}
/**
* Destructor.
*/
public function __destruct()
{
/* Only do garbage collection 0.1% of the time we create an object. */
if (substr(time(), -3) !== '000') {
return;
}
$query = 'DELETE FROM ' . $this->_params['table'] .
' WHERE cache_expiration < ? AND cache_expiration <> 0';
$values = array(time());
try {
$this->_db->delete($query, $values);
} catch (Horde_Db_Exception $e) {}
}
/**
*/
public function get($key, $lifetime = 0)
{
$okey = $key;
$key = hash('md5', $key);
$timestamp = time();
$maxage = $timestamp - $lifetime;
/* Build SQL query. */
$query = 'SELECT cache_data FROM ' . $this->_params['table'] .
' WHERE cache_id = ?';
$values = array($key);
// 0 lifetime checks for objects which have no expiration
if ($lifetime != 0) {
$query .= ' AND cache_timestamp >= ?';
$values[] = $maxage;
}
try {
$result = $this->_db->selectValue($query, $values);
$columns = $this->_db->columns($this->_params['table']);
} catch (Horde_Db_Exception $e) {
return false;
}
if (!$result) {
/* No rows were found - cache miss */
if ($this->_logger) {
$this->_logger->log(sprintf('Cache miss: %s (Id %s newer than %d)', $okey, $key, $maxage), 'DEBUG');
}
return false;
}
if ($this->_logger) {
$this->_logger->log(sprintf('Cache hit: %s (Id %s newer than %d)', $okey, $key, $maxage), 'DEBUG');
}
return $columns['cache_data']->binaryToString($result);
}
/**
*/
public function set($key, $data, $lifetime = 0)
{
$okey = $key;
$key = hash('md5', $key);
$timestamp = time();
// 0 lifetime indicates the object should not be GC'd.
$expiration = ($lifetime === 0)
? 0
: ($lifetime + $timestamp);
if ($this->_logger) {
$this->_logger->log(sprintf('Cache set: %s (Id %s set at %d expires at %d)', $okey, $key, $timestamp, $expiration), 'DEBUG');
}
// Remove any old cache data and prevent duplicate keys
$query = 'DELETE FROM ' . $this->_params['table'] . ' WHERE cache_id = ?';
$values = array($key);
try {
$this->_db->delete($query, $values);
} catch (Horde_Db_Exception $e) {}
/* Build SQL query. */
$values = array(
'cache_id' => $key,
'cache_timestamp' => $timestamp,
'cache_expiration' => $expiration,
'cache_data' => new Horde_Db_Value_Binary($data)
);
try {
$this->_db->insertBlob($this->_params['table'], $values);
} catch (Horde_Db_Exception $e) {
throw new Horde_Cache_Exception($e);
}
}
/**
*/
public function exists($key, $lifetime = 0)
{
$okey = $key;
$key = hash('md5', $key);
/* Build SQL query. */
$query = 'SELECT 1 FROM ' . $this->_params['table'] .
' WHERE cache_id = ?';
$values = array($key);
// 0 lifetime checks for objects which have no expiration
if ($lifetime != 0) {
$query .= ' AND cache_timestamp >= ?';
$values[] = time() - $lifetime;
}
try {
$result = $this->_db->selectValue($query, $values);
} catch (Horde_Db_Exception $e) {
return false;
}
$timestamp = time();
if (empty($result)) {
if ($this->_logger) {
$this->_logger->log(sprintf('Cache exists() miss: %s (Id %s newer than %d)', $okey, $key, $timestamp), 'DEBUG');
}
return false;
}
if ($this->_logger) {
$this->_logger->log(sprintf('Cache exists() hit: %s (Id %s newer than %d)', $okey, $key, $timestamp), 'DEBUG');
}
return true;
}
/**
*/
public function expire($key)
{
$key = hash('md5', $key);
$query = 'DELETE FROM ' . $this->_params['table'] .
' WHERE cache_id = ?';
$values = array($key);
try {
$this->_db->delete($query, $values);
} catch (Horde_Db_Exception $e) {
return false;
}
return true;
}
/**
*/
public function clear()
{
$query = 'DELETE FROM ' . $this->_params['table'];
try {
$this->_db->delete($query);
} catch (Horde_Db_Exception $e) {
throw new Horde_Cache_Exception($e);
}
}
}
|