# Source for file Apc.php

Documentation is available at Apc.php

1. <?php
2. /*
3. * $Id: Apc.php 1080 2007-02-10 18:17:08Z romanb $
4. *
5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16. *
17. * This software consists of voluntary contributions made by many individuals
18. * and is licensed under the LGPL. For more information, see
19. * <http://www.phpdoctrine.com>.
20. */
22. /**
23. * Doctrine_Cache_Apc
24. *
25. * @package Doctrine
26. * @subpackage Doctrine_Cache
27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
28. * @category Object Relational Mapping
29. * @link www.phpdoctrine.com
30. * @since 1.0
31. * @version $Revision: 1080 $
32. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
33. */
34. class Doctrine_Cache_Apc extends Doctrine_Cache_Driver
35. {
36. /**
37. * constructor
38. *
39. * @param array $options associative array of cache driver options
40. */
41. public function __construct($options = array())
42. {
43. if ( ! extension_loaded('apc')) {
44. throw new Doctrine_Cache_Exception('The apc extension must be loaded for using this backend !');
45. }
46. parent::__construct($options);
47. }
48. /**
49. * Test if a cache is available for the given id and (if yes) return it (false else)
50. *
51. * Note : return value is always "string" (unserialization is done by the core not by the backend)
52. *
53. * @param string $id cache id
54. * @param boolean $testCacheValidity if set to false, the cache validity won't be tested
55. * @return string cached datas (or false)
56. */
57. public function fetch($id, $testCacheValidity = true)
58. {
59. $tmp = apc_fetch($id);
60. if (is_array($tmp)) {
61. return $tmp[0];
62. }
63. return false;
64. }
65. /**
66. * Test if a cache is available or not (for the given id)
67. *
68. * @param string $id cache id
69. * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
70. */
71. public function contains($id)
72. {
73. $tmp = apc_fetch($id);
74. if (is_array($tmp)) {
75. return $tmp[1];
76. }
77. return false;
78. }
79. /**
80. * Save some string datas into a cache record
81. *
82. * Note : $data is always saved as a string
83. *
84. * @param string $data data to cache
85. * @param string $id cache id
86. * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime)
87. * @return boolean true if no problem
88. */
89. public function save($id, $data, $lifeTime = false)
90. {
91. $lifeTime = $this->getLifeTime($lifeTime);
93. return (bool) apc_store($id, array($data, time()), $lifeTime);
94. }
95. /**
96. * Remove a cache record
97. *
98. * @param string $id cache id
99. * @return boolean true if no problem
100. */
101. public function delete($id)
102. {
103. return apc_delete($id);
104. }
105. }