/lib/OA/PermanentCache.php

https://bitbucket.org/valmy/openx · PHP · 167 lines · 67 code · 19 blank · 81 comment · 8 complexity · 9184874b065139f5aa0ef3b9ea24ba39 MD5 · raw file

  1. <?php
  2. /*
  3. +---------------------------------------------------------------------------+
  4. | OpenX v2.8 |
  5. | ========== |
  6. | |
  7. | Copyright (c) 2003-2009 OpenX Limited |
  8. | For contact details, see: http://www.openx.org/ |
  9. | |
  10. | This program is free software; you can redistribute it and/or modify |
  11. | it under the terms of the GNU General Public License as published by |
  12. | the Free Software Foundation; either version 2 of the License, or |
  13. | (at your option) any later version. |
  14. | |
  15. | This program is distributed in the hope that it will be useful, |
  16. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  17. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  18. | GNU General Public License for more details. |
  19. | |
  20. | You should have received a copy of the GNU General Public License |
  21. | along with this program; if not, write to the Free Software |
  22. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  23. +---------------------------------------------------------------------------+
  24. $Id$
  25. */
  26. require_once 'Cache/Lite.php';
  27. /**
  28. * A class to read and save permanent cache data, stored in /etc
  29. *
  30. * It features a predictable cache file name and automatic (un)serialising
  31. * and zlib (de)compression
  32. *
  33. * @author Matteo Beccati <matteo.beccati@openx.org>
  34. */
  35. class OA_PermanentCache
  36. {
  37. /**
  38. * @var Cache_Lite
  39. */
  40. var $oCache;
  41. /**
  42. * @var string
  43. */
  44. var $cachePath;
  45. /**
  46. * Class constructor
  47. *
  48. * @param string $cachePath The cache path
  49. *
  50. * @return OA_PermanentCache
  51. */
  52. function OA_PermanentCache($cachePath = null)
  53. {
  54. $this->cachePath = is_null($cachePath) ? MAX_PATH . '/etc/permanentcache/' : $cachePath;
  55. if (substr($cachePath, -1) != '/') {
  56. $this->cachePath .= '/';
  57. }
  58. $this->oCache = new Cache_Lite(array(
  59. 'cacheDir' => $this->cachePath,
  60. 'fileNameProtection' => false,
  61. 'lifeTime' => null,
  62. 'readControlType' => 'md5',
  63. 'dontCacheWhenTheResultIsFalse' => true
  64. ));
  65. }
  66. /**
  67. * A method to get the permanent cache content
  68. *
  69. * @param string $cacheName The name of the original file we are retrieving
  70. * @return mixed The cache content or FALSE in case of cache miss
  71. */
  72. function get($cacheName)
  73. {
  74. if (extension_loaded('zlib')) {
  75. $id = $this->_getId($cacheName);
  76. $group = $this->_getGroup($cacheName);
  77. if ($result = $this->oCache->get($id, $group, true)) {
  78. return unserialize(gzuncompress($result));
  79. }
  80. }
  81. return false;
  82. }
  83. /**
  84. * A method to save the permanent cache content. The content will be serialized and
  85. * compressed to save space
  86. *
  87. * @param mixed $data The content to save
  88. * @param string $cacheName The name of the original file we are storing
  89. * @return bool True if the cache was correctly saved
  90. */
  91. function save($data, $cacheName)
  92. {
  93. if (is_writable($this->cachePath) && extension_loaded('zlib')) {
  94. $id = $this->_getId($cacheName);
  95. $group = $this->_getGroup($cacheName);
  96. return $this->oCache->save(gzcompress(serialize($data), 9), $id, $group);
  97. }
  98. return false;
  99. }
  100. /**
  101. * A method to remove a cache file
  102. *
  103. * @param string $cacheName The name of the original file
  104. * @return bool True if the cache was deleted
  105. */
  106. function remove($cacheName)
  107. {
  108. $id = $this->_getId($cacheName);
  109. $group = $this->_getGroup($cacheName);
  110. return $this->oCache->remove($id, $group);
  111. }
  112. /**
  113. * Private method to generate the Cache_Lite cache ID from a file name
  114. *
  115. * @param string $cacheName The name of the original file
  116. * @return string The cache ID (the base file name without extension)
  117. */
  118. function _getId($cacheName)
  119. {
  120. // Deal with class::method style cache names
  121. $cacheName = str_replace('::', '/', $cacheName);
  122. // Strip extension
  123. $cacheName = preg_replace('/\.[^.]+?$/', '', $cacheName);
  124. $IdName = strtolower(basename($cacheName));
  125. return preg_replace('/[^a-z0-9]/i', '-', $IdName).'.bin';
  126. }
  127. /**
  128. * Private method to generate the Cache_Lite cache group from a file name
  129. *
  130. * @param string $cacheName The name of the original file
  131. * @return string The cache group (generated using the file path, or 'default')
  132. */
  133. function _getGroup($cacheName)
  134. {
  135. // Deal with class::method style cache names
  136. $cacheName = str_replace('::', '/', $cacheName);
  137. // Strip MAX_PATH
  138. if (strpos($cacheName, MAX_PATH) === 0) {
  139. $cacheName = substr($cacheName, strlen(MAX_PATH) + 1);
  140. }
  141. $groupName = strtolower(dirname($cacheName));
  142. if (!empty($groupName)) {
  143. return preg_replace('/[^a-z0-9]/i', '-', $groupName);
  144. }
  145. return 'default';
  146. }
  147. }
  148. ?>