/usr/share/php/Horde/Cache/Storage/Mongo.php is in php-horde-cache 2.5.0-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 | <?php
/**
* Copyright 2013-2014 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 2013-2014 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Cache
*/
/**
* Cache storage in a MongoDB database.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013-2014 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Cache
*/
class Horde_Cache_Storage_Mongo extends Horde_Cache_Storage_Base
{
/* Field names. */
const CID = 'cid';
const DATA = 'data';
const EXPIRE = 'expire';
const TIMESTAMP = 'ts';
/**
* The MongoDB Collection object for the cache data.
*
* @var MongoCollection
*/
protected $_db;
/**
* Constructor.
*
* @param array $params Parameters:
* <pre>
* - collection: (string) The collection name.
* - mongo_db: [REQUIRED] (Horde_Mongo_Client) A MongoDB client object.
* </pre>
*/
public function __construct(array $params = array())
{
if (!isset($params['mongo_db'])) {
throw new InvalidArgumentException('Missing mongo_db parameter.');
}
parent::__construct(array_merge(array(
'collection' => 'horde_cache'
), $params));
}
/**
* Destructor.
*/
public function __destruct()
{
/* Only do garbage collection 0.1% of the time we create an object. */
if (substr(time(), -3) === '000') {
try {
$this->_db->remove(array(
self::EXPIRE => array(
'$exists' => true,
'$lt' => time()
)
));
} catch (MongoException $e) {
$this->_logger->log($e->getMessage(), 'DEBUG');
}
}
}
/**
*/
protected function _initOb()
{
$this->_db = $this->_params['mongo_db']->selectCollection(null, $this->_params['collection']);
}
/**
*/
public function get($key, $lifetime = 0)
{
$okey = $key;
$key = $this->_getCid($key);
/* Build SQL query. */
$query = array(
self::CID => $key
);
// 0 lifetime checks for objects which have no expiration
if ($lifetime != 0) {
$query[self::TIMESTAMP] = array('$gte' => time() - $lifetime);
}
try {
$result = $this->_db->findOne($query, array(self::DATA));
} catch (MongoException $e) {
$this->_logger->log($e->getMessage(), 'DEBUG');
return false;
}
if (empty($result)) {
/* No rows were found - cache miss */
if ($this->_logger) {
$this->_logger->log(sprintf('Cache miss: %s (cache ID %s)', $okey, $key), 'DEBUG');
}
return false;
}
if ($this->_logger) {
$this->_logger->log(sprintf('Cache hit: %s (cache ID %s)', $okey, $key), 'DEBUG');
}
return $result[self::DATA]->bin;
}
/**
*/
public function set($key, $data, $lifetime = 0)
{
$okey = $key;
$key = $this->_getCid($key);
$curr = time();
$data = array(
self::CID => $key,
self::DATA => new MongoBinData($data, MongoBinData::BYTE_ARRAY),
self::TIMESTAMP => $curr
);
// 0 lifetime indicates the object should not be GC'd.
if (!empty($lifetime)) {
$data[self::EXPIRE] = intval($lifetime) + $curr;
}
if ($this->_logger) {
$this->_logger->log(sprintf(
'Cache set: %s (id %s set at %s%s)',
$okey,
$key,
date('r', $curr),
(isset($data[self::EXPIRE]) ? ' expires at ' . date('r', $data[self::EXPIRE]) : '')
), 'DEBUG');
}
// Remove any old cache data and prevent duplicate keys
try {
$this->_db->update(array(
self::CID => $key
), array(
'$set' => $data
), array(
'upsert' => true,
'w' => 0
));
} catch (MongoException $e) {
$this->_logger->log($e->getMessage(), 'DEBUG');
return false;
}
}
/**
*/
public function exists($key, $lifetime = 0)
{
$okey = $key;
$key = $this->_getCid($key);
/* Build SQL query. */
$query = array(
self::CID => $key
);
// 0 lifetime checks for objects which have no expiration
if ($lifetime != 0) {
$query[self::TIMESTAMP] = array('$gte' => time() - $lifetime);
}
try {
$result = $this->_db->findOne($query);
} catch (MongoException $e) {
$this->_logger->log($e->getMessage(), 'DEBUG');
return false;
}
if (is_null($result)) {
if ($this->_logger) {
$this->_logger->log(sprintf('Cache exists() miss: %s (cache ID %s)', $okey, $key), 'DEBUG');
}
return false;
}
if ($this->_logger) {
$this->_logger->log(sprintf('Cache exists() hit: %s (cache ID %s)', $okey, $key), 'DEBUG');
}
return true;
}
/**
*/
public function expire($key)
{
$okey = $key;
$key = $this->_getCid($key);
try {
$this->_db->remove(array(
self::CID => $key
));
$this->_logger->log(sprintf('Cache expire: %s (cache ID %s)', $okey, $key), 'DEBUG');
return true;
} catch (MongoException $e) {
return false;
}
}
/**
*/
public function clear()
{
$this->_db->drop();
}
/**
* Gets the cache ID for a key.
*
* @param string $key The key.
*
* @return string The cache ID.
*/
protected function _getCid($key)
{
return hash('md5', $key);
}
}
|