/extensions/OpenStackManager/aws-sdk/lib/cachecore/cachefile.class.php

https://github.com/ChuguluGames/mediawiki-svn · PHP · 170 lines · 83 code · 21 blank · 66 comment · 11 complexity · 7f06aae849bb34830a360a8caf943d0f MD5 · raw file

  1. <?php
  2. /**
  3. * Container for all file-based cache methods. Inherits additional methods from <CacheCore>. Adheres
  4. * to the ICacheCore interface.
  5. *
  6. * @version 2009.10.10
  7. * @copyright 2006-2010 Ryan Parman
  8. * @copyright 2006-2010 Foleeo, Inc.
  9. * @copyright 2008-2010 Contributors
  10. * @license http://opensource.org/licenses/bsd-license.php Simplified BSD License
  11. * @link http://github.com/skyzyx/cachecore CacheCore
  12. * @link http://getcloudfusion.com CloudFusion
  13. */
  14. class CacheFile extends CacheCore implements ICacheCore
  15. {
  16. /*%******************************************************************************************%*/
  17. // CONSTRUCTOR
  18. /**
  19. * Constructs a new instance of this class.
  20. *
  21. * @param string $name (Required) A name to uniquely identify the cache object.
  22. * @param string $location (Required) The location to store the cache object in. This may vary by cache method.
  23. * @param integer $expires (Required) The number of seconds until a cache object is considered stale.
  24. * @param boolean $gzip (Optional) Whether data should be gzipped before being stored. Defaults to true.
  25. * @return object Reference to the cache object.
  26. */
  27. public function __construct($name, $location, $expires, $gzip = true)
  28. {
  29. parent::__construct($name, $location, $expires, $gzip);
  30. $this->id = $this->location . '/' . $this->name . '.cache';
  31. }
  32. /**
  33. * Creates a new cache.
  34. *
  35. * @param mixed $data (Required) The data to cache.
  36. * @return boolean Whether the operation was successful.
  37. */
  38. public function create($data)
  39. {
  40. if (file_exists($this->id))
  41. {
  42. return false;
  43. }
  44. elseif (file_exists($this->location) && is_writeable($this->location))
  45. {
  46. $data = serialize($data);
  47. $data = $this->gzip ? gzcompress($data) : $data;
  48. return (bool) file_put_contents($this->id, $data);
  49. }
  50. return false;
  51. }
  52. /**
  53. * Reads a cache.
  54. *
  55. * @return mixed Either the content of the cache object, or boolean `false`.
  56. */
  57. public function read()
  58. {
  59. if (file_exists($this->id) && is_readable($this->id))
  60. {
  61. $data = file_get_contents($this->id);
  62. $data = $this->gzip ? gzuncompress($data) : $data;
  63. $data = unserialize($data);
  64. if ($data === false)
  65. {
  66. /*
  67. This should only happen when someone changes the gzip settings and there is
  68. existing data or someone has been mucking about in the cache folder manually.
  69. Delete the bad entry since the file cache doesn't clean up after itself and
  70. then return false so fresh data will be retrieved.
  71. */
  72. $this->delete();
  73. return false;
  74. }
  75. return $data;
  76. }
  77. return false;
  78. }
  79. /**
  80. * Updates an existing cache.
  81. *
  82. * @param mixed $data (Required) The data to cache.
  83. * @return boolean Whether the operation was successful.
  84. */
  85. public function update($data)
  86. {
  87. if (file_exists($this->id) && is_writeable($this->id))
  88. {
  89. $data = serialize($data);
  90. $data = $this->gzip ? gzcompress($data) : $data;
  91. return (bool) file_put_contents($this->id, $data);
  92. }
  93. return false;
  94. }
  95. /**
  96. * Deletes a cache.
  97. *
  98. * @return boolean Whether the operation was successful.
  99. */
  100. public function delete()
  101. {
  102. if (file_exists($this->id))
  103. {
  104. return unlink($this->id);
  105. }
  106. return false;
  107. }
  108. /**
  109. * Checks whether the cache object is expired or not.
  110. *
  111. * @return boolean Whether the cache is expired or not.
  112. */
  113. public function is_expired()
  114. {
  115. if ($this->timestamp() + $this->expires < time())
  116. {
  117. return true;
  118. }
  119. return false;
  120. }
  121. /**
  122. * Retrieves the timestamp of the cache.
  123. *
  124. * @return mixed Either the Unix time stamp of the cache creation, or boolean `false`.
  125. */
  126. public function timestamp()
  127. {
  128. clearstatcache();
  129. if (file_exists($this->id))
  130. {
  131. $this->timestamp = filemtime($this->id);
  132. return $this->timestamp;
  133. }
  134. return false;
  135. }
  136. /**
  137. * Resets the freshness of the cache.
  138. *
  139. * @return boolean Whether the operation was successful.
  140. */
  141. public function reset()
  142. {
  143. if (file_exists($this->id))
  144. {
  145. return touch($this->id);
  146. }
  147. return false;
  148. }
  149. }