PluginProbe
MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings / 5.8.4
MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings v5.8.4
7.2.2 7.2.1 7.2 7.1.2 7.1.1 7.1 7.0.4 7.0.6 7.0.7 6.3.8 6.3.7 6.3.6 6.3.5 6.3.4 6.3.3 6.3.1 trunk 5.7.3 5.7.5 5.8.1 5.8.2 5.8.3 5.8.4 5.8.6 6.0.4 All 37 releases
mlsimport / vendor / phpcsstandards / phpcsutils / PHPCSUtils / Internal / NoFileCache.php

NoFileCache.php in MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings 5.8.4, at vendor/phpcsstandards/phpcsutils/PHPCSUtils/Internal/NoFileCache.php

165 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
4 *
5 * @package PHPCSUtils
6 * @copyright 2019-2020 PHPCSUtils Contributors
7 * @license https://opensource.org/licenses/LGPL-3.0 LGPL3
8 * @link https://github.com/PHPCSStandards/PHPCSUtils
9 */
10
11 namespace PHPCSUtils\Internal;
12
13 /**
14 * Cache for function results which are not directly related to a $phpcsFile.
15 *
16 * Allows to cache the return value of utility functions which don't use PHPCS File objects.
17 *
18 * Caching the results can significantly speed things up, though it can also eat memory,
19 * so use with care.
20 *
21 * Typical usage:
22 * ```php
23 * function doSomething()
24 * {
25 * if (NoFileCache::isCached(__METHOD__, $paramHash) === true) {
26 * return NoFileCache::get(__METHOD__, $paramHash);
27 * }
28 *
29 * // Do something.
30 *
31 * NoFileCache::set(__METHOD__, $paramHash, $returnValue);
32 * return $returnValue;
33 * }
34 * ```
35 *
36 * ---------------------------------------------------------------------------------------------
37 * This class is only intended for internal use by PHPCSUtils and is not part of the public API.
38 * This also means that it has no promise of backward compatibility.
39 * ---------------------------------------------------------------------------------------------
40 *
41 * @internal
42 *
43 * @since 1.0.0
44 */
45 final class NoFileCache
46 {
47
48 /**
49 * Whether caching is enabled or not.
50 *
51 * Note: this switch is ONLY intended for use within test suites and should never
52 * be touched in any other circumstances!
53 *
54 * Don't forget to always turn the cache back on in a `tear_down()` method!
55 *
56 * @since 1.0.0
57 *
58 * @var bool
59 */
60 public static $enabled = true;
61
62 /**
63 * Results cache.
64 *
65 * @since 1.0.0
66 *
67 * @var array<string, array<int|string, mixed>> Format: $cache[$key][$id] = mixed $value;
68 */
69 private static $cache = [];
70
71 /**
72 * Check whether a result has been cached for a certain utility function.
73 *
74 * @since 1.0.0
75 *
76 * @param string $key The key to identify a particular set of results.
77 * It is recommended to pass `__METHOD__` to this parameter.
78 * @param int|string $id Unique identifier for these results.
79 * It is recommended for this to be a serialization of arguments passed
80 * to the function or an md5 hash of an input.
81 *
82 * @return bool
83 */
84 public static function isCached($key, $id)
85 {
86 return self::$enabled === true && isset(self::$cache[$key]) && \array_key_exists($id, self::$cache[$key]);
87 }
88
89 /**
90 * Retrieve a previously cached result for a certain utility function.
91 *
92 * @since 1.0.0
93 *
94 * @param string $key The key to identify a particular set of results.
95 * It is recommended to pass `__METHOD__` to this parameter.
96 * @param int|string $id Unique identifier for these results.
97 * It is recommended for this to be a serialization of arguments passed
98 * to the function or an md5 hash of an input.
99 *
100 * @return mixed
101 */
102 public static function get($key, $id)
103 {
104 if (self::$enabled === true && isset(self::$cache[$key]) && \array_key_exists($id, self::$cache[$key])) {
105 return self::$cache[$key][$id];
106 }
107
108 return null;
109 }
110
111 /**
112 * Retrieve all previously cached results for a certain utility function.
113 *
114 * @since 1.0.0
115 *
116 * @param string $key The key to identify a particular set of results.
117 * It is recommended to pass `__METHOD__` to this parameter.
118 *
119 * @return array<int|string, mixed>
120 */
121 public static function getForKey($key)
122 {
123 if (self::$enabled === true && \array_key_exists($key, self::$cache)) {
124 return self::$cache[$key];
125 }
126
127 return [];
128 }
129
130 /**
131 * Cache the result for a certain utility function.
132 *
133 * @since 1.0.0
134 *
135 * @param string $key The key to identify a particular set of results.
136 * It is recommended to pass `__METHOD__` to this parameter.
137 * @param int|string $id Unique identifier for these results.
138 * It is recommended for this to be a serialization of arguments passed
139 * to the function or an md5 hash of an input.
140 * @param mixed $value An arbitrary value to write to the cache.
141 *
142 * @return mixed
143 */
144 public static function set($key, $id, $value)
145 {
146 if (self::$enabled === false) {
147 return;
148 }
149
150 self::$cache[$key][$id] = $value;
151 }
152
153 /**
154 * Clear the cache.
155 *
156 * @since 1.0.0
157 *
158 * @return void
159 */
160 public static function clear()
161 {
162 self::$cache = [];
163 }
164 }
165