PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/ojs/ojs-2.2/classes/cache/FileCache.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite
PHP | 114 lines | 50 code | 14 blank | 50 comment | 8 complexity | 3c51163625a05f33085bd7badc4e1d6e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * FileCache.inc.php
  4. *
  5. * Copyright (c) 2003-2007 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @package cache
  9. *
  10. * Provides caching based on machine-generated PHP code on the filesystem.
  11. *
  12. * $Id: FileCache.inc.php,v 1.12 2007/09/21 16:38:34 asmecher Exp $
  13. */
  14. import('cache.GenericCache');
  15. class FileCache extends GenericCache {
  16. /**
  17. * Connection to use for caching.
  18. */
  19. var $filename;
  20. /**
  21. * The cached data
  22. */
  23. var $cache;
  24. /**
  25. * Instantiate a cache.
  26. */
  27. function FileCache($context, $cacheId, $fallback, $path) {
  28. parent::GenericCache($context, $cacheId, $fallback);
  29. $this->filename = $path . DIRECTORY_SEPARATOR . "fc-$context-" . str_replace('/', '.', $cacheId) . '.php';
  30. // Load the cache data if it exists.
  31. if (file_exists($this->filename)) {
  32. $this->cache = include($this->filename);
  33. } else {
  34. $this->cache = null;
  35. }
  36. }
  37. /**
  38. * Flush the cache
  39. */
  40. function flush() {
  41. unset($this->cache);
  42. $this->cache = null;
  43. if (file_exists($this->filename)) {
  44. unlink($this->filename);
  45. }
  46. }
  47. /**
  48. * Get an object from the cache.
  49. * @param $id
  50. */
  51. function getCache($id) {
  52. if (!isset($this->cache)) return $this->cacheMiss;
  53. return (isset($this->cache[$id])?$this->cache[$id]:null);
  54. }
  55. /**
  56. * Set an object in the cache. This function should be overridden
  57. * by subclasses.
  58. * @param $id
  59. * @param $value
  60. */
  61. function setCache($id, $value) {
  62. // Flush the cache; it will be regenerated on demand.
  63. $this->flush();
  64. }
  65. /**
  66. * Set the entire contents of the cache.
  67. */
  68. function setEntireCache(&$contents) {
  69. $fp = @fopen($this->filename, 'wb');
  70. // If the cache can be written, write it. If not, fall
  71. // back on NO CACHING AT ALL.
  72. if ($fp) {
  73. fwrite ($fp, '<?php return ' . var_export($contents, true) . '; ?>');
  74. fclose ($fp);
  75. }
  76. $this->cache =& $contents;
  77. }
  78. /**
  79. * Get the time at which the data was cached.
  80. * If the file does not exist or an error occurs, null is returned.
  81. * @return int
  82. */
  83. function getCacheTime() {
  84. if (!file_exists($this->filename)) return null;
  85. $result = filemtime($this->filename);
  86. if ($result === false) return null;
  87. return ((int) $result);
  88. }
  89. /**
  90. * Get the entire contents of the cache in an associative array.
  91. */
  92. function &getContents() {
  93. if (!isset($this->cache)) {
  94. // Trigger a cache miss to load the cache.
  95. $this->get(null);
  96. }
  97. return $this->cache;
  98. }
  99. }
  100. ?>