FileCache.php 3.87 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php

/**
 * @file
 * Contains \Drupal\Component\FileCache\FileCache.
 */

namespace Drupal\Component\FileCache;

/**
 * Allows to cache data based on file modification dates.
 */
class FileCache implements FileCacheInterface {

  /**
   * Prefix that is used for cache entries.
   *
   * @var string
   */
  protected $prefix;

  /**
   * Static cache that contains already loaded cache entries.
   *
   * @var array
   */
  protected static $cached = [];

  /**
   * The collection identifier of this cache.
   *
   * @var string
   */
  protected $collection;

  /**
   * The cache backend backing this FileCache object.
   *
   * @var \Drupal\Component\FileCache\FileCacheBackendInterface
   */
  protected $cache;

  /**
   * Constructs a FileCache object.
   *
   * @param string $prefix
   *   The cache prefix.
   * @param string $collection
   *   A collection identifier to ensure that the same files could be cached for
   *   different purposes without clashing.
   * @param string|null $cache_backend_class
   *   (optional) The class that should be used as cache backend.
   * @param array $cache_backend_configuration
   *   (optional) The configuration for the backend class.
   */
  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);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function get($filepath) {
    $filepaths = [$filepath];
    $cached = $this->getMultiple($filepaths);
    return isset($cached[$filepath]) ? $cached[$filepath] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getMultiple(array $filepaths) {
    $file_data = [];
    $remaining_cids = [];

    // First load from the static cache what we can.
    foreach ($filepaths as $filepath) {
      if (!file_exists($filepath)) {
        continue;
      }

      $realpath = realpath($filepath);
      // If the file exists but realpath returns nothing, it is using a stream
      // wrapper, those are not supported.
      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 {
        // Collect a list of cache IDs that we still need to fetch from cache
        // backend.
        $remaining_cids[$cid] = $filepath;
      }
    }

    // If there are any cache IDs left to fetch from the cache backend.
    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;
  }

  /**
   * {@inheritdoc}
   */
  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);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function delete($filepath) {
    $realpath = realpath($filepath);
    $cid = $this->prefix . ':' . $this->collection . ':' . $realpath;

    unset(static::$cached[$cid]);
    if ($this->cache) {
      $this->cache->delete($cid);
    }
  }

}