This repository has been archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Cache.php
169 lines (151 loc) · 4.67 KB
/
Cache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
// vim: fenc=utf-8:ft=php:ai:si:ts=4:sw=4:et:
/**
* Hashmark_Cache
*
* @filesource
* @copyright Copyright (c) 2008-2011 David Smith
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @package Hashmark
* @subpackage Base
* @version $Id$
*/
/**
* Zend_Cache wrapper.
*
* - Group keys based on Alex Rickabaugh's:
* http://www.aminus.org/blogs/index.php/2007/12/30/memcached_set_invalidation?blog=2#c38801
*
* @package Hashmark
* @subpackage Base
*/
class Hashmark_Cache extends Hashmark_Module
{
/**
* @var Zend_Cache_Frontend_* Current instance. See initModule().
*/
protected $_cache;
/**
* @param mixed $db Connection object/resource.
* @return boolean False if module could not be initialized and is unusable.
* Hashmark::getModule() will also then return false.
*/
public function initModule()
{
if (!empty($this->_baseConfig['backEndName'])) {
$this->_cache = Zend_Cache::factory('Core',
$this->_baseConfig['backEndName'],
$this->_baseConfig['frontEndOpts'],
$this->_baseConfig['backEndOpts']);
}
// Do not require caching.
return true;
}
/**
* Expose cache adapter status.
*
* @return boolean
*/
public function isConfigured()
{
return (boolean) $this->_cache;
}
/**
* Namespace/group-key wrapper for load().
*
* @param string $key
* @param string $group
* @param boolean $doNotTestCacheValidity See Zend_Cache_Core::save().
* @param boolean $doNotUnserialize See Zend_Cache_Core::save().
* @return mixed
*/
public function load($key, $group = '', $doNotTestCacheValidity = false, $doNotUnserialize = false)
{
if (!$this->_cache) {
return false;
}
return $this->_cache->load($this->_finalizeKey($key, $group),
$doNotTestCacheValidity, $doNotUnserialize);
}
/**
* Namespace/group-key wrapper for save().
*
* @param mixed $value
* @param string $key
* @param string $group
* @param int $specificLifetime See Zend_Cache_Core::save().
* @param int $priority See Zend_Cache_Core::save().
* @throws Zend_Cache_Exception
* @return boolean True on success.
*/
public function save($value, $key, $group = '', $specificLifetime = false, $priority = 8)
{
if (!$this->_cache) {
return true;
}
return $this->_cache->save($value, $this->_finalizeKey($key, $group), array(),
$specificLifetime, $priority);
}
/**
* Namespace/group-key wrapper for remove().
*
* @param string $key
* @param string $group
* @return boolean True on success.
*/
public function remove($key, $group = '')
{
if (!$this->_cache) {
return true;
}
return $this->_cache->remove($this->_finalizeKey($key, $group));
}
/**
* Invalidates all value keys composed with $group key.
*
* @return boolean True on success.
*/
public function removeGroup($group)
{
return $this->_getGroupKey($group, true) ? true : false;
}
/**
* Add namespace and group key (if needed) to get/set/remove keys.
*
* @param string $key Key name.
* @param string $group Group name.
* @return string Finalized key.
*/
protected function _finalizeKey($key, $group = '')
{
$fullKey = 'Hashmark';
if ($group) {
$groupKey = $this->_getGroupKey($group);
if (!$groupKey) {
$groupKey = $this->_getGroupKey($group, true);
}
$fullKey .= $groupKey;
}
return $fullKey . $key;
}
/**
* Return a value key key based on a group name.
*
* @param string $group Group name.
* @param boolean $new If true, a new key is created.
* @return string Active group key; false on error.
*/
protected function _getGroupKey($group, $new = false)
{
$groupKeyKey = 'Hashmark' . $group;
if ($new) {
$groupKeyValue = Hashmark_Util::randomSha1();
if ($this->save($groupKeyValue, $groupKeyKey)) {
return $groupKeyValue;
} else {
return false;
}
}
return $this->load($groupKeyKey);
}
}