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

« back to all changes in this revision

Viewing changes to Horde_Cache-2.2.1/lib/Horde/Cache/Storage/Memcache.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2013-10-08 18:54:29 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20131008185429-8r3ao97k5z15l325
Tags: 2.3.0-1
New upstream version 2.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
 * Copyright 2006-2013 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 2006-2013 Horde LLC
10
 
 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
11
 
 * @package   Cache
12
 
 */
13
 
 
14
 
/**
15
 
 * Cache storage on a memcache installation.
16
 
 *
17
 
 * @author     Duck <duck@obala.net>
18
 
 * @author     Michael Slusarz <slusarz@horde.org>
19
 
 * @category   Horde
20
 
 * @copyright  2006-2013 Horde LLC
21
 
 * @deprecated Use HashTable driver instead.
22
 
 * @license    http://www.horde.org/licenses/lgpl21 LGPL 2.1
23
 
 * @package    Cache
24
 
 */
25
 
class Horde_Cache_Storage_Memcache extends Horde_Cache_Storage_Base
26
 
{
27
 
    /**
28
 
     * Cache results of expire() calls (since we will get the entire object
29
 
     * on an expire() call anyway).
30
 
     *
31
 
     * @var array
32
 
     */
33
 
    protected $_expirecache = array();
34
 
 
35
 
    /**
36
 
     * Memcache object.
37
 
     *
38
 
     * @var Horde_Memcache
39
 
     */
40
 
    protected $_memcache;
41
 
 
42
 
    /**
43
 
     * Construct a new Horde_Cache_Memcache object.
44
 
     *
45
 
     * @param array $params  Parameter array:
46
 
     *   - memcache: (Horde_Memcache) [REQUIRED] A Horde_Memcache object.
47
 
     *   - prefix: (string) The prefix to use for the cache keys.
48
 
     *             DEFAULT: ''
49
 
     */
50
 
    public function __construct(array $params = array())
51
 
    {
52
 
        if (!isset($params['memcache'])) {
53
 
            if (isset($params['hashtable'])) {
54
 
                $params['memcache'] = $params['hashtable'];
55
 
            } else {
56
 
                throw new InvalidArgumentException('Missing memcache object');
57
 
            }
58
 
        }
59
 
 
60
 
        parent::__construct(array_merge(array(
61
 
            'prefix' => '',
62
 
        ), $params));
63
 
    }
64
 
 
65
 
    /**
66
 
     */
67
 
    protected function _initOb()
68
 
    {
69
 
        $this->_memcache = $this->_params['memcache'];
70
 
    }
71
 
 
72
 
    /**
73
 
     */
74
 
    public function get($key, $lifetime = 0)
75
 
    {
76
 
        $original_key = $key;
77
 
        $key = $this->_params['prefix'] . $key;
78
 
        if (isset($this->_expirecache[$key])) {
79
 
            return $this->_expirecache[$key];
80
 
        }
81
 
 
82
 
        $key_list = array($key);
83
 
        if (!empty($lifetime)) {
84
 
            $key_list[] = $key . '_e';
85
 
        }
86
 
 
87
 
        $res = $this->_memcache->get($key_list);
88
 
 
89
 
        if ($res === false) {
90
 
            unset($this->_expirecache[$key]);
91
 
        } else {
92
 
            // If we can't find the expire time, assume we have exceeded it.
93
 
            if (empty($lifetime) ||
94
 
                (($res[$key . '_e'] !== false) && ($res[$key . '_e'] + $lifetime > time()))) {
95
 
                $this->_expirecache[$key] = $res[$key];
96
 
            } else {
97
 
                $res[$key] = false;
98
 
                $this->expire($original_key);
99
 
            }
100
 
        }
101
 
 
102
 
        return $res[$key];
103
 
    }
104
 
 
105
 
    /**
106
 
     */
107
 
    public function set($key, $data, $lifetime = 0)
108
 
    {
109
 
        $key = $this->_params['prefix'] . $key;
110
 
 
111
 
        if ($this->_memcache->set($key . '_e', time(), $lifetime) !== false) {
112
 
            $this->_memcache->set($key, $data, $lifetime);
113
 
        }
114
 
    }
115
 
 
116
 
    /**
117
 
     */
118
 
    public function exists($key, $lifetime = 0)
119
 
    {
120
 
        return ($this->get($key, $lifetime) !== false);
121
 
    }
122
 
 
123
 
    /**
124
 
     */
125
 
    public function expire($key)
126
 
    {
127
 
        $key = $this->_params['prefix'] . $key;
128
 
        unset($this->_expirecache[$key]);
129
 
        $this->_memcache->delete($key . '_e');
130
 
 
131
 
        return $this->_memcache->delete($key);
132
 
    }
133
 
 
134
 
    /**
135
 
     */
136
 
    public function clear()
137
 
    {
138
 
        $this->_memcache->flush();
139
 
    }
140
 
 
141
 
}