FileCache.php in Drupal 10
File
core/lib/Drupal/Component/FileCache/FileCache.php
View source
<?php
namespace Drupal\Component\FileCache;
class FileCache implements FileCacheInterface {
protected $prefix;
protected static $cached = [];
protected $collection;
protected $cache;
public function __construct($prefix, $collection, $cache_backend_class = NULL, array $cache_backend_configuration = []) {
if (empty($prefix)) {
throw new \InvalidArgumentException('Required prefix configuration is missing');
}
$this->prefix = $prefix;
$this->collection = $collection;
if (isset($cache_backend_class)) {
$this->cache = new $cache_backend_class($cache_backend_configuration);
}
}
public function get($filepath) {
$filepaths = [
$filepath,
];
$cached = $this
->getMultiple($filepaths);
return $cached[$filepath] ?? NULL;
}
public function getMultiple(array $filepaths) {
$file_data = [];
$remaining_cids = [];
foreach ($filepaths as $filepath) {
if (!file_exists($filepath)) {
continue;
}
$realpath = realpath($filepath);
if (empty($realpath)) {
continue;
}
$cid = $this->prefix . ':' . $this->collection . ':' . $realpath;
if (isset(static::$cached[$cid]) && static::$cached[$cid]['mtime'] == filemtime($filepath)) {
$file_data[$filepath] = static::$cached[$cid]['data'];
}
else {
$remaining_cids[$cid] = $filepath;
}
}
if ($remaining_cids && $this->cache) {
$cache_results = $this->cache
->fetch(array_keys($remaining_cids)) ?: [];
foreach ($cache_results as $cid => $cached) {
$filepath = $remaining_cids[$cid];
if ($cached['mtime'] == filemtime($filepath)) {
$file_data[$cached['filepath']] = $cached['data'];
static::$cached[$cid] = $cached;
}
}
}
return $file_data;
}
public function set($filepath, $data) {
$realpath = realpath($filepath);
$cached = [
'mtime' => filemtime($filepath),
'filepath' => $filepath,
'data' => $data,
];
$cid = $this->prefix . ':' . $this->collection . ':' . $realpath;
static::$cached[$cid] = $cached;
if ($this->cache) {
$this->cache
->store($cid, $cached);
}
}
public function delete($filepath) {
$realpath = realpath($filepath);
$cid = $this->prefix . ':' . $this->collection . ':' . $realpath;
unset(static::$cached[$cid]);
if ($this->cache) {
$this->cache
->delete($cid);
}
}
public static function reset() {
static::$cached = [];
}
}
Classes
Name |
Description |
FileCache |
Allows to cache data based on file modification dates. |