~ubuntu-branches/ubuntu/wily/php-horde-cache/wily

« back to all changes in this revision

Viewing changes to Horde_Cache-2.5.0/lib/Horde/Cache/Storage/Sql.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2014-05-17 11:10:04 UTC
  • mfrom: (1.1.11)
  • Revision ID: package-import@ubuntu.com-20140517111004-sivxqdkho9p34xfy
Tags: 2.5.0-1
New upstream version 2.5.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Copyright 2007-2014 Horde LLC (http://www.horde.org/)
 
4
 *
 
5
 * See the enclosed file COPYING for license information (LGPL). If you
 
6
 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 
7
 *
 
8
 * @category  Horde
 
9
 * @copyright 2007-2014 Horde LLC
 
10
 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
 
11
 * @package   Cache
 
12
 */
 
13
 
 
14
/**
 
15
 * Cache storage in a SQL databsae.
 
16
 *
 
17
 * The table structure for the cache is as follows:
 
18
 * <pre>
 
19
 * CREATE TABLE horde_cache (
 
20
 *     cache_id          VARCHAR(32) NOT NULL,
 
21
 *     cache_timestamp   BIGINT NOT NULL,
 
22
 *     cache_data        LONGBLOB,
 
23
 *     (Or on PostgreSQL:)
 
24
 *     cache_data        TEXT,
 
25
 *     (Or on some other DBMS systems:)
 
26
 *     cache_data        IMAGE,
 
27
 *
 
28
 *     PRIMARY KEY (cache_id)
 
29
 * );
 
30
 * </pre>
 
31
 *
 
32
 * @author    Ben Klang <ben@alkaloid.net>
 
33
 * @author    Michael Slusarz <slusarz@horde.org>
 
34
 * @category  Horde
 
35
 * @copyright 2007-2014 Horde LLC
 
36
 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
 
37
 * @package   Cache
 
38
 */
 
39
class Horde_Cache_Storage_Sql extends Horde_Cache_Storage_Base
 
40
{
 
41
    /**
 
42
     * Handle for the current database connection.
 
43
     *
 
44
     * @var Horde_Db_Adapter
 
45
     */
 
46
    protected $_db;
 
47
 
 
48
    /**
 
49
     * Constructor.
 
50
     *
 
51
     * @param array $params  Parameters:
 
52
     * <pre>
 
53
     *   - db: (Horde_Db_Adapter) [REQUIRED] The DB instance.
 
54
     *   - table: (string) The name of the cache table.
 
55
     *            DEFAULT: 'horde_cache'
 
56
     * </pre>
 
57
     */
 
58
    public function __construct($params = array())
 
59
    {
 
60
        if (!isset($params['db'])) {
 
61
            throw new InvalidArgumentException('Missing db parameter.');
 
62
        }
 
63
 
 
64
        parent::__construct(array_merge(array(
 
65
            'table' => 'horde_cache',
 
66
        ), $params));
 
67
    }
 
68
 
 
69
    /**
 
70
     */
 
71
    protected function _initOb()
 
72
    {
 
73
        $this->_db = $this->_params['db'];
 
74
    }
 
75
 
 
76
    /**
 
77
     * Destructor.
 
78
     */
 
79
    public function __destruct()
 
80
    {
 
81
        /* Only do garbage collection 0.1% of the time we create an object. */
 
82
        if (substr(time(), -3) !== '000') {
 
83
            return;
 
84
        }
 
85
 
 
86
        $query = 'DELETE FROM ' . $this->_params['table'] .
 
87
                 ' WHERE cache_expiration < ? AND cache_expiration <> 0';
 
88
        $values = array(time());
 
89
 
 
90
        try {
 
91
            $this->_db->delete($query, $values);
 
92
        } catch (Horde_Db_Exception $e) {}
 
93
    }
 
94
 
 
95
    /**
 
96
     */
 
97
    public function get($key, $lifetime = 0)
 
98
    {
 
99
        $okey = $key;
 
100
        $key = hash('md5', $key);
 
101
 
 
102
        $timestamp = time();
 
103
        $maxage = $timestamp - $lifetime;
 
104
 
 
105
        /* Build SQL query. */
 
106
        $query = 'SELECT cache_data FROM ' . $this->_params['table'] .
 
107
                 ' WHERE cache_id = ?';
 
108
        $values = array($key);
 
109
 
 
110
        // 0 lifetime checks for objects which have no expiration
 
111
        if ($lifetime != 0) {
 
112
            $query .= ' AND cache_timestamp >= ?';
 
113
            $values[] = $maxage;
 
114
        }
 
115
 
 
116
        try {
 
117
            $result = $this->_db->selectValue($query, $values);
 
118
        } catch (Horde_Db_Exception $e) {
 
119
            return false;
 
120
        }
 
121
 
 
122
        if (!$result) {
 
123
            /* No rows were found - cache miss */
 
124
            if ($this->_logger) {
 
125
                $this->_logger->log(sprintf('Cache miss: %s (Id %s newer than %d)', $okey, $key, $maxage), 'DEBUG');
 
126
            }
 
127
            return false;
 
128
        }
 
129
 
 
130
        if ($this->_logger) {
 
131
            $this->_logger->log(sprintf('Cache hit: %s (Id %s newer than %d)', $okey, $key, $maxage), 'DEBUG');
 
132
        }
 
133
 
 
134
        return $result;
 
135
    }
 
136
 
 
137
    /**
 
138
     */
 
139
    public function set($key, $data, $lifetime = 0)
 
140
    {
 
141
        $okey = $key;
 
142
        $key = hash('md5', $key);
 
143
 
 
144
        $timestamp = time();
 
145
 
 
146
        // 0 lifetime indicates the object should not be GC'd.
 
147
        $expiration = ($lifetime === 0)
 
148
            ? 0
 
149
            : ($lifetime + $timestamp);
 
150
 
 
151
        if ($this->_logger) {
 
152
            $this->_logger->log(sprintf('Cache set: %s (Id %s set at %d expires at %d)', $okey, $key, $timestamp, $expiration), 'DEBUG');
 
153
        }
 
154
 
 
155
        // Remove any old cache data and prevent duplicate keys
 
156
        $query = 'DELETE FROM ' . $this->_params['table'] . ' WHERE cache_id=?';
 
157
        $values = array($key);
 
158
        try {
 
159
            $this->_db->delete($query, $values);
 
160
        } catch (Horde_Db_Exception $e) {}
 
161
 
 
162
        /* Build SQL query. */
 
163
        $query = 'INSERT INTO ' . $this->_params['table'] .
 
164
                 ' (cache_id, cache_timestamp, cache_expiration, cache_data)' .
 
165
                 ' VALUES (?, ?, ?, ?)';
 
166
        $values = array($key, $timestamp, $expiration, $data);
 
167
 
 
168
        try {
 
169
            $this->_db->insert($query, $values);
 
170
        } catch (Horde_Db_Exception $e) {
 
171
            throw new Horde_Cache_Exception($e);
 
172
        }
 
173
    }
 
174
 
 
175
    /**
 
176
     */
 
177
    public function exists($key, $lifetime = 0)
 
178
    {
 
179
        $okey = $key;
 
180
        $key = hash('md5', $key);
 
181
 
 
182
        /* Build SQL query. */
 
183
        $query = 'SELECT 1 FROM ' . $this->_params['table'] .
 
184
                 ' WHERE cache_id = ?';
 
185
        $values = array($key);
 
186
 
 
187
        // 0 lifetime checks for objects which have no expiration
 
188
        if ($lifetime != 0) {
 
189
            $query .= ' AND cache_timestamp >= ?';
 
190
            $values[] = time() - $lifetime;
 
191
        }
 
192
 
 
193
        try {
 
194
            $result = $this->_db->selectValue($query, $values);
 
195
        } catch (Horde_Db_Exception $e) {
 
196
            return false;
 
197
        }
 
198
 
 
199
        $timestamp = time();
 
200
        if (empty($result)) {
 
201
            if ($this->_logger) {
 
202
                $this->_logger->log(sprintf('Cache exists() miss: %s (Id %s newer than %d)', $okey, $key, $timestamp), 'DEBUG');
 
203
            }
 
204
            return false;
 
205
        }
 
206
 
 
207
        if ($this->_logger) {
 
208
            $this->_logger->log(sprintf('Cache exists() hit: %s (Id %s newer than %d)', $okey, $key, $timestamp), 'DEBUG');
 
209
        }
 
210
 
 
211
        return true;
 
212
    }
 
213
 
 
214
    /**
 
215
     */
 
216
    public function expire($key)
 
217
    {
 
218
        $key = hash('md5', $key);
 
219
 
 
220
        $query = 'DELETE FROM ' . $this->_params['table'] .
 
221
                 ' WHERE cache_id = ?';
 
222
        $values = array($key);
 
223
 
 
224
        try {
 
225
            $this->_db->delete($query, $values);
 
226
        } catch (Horde_Db_Exception $e) {
 
227
            return false;
 
228
        }
 
229
 
 
230
        return true;
 
231
    }
 
232
 
 
233
    /**
 
234
     */
 
235
    public function clear()
 
236
    {
 
237
        $query = 'DELETE FROM ' . $this->_params['table'];
 
238
 
 
239
        try {
 
240
            $this->_db->delete($query);
 
241
        } catch (Horde_Db_Exception $e) {
 
242
            throw new Horde_Cache_Exception($e);
 
243
        }
 
244
    }
 
245
 
 
246
}