3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Cache\Storage\Adapter
;
13 use Zend\Cache\Exception
;
14 use Zend\Cache\Storage\Capabilities
;
16 abstract class AbstractZendServer
extends AbstractAdapter
19 * The namespace separator used on Zend Data Cache functions
23 const NAMESPACE_SEPARATOR
= '::';
28 * Internal method to get an item.
30 * @param string $normalizedKey
31 * @param bool $success
32 * @param mixed $casToken
33 * @return mixed Data on success, null on failure
34 * @throws Exception\ExceptionInterface
36 protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null)
38 $namespace = $this->getOptions()->getNamespace();
39 $prefix = ($namespace === '') ?
'' : $namespace . self
::NAMESPACE_SEPARATOR
;
41 $result = $this->zdcFetch($prefix . $normalizedKey);
42 if ($result === false) {
54 * Internal method to get multiple items.
56 * @param array $normalizedKeys
57 * @return array Associative array of keys and values
58 * @throws Exception\ExceptionInterface
60 protected function internalGetItems(array & $normalizedKeys)
62 $namespace = $this->getOptions()->getNamespace();
63 if ($namespace === '') {
64 return $this->zdcFetchMulti($normalizedKeys);
67 $prefix = $namespace . self
::NAMESPACE_SEPARATOR
;
68 $internalKeys = array();
69 foreach ($normalizedKeys as $normalizedKey) {
70 $internalKeys[] = $prefix . $normalizedKey;
73 $fetch = $this->zdcFetchMulti($internalKeys);
75 $prefixL = strlen($prefix);
76 foreach ($fetch as $k => & $v) {
77 $result[substr($k, $prefixL)] = $v;
84 * Internal method to test if an item exists.
86 * @param string $normalizedKey
88 * @throws Exception\ExceptionInterface
90 protected function internalHasItem(& $normalizedKey)
92 $namespace = $this->getOptions()->getNamespace();
93 $prefix = ($namespace === '') ?
'' : $namespace . self
::NAMESPACE_SEPARATOR
;
94 return ($this->zdcFetch($prefix . $normalizedKey) !== false);
98 * Internal method to test multiple items.
100 * @param array $normalizedKeys
101 * @return array Array of found keys
102 * @throws Exception\ExceptionInterface
104 protected function internalHasItems(array & $normalizedKeys)
106 $namespace = $this->getOptions()->getNamespace();
107 if ($namespace === '') {
108 return array_keys($this->zdcFetchMulti($normalizedKeys));
111 $prefix = $namespace . self
::NAMESPACE_SEPARATOR
;
112 $internalKeys = array();
113 foreach ($normalizedKeys as $normalizedKey) {
114 $internalKeys[] = $prefix . $normalizedKey;
117 $fetch = $this->zdcFetchMulti($internalKeys);
119 $prefixL = strlen($prefix);
120 foreach ($fetch as $internalKey => & $value) {
121 $result[] = substr($internalKey, $prefixL);
128 * Get metadata for multiple items
130 * @param array $normalizedKeys
131 * @return array Associative array of keys and metadata
133 * @triggers getMetadatas.pre(PreEvent)
134 * @triggers getMetadatas.post(PostEvent)
135 * @triggers getMetadatas.exception(ExceptionEvent)
137 protected function internalGetMetadatas(array & $normalizedKeys)
139 $namespace = $this->getOptions()->getNamespace();
140 if ($namespace === '') {
141 $result = $this->zdcFetchMulti($normalizedKeys);
142 return array_fill_keys(array_keys($result), array());
145 $prefix = $namespace . self
::NAMESPACE_SEPARATOR
;
146 $internalKeys = array();
147 foreach ($normalizedKeys as $normalizedKey) {
148 $internalKeys[] = $prefix . $normalizedKey;
151 $fetch = $this->zdcFetchMulti($internalKeys);
153 $prefixL = strlen($prefix);
154 foreach ($fetch as $internalKey => $value) {
155 $result[substr($internalKey, $prefixL)] = array();
164 * Internal method to store an item.
166 * @param string $normalizedKey
167 * @param mixed $value
169 * @throws Exception\ExceptionInterface
171 protected function internalSetItem(& $normalizedKey, & $value)
173 $options = $this->getOptions();
174 $namespace = $options->getNamespace();
175 $prefix = ($namespace === '') ?
'' : $namespace . self
::NAMESPACE_SEPARATOR
;
176 $this->zdcStore($prefix . $normalizedKey, $value, $options->getTtl());
181 * Internal method to remove an item.
183 * @param string $normalizedKey
185 * @throws Exception\ExceptionInterface
187 protected function internalRemoveItem(& $normalizedKey)
189 $namespace = $this->getOptions()->getNamespace();
190 $prefix = ($namespace === '') ?
'' : $namespace . self
::NAMESPACE_SEPARATOR
;
191 return $this->zdcDelete($prefix . $normalizedKey);
197 * Internal method to get capabilities of this adapter
199 * @return Capabilities
201 protected function internalGetCapabilities()
203 if ($this->capabilities
=== null) {
204 $this->capabilityMarker
= new stdClass();
205 $this->capabilities
= new Capabilities(
207 $this->capabilityMarker
,
209 'supportedDatatypes' => array(
216 'object' => 'object',
219 'supportedMetadata' => array(),
223 'useRequestTime' => false,
224 'expiredRead' => false,
226 'namespaceIsPrefix' => true,
227 'namespaceSeparator' => self
::NAMESPACE_SEPARATOR
,
232 return $this->capabilities
;
235 /* internal wrapper of zend_[disk|shm]_cache_* functions */
238 * Store data into Zend Data Cache (zdc)
240 * @param string $internalKey
241 * @param mixed $value
244 * @throws Exception\RuntimeException
246 abstract protected function zdcStore($internalKey, $value, $ttl);
249 * Fetch a single item from Zend Data Cache (zdc)
251 * @param string $internalKey
252 * @return mixed The stored value or FALSE if item wasn't found
253 * @throws Exception\RuntimeException
255 abstract protected function zdcFetch($internalKey);
258 * Fetch multiple items from Zend Data Cache (zdc)
260 * @param array $internalKeys
261 * @return array All found items
262 * @throws Exception\RuntimeException
264 abstract protected function zdcFetchMulti(array $internalKeys);
267 * Delete data from Zend Data Cache (zdc)
269 * @param string $internalKey
271 * @throws Exception\RuntimeException
273 abstract protected function zdcDelete($internalKey);