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

## Source for file apc_backend.php

Documentation is available at apc_backend.php

1. <?php
2. /**
3. * File containing the ezcCacheApcBackend 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 an APC cache.
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. * ezcCacheStorageApc} instead.
34. *
35. * @package Cache
36. * @version //autogentag//
37. */
38. class ezcCacheApcBackend extends ezcCacheMemoryBackend
39. {
40. /**
41. * Constructs a new ezcCacheApcBackend object.
42. *
43. * @throws ezcBaseExtensionNotFoundException
44. * If the PHP apc extension is not installed.
45. */
46. public function __construct ()
47. {
48. if ( ! ezcBaseFeatures :: hasExtensionSupport ( 'apc' ) )
49. {
50. throw new ezcBaseExtensionNotFoundException ( 'apc', null, "PHP does not have APC support." );
51. }
52. }
54. /**
55. * Stores the data $var under the key $key. Returns true or false depending
56. * on the success of the operation.
57. *
58. * @param string $key
59. * @param mixed $var
60. * @param int $ttl
61. * @return bool
62. */
63. public function store ( $key, $var, $ttl = 0 )
64. {
65. $data = new ezcCacheMemoryVarStruct ( $key, $var, $ttl );
66. return apc_store( $key, $data, $ttl );
67. }
69. /**
70. * Fetches the data associated with key $key.
71. *
72. * @param mixed $key
73. * @return mixed
74. */
75. public function fetch ( $key )
76. {
77. $data = apc_fetch( $key );
78. return ( is_object ( $data ) ) ? $data->var : false;
79. }
81. /**
82. * Deletes the data associated with key $key. Returns true or false depending
83. * on the success of the operation.
84. *
85. * @param string $key
86. * @return bool
87. */
88. public function delete ( $key )
89. {
90. return apc_delete( $key );
91. }
93. /**
94. * Resets the complete backend.
95. *
96. * Marked private to not expose more of this interface to the user, since
97. * this will be removed in future versions.
98. *
99. * @return void
100. * @access private
101. */
102. public function reset ()
103. {
104. // Kills the whole user cache
105. apc_clear_cache( "user" );
106. }
108. /**
109. * Acquires a lock on the given $key.
110. *
111. * @param string $key
112. * @param int $waitTime usleep()
113. * @param int $maxTime seconds
114. */
115. public function acquireLock ( $key, $waitTime, $maxTime )
116. {
117. $counter = 0;
118. // add() does not replace and returns true on success. $maxTime is
119. // obeyed by Memcache expiry.
120. while ( apc_add( $key, time (), $maxTime ) === false )
121. {
122. // Wait for next check
123. usleep ( $waitTime );
124. // Don't check expiry time too frquently, since it requires restoring
125. if ( ( ++$counter % 10 === 0 ) && ( time () - (int)apc_fetch( $key ) > $maxTime ) )
126. {
127. // Release expired lock and place own lock
128. apc_store( $key, time (), $maxTime );
129. break;
130. }
131. }
132. }
134. /**
135. * Releases a lock on the given $key.
136. *
137. * @param string $key
138. * @return void
139. */
140. public function releaseLock ( $key )
141. {
142. apc_delete( $key );
143. }
144. }
145. ?>

Documentation generated by phpDocumentor 1.4.3