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

« back to all changes in this revision

Viewing changes to Horde_Cache-2.4.2/lib/Horde/Cache/Storage/Memcache.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 2006-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 2006-2014 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-2014 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
 
     * <pre>
47
 
     *   - memcache: (Horde_Memcache) [REQUIRED] A Horde_Memcache object.
48
 
     *   - prefix: (string) The prefix to use for the cache keys.
49
 
     *             DEFAULT: ''
50
 
     * </pre>
51
 
     */
52
 
    public function __construct(array $params = array())
53
 
    {
54
 
        if (!isset($params['memcache'])) {
55
 
            if (isset($params['hashtable'])) {
56
 
                $params['memcache'] = $params['hashtable'];
57
 
            } else {
58
 
                throw new InvalidArgumentException('Missing memcache object');
59
 
            }
60
 
        }
61
 
 
62
 
        parent::__construct(array_merge(array(
63
 
            'prefix' => '',
64
 
        ), $params));
65
 
    }
66
 
 
67
 
    /**
68
 
     */
69
 
    protected function _initOb()
70
 
    {
71
 
        $this->_memcache = $this->_params['memcache'];
72
 
    }
73
 
 
74
 
    /**
75
 
     */
76
 
    public function get($key, $lifetime = 0)
77
 
    {
78
 
        $original_key = $key;
79
 
        $key = $this->_params['prefix'] . $key;
80
 
        if (isset($this->_expirecache[$key])) {
81
 
            return $this->_expirecache[$key];
82
 
        }
83
 
 
84
 
        $key_list = array($key);
85
 
        if (!empty($lifetime)) {
86
 
            $key_list[] = $key . '_e';
87
 
        }
88
 
 
89
 
        $res = $this->_memcache->get($key_list);
90
 
 
91
 
        if ($res === false) {
92
 
            unset($this->_expirecache[$key]);
93
 
        } else {
94
 
            // If we can't find the expire time, assume we have exceeded it.
95
 
            if (empty($lifetime) ||
96
 
                (($res[$key . '_e'] !== false) && ($res[$key . '_e'] + $lifetime > time()))) {
97
 
                $this->_expirecache[$key] = $res[$key];
98
 
            } else {
99
 
                $res[$key] = false;
100
 
                $this->expire($original_key);
101
 
            }
102
 
        }
103
 
 
104
 
        return $res[$key];
105
 
    }
106
 
 
107
 
    /**
108
 
     */
109
 
    public function set($key, $data, $lifetime = 0)
110
 
    {
111
 
        $key = $this->_params['prefix'] . $key;
112
 
 
113
 
        if ($this->_memcache->set($key . '_e', time(), $lifetime) !== false) {
114
 
            $this->_memcache->set($key, $data, $lifetime);
115
 
        }
116
 
    }
117
 
 
118
 
    /**
119
 
     */
120
 
    public function exists($key, $lifetime = 0)
121
 
    {
122
 
        return ($this->get($key, $lifetime) !== false);
123
 
    }
124
 
 
125
 
    /**
126
 
     */
127
 
    public function expire($key)
128
 
    {
129
 
        $key = $this->_params['prefix'] . $key;
130
 
        unset($this->_expirecache[$key]);
131
 
        $this->_memcache->delete($key . '_e');
132
 
 
133
 
        return $this->_memcache->delete($key);
134
 
    }
135
 
 
136
 
    /**
137
 
     */
138
 
    public function clear()
139
 
    {
140
 
        $this->_memcache->flush();
141
 
    }
142
 
 
143
 
}