| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: Memcache.php 7490 2010-03-29 19:53:27Z jwage $
|
|
|
4 |
*
|
|
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
16 |
*
|
|
|
17 |
* This software consists of voluntary contributions made by many individuals
|
|
|
18 |
* and is licensed under the LGPL. For more information, see
|
|
|
19 |
* <http://www.doctrine-project.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Memcache cache driver
|
|
|
24 |
*
|
|
|
25 |
* @package Doctrine
|
|
|
26 |
* @subpackage Cache
|
|
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
28 |
* @link www.doctrine-project.org
|
|
|
29 |
* @since 1.0
|
|
|
30 |
* @version $Revision: 7490 $
|
|
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
32 |
* @author Jonathan H. Wage <jonwage@gmail.com>
|
|
|
33 |
*/
|
|
|
34 |
class Doctrine_Cache_Memcache extends Doctrine_Cache_Driver
|
|
|
35 |
{
|
|
|
36 |
/**
|
|
|
37 |
* @var Memcache $_memcache memcache object
|
|
|
38 |
*/
|
|
|
39 |
protected $_memcache = null;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* constructor
|
|
|
43 |
*
|
|
|
44 |
* @param array $options associative array of cache driver options
|
|
|
45 |
*/
|
|
|
46 |
public function __construct($options = array())
|
|
|
47 |
{
|
|
|
48 |
if ( ! extension_loaded('memcache')) {
|
|
|
49 |
throw new Doctrine_Cache_Exception('In order to use Memcache driver, the memcache extension must be loaded.');
|
|
|
50 |
}
|
|
|
51 |
parent::__construct($options);
|
|
|
52 |
|
|
|
53 |
if (isset($options['servers'])) {
|
|
|
54 |
$value= $options['servers'];
|
|
|
55 |
if (isset($value['host'])) {
|
|
|
56 |
// in this case, $value seems to be a simple associative array (one server only)
|
|
|
57 |
$value = array(0 => $value); // let's transform it into a classical array of associative arrays
|
|
|
58 |
}
|
|
|
59 |
$this->setOption('servers', $value);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
$this->_memcache = new Memcache;
|
|
|
63 |
|
|
|
64 |
foreach ($this->_options['servers'] as $server) {
|
|
|
65 |
if ( ! array_key_exists('persistent', $server)) {
|
|
|
66 |
$server['persistent'] = true;
|
|
|
67 |
}
|
|
|
68 |
if ( ! array_key_exists('port', $server)) {
|
|
|
69 |
$server['port'] = 11211;
|
|
|
70 |
}
|
|
|
71 |
$this->_memcache->addServer($server['host'], $server['port'], $server['persistent']);
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Test if a cache record exists for the passed id
|
|
|
77 |
*
|
|
|
78 |
* @param string $id cache id
|
|
|
79 |
* @return mixed Returns either the cached data or false
|
|
|
80 |
*/
|
|
|
81 |
protected function _doFetch($id, $testCacheValidity = true)
|
|
|
82 |
{
|
|
|
83 |
return $this->_memcache->get($id);
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Test if a cache is available or not (for the given id)
|
|
|
88 |
*
|
|
|
89 |
* @param string $id cache id
|
|
|
90 |
* @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
|
|
|
91 |
*/
|
|
|
92 |
protected function _doContains($id)
|
|
|
93 |
{
|
|
|
94 |
return (bool) $this->_memcache->get($id);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Save a cache record directly. This method is implemented by the cache
|
|
|
99 |
* drivers and used in Doctrine_Cache_Driver::save()
|
|
|
100 |
*
|
|
|
101 |
* @param string $id cache id
|
|
|
102 |
* @param string $data data to cache
|
|
|
103 |
* @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime)
|
|
|
104 |
* @return boolean true if no problem
|
|
|
105 |
*/
|
|
|
106 |
protected function _doSave($id, $data, $lifeTime = false)
|
|
|
107 |
{
|
|
|
108 |
if ($this->_options['compression']) {
|
|
|
109 |
$flag = MEMCACHE_COMPRESSED;
|
|
|
110 |
} else {
|
|
|
111 |
$flag = 0;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
return $this->_memcache->set($id, $data, $flag, $lifeTime);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Remove a cache record directly. This method is implemented by the cache
|
|
|
119 |
* drivers and used in Doctrine_Cache_Driver::delete()
|
|
|
120 |
*
|
|
|
121 |
* @param string $id cache id
|
|
|
122 |
* @return boolean true if no problem
|
|
|
123 |
*/
|
|
|
124 |
protected function _doDelete($id)
|
|
|
125 |
{
|
|
|
126 |
return $this->_memcache->delete($id);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Fetch an array of all keys stored in cache
|
|
|
131 |
*
|
|
|
132 |
* @return array Returns the array of cache keys
|
|
|
133 |
*/
|
|
|
134 |
protected function _getCacheKeys()
|
|
|
135 |
{
|
|
|
136 |
$keys = array();
|
|
|
137 |
$allSlabs = $this->_memcache->getExtendedStats('slabs');
|
|
|
138 |
|
|
|
139 |
foreach ($allSlabs as $server => $slabs) {
|
|
|
140 |
foreach (array_keys($slabs) as $slabId) {
|
|
|
141 |
$dump = $this->_memcache->getExtendedStats('cachedump', (int) $slabId);
|
|
|
142 |
foreach ($dump as $entries) {
|
|
|
143 |
if ($entries) {
|
|
|
144 |
$keys = array_merge($keys, array_keys($entries));
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
return $keys;
|
|
|
150 |
}
|
|
|
151 |
}
|