Open Journal Systems  3.3.0
FileCache.inc.php
1 <?php
2 
22 import('lib.pkp.classes.cache.GenericCache');
23 
24 class FileCache extends GenericCache {
28  var $filename;
29 
33  var $cache;
34 
38  function __construct($context, $cacheId, $fallback, $path) {
39  parent::__construct($context, $cacheId, $fallback);
40 
41  $this->filename = $path . DIRECTORY_SEPARATOR . "fc-$context-" . str_replace('/', '.', $cacheId) . '.php';
42 
43  // Load the cache data if it exists.
44  if (($fp = @fopen($this->filename, 'r')) !== false) {
45  flock($fp, LOCK_SH);
46  $this->cache = include($this->filename);
47  flock($fp, LOCK_UN);
48  } else {
49  $this->cache = null;
50  }
51  }
52 
56  function flush() {
57  unset($this->cache);
58  $this->cache = null;
59  if (function_exists('opcache_invalidate')) opcache_invalidate($this->filename, true);
60  @unlink($this->filename);
61  }
62 
67  function getCache($id) {
68  if (!isset($this->cache)) return $this->cacheMiss;
69  return (isset($this->cache[$id])?$this->cache[$id]:null);
70  }
71 
78  function setCache($id, $value) {
79  // Flush the cache; it will be regenerated on demand.
80  $this->flush();
81  }
82 
86  function setEntireCache($contents) {
87  if (file_put_contents(
88  $this->filename,
89  '<?php return ' . var_export($contents, true) . ';',
90  LOCK_EX
91  ) !== false) {
92  $umask = Config::getVar('files', 'umask');
93  if ($umask) @chmod($this->filename, FILE_MODE_MASK & ~$umask);
94  }
95  $this->cache = $contents;
96  }
97 
103  function getCacheTime() {
104  $result = @filemtime($this->filename);
105  if ($result === false) return null;
106  return ((int) $result);
107  }
108 
112  function &getContents() {
113  if (!isset($this->cache)) {
114  // Trigger a cache miss to load the cache.
115  $this->get(null);
116  }
117  return $this->cache;
118  }
119 }
120 
121 
FileCache
Provides caching based on machine-generated PHP code on the filesystem.
Config\getVar
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
Http\Message\Decorator\getContents
getContents()
Definition: StreamDecorator.php:129
GenericCache
Provides implementation-independent caching. Although this class is intended to be overridden with a ...
Definition: GenericCache.inc.php:23