1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17: class Horde_Cache_Storage_Memcache extends Horde_Cache_Storage_Base implements Serializable
18: {
19: 20: 21: 22: 23: 24:
25: protected $_expirecache = array();
26:
27: 28: 29: 30: 31:
32: protected $_memcache;
33:
34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:
46: public function __construct(array $params = array())
47: {
48: if (!isset($params['memcache'])) {
49: throw new InvalidArgumentException('Missing memcache object');
50: }
51:
52: $this->_memcache = $params['memcache'];
53: unset($params['memcache']);
54:
55: parent::__construct(array_merge(array(
56: 'prefix' => '',
57: ), $params));
58: }
59:
60: 61:
62: public function get($key, $lifetime = 0)
63: {
64: $original_key = $key;
65: $key = $this->_params['prefix'] . $key;
66: if (isset($this->_expirecache[$key])) {
67: return $this->_expirecache[$key];
68: }
69:
70: $key_list = array($key);
71: if (!empty($lifetime)) {
72: $key_list[] = $key . '_e';
73: }
74:
75: $res = $this->_memcache->get($key_list);
76:
77: if ($res === false) {
78: unset($this->_expirecache[$key]);
79: } else {
80:
81: if (empty($lifetime) ||
82: (($res[$key . '_e'] !== false) && ($res[$key . '_e'] + $lifetime > time()))) {
83: $this->_expirecache[$key] = $res[$key];
84: } else {
85: $res[$key] = false;
86: $this->expire($original_key);
87: }
88: }
89:
90: return $res[$key];
91: }
92:
93: 94:
95: public function set($key, $data, $lifetime = 0)
96: {
97: $key = $this->_params['prefix'] . $key;
98:
99: if ($this->_memcache->set($key . '_e', time(), $lifetime) !== false) {
100: $this->_memcache->set($key, $data, $lifetime);
101: }
102: }
103:
104: 105:
106: public function exists($key, $lifetime = 0)
107: {
108: return ($this->get($key, $lifetime) !== false);
109: }
110:
111: 112:
113: public function expire($key)
114: {
115: $key = $this->_params['prefix'] . $key;
116: unset($this->_expirecache[$key]);
117: $this->_memcache->delete($key . '_e');
118:
119: return $this->_memcache->delete($key);
120: }
121:
122: 123:
124: public function clear()
125: {
126: $this->_memcache->flush();
127: }
128:
129:
130:
131: 132:
133: public function serialize()
134: {
135: return serialize(array(
136: $this->_memcache,
137: $this->_params
138: ));
139: }
140:
141: 142:
143: public function unserialize($data)
144: {
145: list($this->_memcache, $this->_params) = unserialize($data);
146: }
147:
148: }
149: