3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 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 === null) {
53 * Internal method to get multiple items.
55 * @param array $normalizedKeys
56 * @return array Associative array of keys and values
57 * @throws Exception\ExceptionInterface
59 protected function internalGetItems(array & $normalizedKeys)
61 $namespace = $this->getOptions()->getNamespace();
62 if ($namespace === '') {
63 return $this->zdcFetchMulti($normalizedKeys);
66 $prefix = $namespace . self
::NAMESPACE_SEPARATOR
;
67 $internalKeys = array();
68 foreach ($normalizedKeys as $normalizedKey) {
69 $internalKeys[] = $prefix . $normalizedKey;
72 $fetch = $this->zdcFetchMulti($internalKeys);
74 $prefixL = strlen($prefix);
75 foreach ($fetch as $k => & $v) {
76 $result[substr($k, $prefixL)] = $v;
83 * Internal method to test if an item exists.
85 * @param string $normalizedKey
87 * @throws Exception\ExceptionInterface
89 protected function internalHasItem(& $normalizedKey)
91 $namespace = $this->getOptions()->getNamespace();
92 $prefix = ($namespace === '') ?
'' : $namespace . self
::NAMESPACE_SEPARATOR
;
93 return ($this->zdcFetch($prefix . $normalizedKey) !== false);
97 * Internal method to test multiple items.
99 * @param array $normalizedKeys
100 * @return array Array of found keys
101 * @throws Exception\ExceptionInterface
103 protected function internalHasItems(array & $normalizedKeys)
105 $namespace = $this->getOptions()->getNamespace();
106 if ($namespace === '') {
107 return array_keys($this->zdcFetchMulti($normalizedKeys));
110 $prefix = $namespace . self
::NAMESPACE_SEPARATOR
;
111 $internalKeys = array();
112 foreach ($normalizedKeys as $normalizedKey) {
113 $internalKeys[] = $prefix . $normalizedKey;
116 $fetch = $this->zdcFetchMulti($internalKeys);
118 $prefixL = strlen($prefix);
119 foreach ($fetch as $internalKey => & $value) {
120 $result[] = substr($internalKey, $prefixL);
127 * Get metadata for multiple items
129 * @param array $normalizedKeys
130 * @return array Associative array of keys and metadata
132 * @triggers getMetadatas.pre(PreEvent)
133 * @triggers getMetadatas.post(PostEvent)
134 * @triggers getMetadatas.exception(ExceptionEvent)
136 protected function internalGetMetadatas(array & $normalizedKeys)
138 $namespace = $this->getOptions()->getNamespace();
139 if ($namespace === '') {
140 $result = $this->zdcFetchMulti($normalizedKeys);
141 return array_fill_keys(array_keys($result), array());
144 $prefix = $namespace . self
::NAMESPACE_SEPARATOR
;
145 $internalKeys = array();
146 foreach ($normalizedKeys as $normalizedKey) {
147 $internalKeys[] = $prefix . $normalizedKey;
150 $fetch = $this->zdcFetchMulti($internalKeys);
152 $prefixL = strlen($prefix);
153 foreach ($fetch as $internalKey => $value) {
154 $result[substr($internalKey, $prefixL)] = array();
163 * Internal method to store an item.
165 * @param string $normalizedKey
166 * @param mixed $value
168 * @throws Exception\ExceptionInterface
170 protected function internalSetItem(& $normalizedKey, & $value)
172 $options = $this->getOptions();
173 $namespace = $options->getNamespace();
174 $prefix = ($namespace === '') ?
'' : $namespace . self
::NAMESPACE_SEPARATOR
;
175 $this->zdcStore($prefix . $normalizedKey, $value, $options->getTtl());
180 * Internal method to remove an item.
182 * @param string $normalizedKey
184 * @throws Exception\ExceptionInterface
186 protected function internalRemoveItem(& $normalizedKey)
188 $namespace = $this->getOptions()->getNamespace();
189 $prefix = ($namespace === '') ?
'' : $namespace . self
::NAMESPACE_SEPARATOR
;
190 return $this->zdcDelete($prefix . $normalizedKey);
196 * Internal method to get capabilities of this adapter
198 * @return Capabilities
200 protected function internalGetCapabilities()
202 if ($this->capabilities
=== null) {
203 $this->capabilityMarker
= new stdClass();
204 $this->capabilities
= new Capabilities(
206 $this->capabilityMarker
,
208 'supportedDatatypes' => array(
215 'object' => 'object',
218 'supportedMetadata' => array(),
222 'useRequestTime' => false,
223 'expiredRead' => false,
225 'namespaceIsPrefix' => true,
226 'namespaceSeparator' => self
::NAMESPACE_SEPARATOR
,
231 return $this->capabilities
;
234 /* internal wrapper of zend_[disk|shm]_cache_* functions */
237 * Store data into Zend Data Cache (zdc)
239 * @param string $internalKey
240 * @param mixed $value
243 * @throws Exception\RuntimeException
245 abstract protected function zdcStore($internalKey, $value, $ttl);
248 * Fetch a single item from Zend Data Cache (zdc)
250 * @param string $internalKey
251 * @return mixed The stored value or FALSE if item wasn't found
252 * @throws Exception\RuntimeException
254 abstract protected function zdcFetch($internalKey);
257 * Fetch multiple items from Zend Data Cache (zdc)
259 * @param array $internalKeys
260 * @return array All found items
261 * @throws Exception\RuntimeException
263 abstract protected function zdcFetchMulti(array $internalKeys);
266 * Delete data from Zend Data Cache (zdc)
268 * @param string $internalKey
270 * @throws Exception\RuntimeException
272 abstract protected function zdcDelete($internalKey);