## Apache Zeta Components Manual :: File Source for memcache_backend.php

## Source for file memcache_backend.php

Documentation is available at memcache_backend.php

1. <?php
2. /**
3. * File containing the ezcCacheMemcacheBackend class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @package Cache
23. * @version //autogentag//
24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
25. * @filesource
26. */
28. /**
29. * This backend stores data in a Memcache.
30. *
31. * @apichange This class will be deprecated in the next major version of the
32. * Cache component. Please do not use it directly, but use {@link }
33. * ezcCacheStorageMemcache} instead.
34. *
35. * @package Cache
36. * @version //autogentag//
37. */
38. class ezcCacheMemcacheBackend extends ezcCacheMemoryBackend
39. {
40. /**
41. * The compress threshold.
42. *
43. * Nearly 1MB (48,576B less).
44. */
45. const COMPRESS_THRESHOLD = 1000000;
47. /**
48. * Maximum length of a cache key for Memcached.
49. */
50. const MAX_KEY_LENGTH = 249;
52. /**
53. * Holds an instance to a Memcache object.
54. *
55. * @var resource
56. */
57. protected $memcache ;
59. /**
60. * Holds the options for this class.
61. *
62. * @var ezcCacheStorageMemcacheOptions
63. */
64. protected $options ;
66. /**
67. * Stores the connections to Memcached.
68. *
69. * @var array(string=>Memcache)
70. */
71. protected static $connections = array();
73. /**
74. * Keeps track of the number of backends using the same connection.
75. *
76. * This is to avoid that the dtor of a backend accedentally closes a
77. * connection that is still in used by another backend.
78. *
79. * @var array(string=>int)
80. */
81. protected static $connectionCounter = array();
83. /**
84. * Stores the connection identifier.
85. *
86. * This is generated in the ctor and used in the dtor.
87. *
88. * @var string
89. */
90. protected $connectionIdentifier ;
92. /**
93. * Constructs a new ezcCacheMemcacheBackend object.
94. *
95. * For options for this backend see {@link ezcCacheStorageMemcacheOptions}.
96. *
97. * @throws ezcBaseExtensionNotFoundException
98. * If the PHP memcache and zlib extensions are not installed.
99. * @throws ezcCacheMemcacheException
100. * If the connection to the Memcache host did not succeed.
101. *
102. * @param array(string=>mixed) $options
103. */
104. public function __construct ( array $options = array() )
105. {
106. if ( ! ezcBaseFeatures :: hasExtensionSupport ( 'memcache' ) )
107. {
108. throw new ezcBaseExtensionNotFoundException ( 'memcache', null, "PHP does not have Memcache support." );
109. }
111. if ( ! ezcBaseFeatures :: hasExtensionSupport ( 'zlib' ) )
112. {
113. throw new ezcBaseExtensionNotFoundException ( 'zlib', null, "PHP not configured with --with-zlib." );
114. }
116. $this-> options = new ezcCacheStorageMemcacheOptions ( $options );
118. $this-> connectionIdentifier = $this-> options ->host . ':' . $this-> options ->port;
119. if ( !isset( self::$connections[$this-> connectionIdentifier ] ) )
120. {
121. self::$connections[$this-> connectionIdentifier ] = new Memcache();
122. // Currently 0 backends use the connection
123. self::$connectionCounter[$this-> connectionIdentifier ] = 0;
124. }
126. $this-> memcache = self::$connections[$this-> connectionIdentifier ];
127. // Now 1 backend uses it
128. self::$connectionCounter[$this-> connectionIdentifier ]++;
129. if ( $this-> options ->persistent === true )
130. {
131. if ( !@$this-> memcache ->pconnect( $this-> options ->host, $this-> options ->port, $this-> options ->ttl ) )
132. {
133. throw new ezcCacheMemcacheException ( 'Could not connect to Memcache using a persistent connection.' );
134. }
135. }
136. else
137. {
138. if ( !@$this-> memcache ->connect( $this-> options ->host, $this-> options ->port, $this-> options ->ttl ) )
139. {
140. throw new ezcCacheMemcacheException ( 'Could not connect to Memcache.' );
141. }
142. }
144. $this-> memcache ->setCompressThreshold( self::COMPRESS_THRESHOLD );
145. }
147. /**
148. * Destructor for the Memcache backend.
149. */
150. public function __destruct ()
151. {
152. self::$connectionCounter[$this-> connectionIdentifier ]--;
153. // Save to ignore persistent connections, since close() does not affect them
154. if ( self::$connectionCounter[$this-> connectionIdentifier ] === 0 )
155. {
156. $this-> memcache ->close();
157. }
158. }
160. /**
161. * Adds the $var data to the cache under the key $key. Returns true or
162. * false depending on the success of the operation.
163. *
164. * @param string $key
165. * @param mixed $var
166. * @param int $expire
167. * @return bool
168. */
169. public function store ( $key, $var, $expire = 0 )
170. {
171. if ( strlen( $key ) > self::MAX_KEY_LENGTH )
172. {
173. throw new ezcCacheInvalidKeyException ( $key, 'Length > ' . self::MAX_KEY_LENGTH . '.' );
174. }
176. // protect our data by wrapping it in an object
177. $data = new ezcCacheMemoryVarStruct ( $key, $var, $expire );
178. $compressed = ( $this-> options ->compressed === true ) ? MEMCACHE_COMPRESSED : false;
179. return $this-> memcache ->set( $key, $data, $compressed, $expire );
180. }
182. /**
183. * Returns the data from the cache associated with key $key.
184. *
185. * @param mixed $key
186. * @return mixed
187. */
188. public function fetch ( $key )
189. {
190. if ( strlen ( $key ) > self::MAX_KEY_LENGTH )
191. {
192. throw new ezcCacheInvalidKeyException ( $key, 'Length > ' . self::MAX_KEY_LENGTH . '.' );
193. }
195. $data = $this-> memcache ->get( $key );
196. return ( is_object ( $data ) ) ? $data->var : false;
197. }
199. /**
200. * Deletes the data from the cache associated with key $key. Returns true or
201. * false depending on the success of the operation.
202. *
203. * The value $timeout specifies the timeout period in seconds for the delete
204. * command.
205. *
206. * @param string $key
207. * @param int $timeout
208. * @return bool
209. */
210. public function delete ( $key, $timeout = 0 )
211. {
212. if ( strlen ( $key ) > self::MAX_KEY_LENGTH )
213. {
214. throw new ezcCacheInvalidKeyException ( $key, 'Length > ' . self::MAX_KEY_LENGTH . '.' );
215. }
217. return $this-> memcache ->delete( $key, $timeout );
218. }
220. /**
221. * Resets the complete backend.
222. *
223. * Marked private to not expose more of this interface to the user, since
224. * this will be removed in future versions.
225. *
226. * @return void
227. * @access private
228. */
229. public function reset ()
230. {
231. // Kills whole memcache content
232. $this-> memcache ->flush();
233. }
235. /**
236. * Acquires a lock on the given $key.
237. *
238. * @param string $key
239. * @param int $waitTime usleep()
240. * @param int $maxTime seconds
241. */
242. public function acquireLock ( $key, $waitTime, $maxTime )
243. {
244. if ( strlen ( $key ) > self::MAX_KEY_LENGTH )
245. {
246. throw new ezcCacheInvalidKeyException ( $key, 'Length > ' . self::MAX_KEY_LENGTH . '.' );
247. }
249. // add() does not replace and returns true on success. $maxTime is
250. // obeyed by Memcache expiry.
251. while ( $this-> memcache ->add( $key, $key, false, $maxTime ) === false )
252. {
253. // Wait for next check
254. usleep ( $waitTime );
255. }
256. }
258. /**
259. * Releases a lock on the given $key.
260. *
261. * @param string $key
262. * @return void
263. */
264. public function releaseLock ( $key )
265. {
266. if ( strlen ( $key ) > self::MAX_KEY_LENGTH )
267. {
268. throw new ezcCacheInvalidKeyException ( $key, 'Length > ' . self::MAX_KEY_LENGTH . '.' );
269. }
271. $this-> memcache ->delete( $key );
272. }
274. }
275. ?>

Documentation generated by phpDocumentor 1.4.3