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

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2014-04-13 09:11:33 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20140413091133-um8xxmlwuawkhbi5
Tags: 2.4.2-1
New upstream version 2.4.2

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
 
     *   - db: (Horde_Db_Adapter) [REQUIRED] The DB instance.
53
 
     *   - table: (string) The name of the cache table.
54
 
     *            DEFAULT: 'horde_cache'
55
 
     */
56
 
    public function __construct($params = array())
57
 
    {
58
 
        if (!isset($params['db'])) {
59
 
            throw new InvalidArgumentException('Missing db parameter.');
60
 
        }
61
 
 
62
 
        parent::__construct(array_merge(array(
63
 
            'table' => 'horde_cache',
64
 
        ), $params));
65
 
    }
66
 
 
67
 
    /**
68
 
     */
69
 
    protected function _initOb()
70
 
    {
71
 
        $this->_db = $this->_params['db'];
72
 
    }
73
 
 
74
 
    /**
75
 
     * Destructor.
76
 
     */
77
 
    public function __destruct()
78
 
    {
79
 
        /* Only do garbage collection 0.1% of the time we create an object. */
80
 
        if (substr(time(), -3) !== '000') {
81
 
            return;
82
 
        }
83
 
 
84
 
        $query = 'DELETE FROM ' . $this->_params['table'] .
85
 
                 ' WHERE cache_expiration < ? AND cache_expiration <> 0';
86
 
        $values = array(time());
87
 
 
88
 
        try {
89
 
            $this->_db->delete($query, $values);
90
 
        } catch (Horde_Db_Exception $e) {}
91
 
    }
92
 
 
93
 
    /**
94
 
     */
95
 
    public function get($key, $lifetime = 0)
96
 
    {
97
 
        $okey = $key;
98
 
        $key = hash('md5', $key);
99
 
 
100
 
        $timestamp = time();
101
 
        $maxage = $timestamp - $lifetime;
102
 
 
103
 
        /* Build SQL query. */
104
 
        $query = 'SELECT cache_data FROM ' . $this->_params['table'] .
105
 
                 ' WHERE cache_id = ?';
106
 
        $values = array($key);
107
 
 
108
 
        // 0 lifetime checks for objects which have no expiration
109
 
        if ($lifetime != 0) {
110
 
            $query .= ' AND cache_timestamp >= ?';
111
 
            $values[] = $maxage;
112
 
        }
113
 
 
114
 
        try {
115
 
            $result = $this->_db->selectValue($query, $values);
116
 
        } catch (Horde_Db_Exception $e) {
117
 
            return false;
118
 
        }
119
 
 
120
 
        if (!$result) {
121
 
            /* No rows were found - cache miss */
122
 
            if ($this->_logger) {
123
 
                $this->_logger->log(sprintf('Cache miss: %s (Id %s newer than %d)', $okey, $key, $maxage), 'DEBUG');
124
 
            }
125
 
            return false;
126
 
        }
127
 
 
128
 
        if ($this->_logger) {
129
 
            $this->_logger->log(sprintf('Cache hit: %s (Id %s newer than %d)', $okey, $key, $maxage), 'DEBUG');
130
 
        }
131
 
 
132
 
        return $result;
133
 
    }
134
 
 
135
 
    /**
136
 
     */
137
 
    public function set($key, $data, $lifetime = 0)
138
 
    {
139
 
        $okey = $key;
140
 
        $key = hash('md5', $key);
141
 
 
142
 
        $timestamp = time();
143
 
 
144
 
        // 0 lifetime indicates the object should not be GC'd.
145
 
        $expiration = ($lifetime === 0)
146
 
            ? 0
147
 
            : ($lifetime + $timestamp);
148
 
 
149
 
        if ($this->_logger) {
150
 
            $this->_logger->log(sprintf('Cache set: %s (Id %s set at %d expires at %d)', $okey, $key, $timestamp, $expiration), 'DEBUG');
151
 
        }
152
 
 
153
 
        // Remove any old cache data and prevent duplicate keys
154
 
        $query = 'DELETE FROM ' . $this->_params['table'] . ' WHERE cache_id=?';
155
 
        $values = array($key);
156
 
        try {
157
 
            $this->_db->delete($query, $values);
158
 
        } catch (Horde_Db_Exception $e) {}
159
 
 
160
 
        /* Build SQL query. */
161
 
        $query = 'INSERT INTO ' . $this->_params['table'] .
162
 
                 ' (cache_id, cache_timestamp, cache_expiration, cache_data)' .
163
 
                 ' VALUES (?, ?, ?, ?)';
164
 
        $values = array($key, $timestamp, $expiration, $data);
165
 
 
166
 
        try {
167
 
            $this->_db->insert($query, $values);
168
 
        } catch (Horde_Db_Exception $e) {
169
 
            throw new Horde_Cache_Exception($e);
170
 
        }
171
 
    }
172
 
 
173
 
    /**
174
 
     */
175
 
    public function exists($key, $lifetime = 0)
176
 
    {
177
 
        $okey = $key;
178
 
        $key = hash('md5', $key);
179
 
 
180
 
        /* Build SQL query. */
181
 
        $query = 'SELECT 1 FROM ' . $this->_params['table'] .
182
 
                 ' WHERE cache_id = ?';
183
 
        $values = array($key);
184
 
 
185
 
        // 0 lifetime checks for objects which have no expiration
186
 
        if ($lifetime != 0) {
187
 
            $query .= ' AND cache_timestamp >= ?';
188
 
            $values[] = time() - $lifetime;
189
 
        }
190
 
 
191
 
        try {
192
 
            $result = $this->_db->selectValue($query, $values);
193
 
        } catch (Horde_Db_Exception $e) {
194
 
            return false;
195
 
        }
196
 
 
197
 
        $timestamp = time();
198
 
        if (empty($result)) {
199
 
            if ($this->_logger) {
200
 
                $this->_logger->log(sprintf('Cache exists() miss: %s (Id %s newer than %d)', $okey, $key, $timestamp), 'DEBUG');
201
 
            }
202
 
            return false;
203
 
        }
204
 
 
205
 
        if ($this->_logger) {
206
 
            $this->_logger->log(sprintf('Cache exists() hit: %s (Id %s newer than %d)', $okey, $key, $timestamp), 'DEBUG');
207
 
        }
208
 
 
209
 
        return true;
210
 
    }
211
 
 
212
 
    /**
213
 
     */
214
 
    public function expire($key)
215
 
    {
216
 
        $key = hash('md5', $key);
217
 
 
218
 
        $query = 'DELETE FROM ' . $this->_params['table'] .
219
 
                 ' WHERE cache_id = ?';
220
 
        $values = array($key);
221
 
 
222
 
        try {
223
 
            $this->_db->delete($query, $values);
224
 
        } catch (Horde_Db_Exception $e) {
225
 
            return false;
226
 
        }
227
 
 
228
 
        return true;
229
 
    }
230
 
 
231
 
    /**
232
 
     */
233
 
    public function clear()
234
 
    {
235
 
        $query = 'DELETE FROM ' . $this->_params['table'];
236
 
 
237
 
        try {
238
 
            $this->_db->delete($query);
239
 
        } catch (Horde_Db_Exception $e) {
240
 
            throw new Horde_Cache_Exception($e);
241
 
        }
242
 
    }
243
 
 
244
 
}