Skip to content

Commit 8bf7455

Browse files
committed
constants are PascalCase
1 parent 295bda5 commit 8bf7455

34 files changed

+283
-271
lines changed

readme.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,15 @@ The validity of the data is set at the time of saving using the third parameter
118118

119119
```php
120120
$cache->save($key, $value, [
121-
Cache::EXPIRE => '20 minutes',
121+
Cache::Expire => '20 minutes',
122122
]);
123123
```
124124

125125
Or using the `$dependencies` parameter passed by reference to the callback in the `load()` method, eg:
126126

127127
```php
128128
$value = $cache->load($key, function (&$dependencies) {
129-
$dependencies[Cache::EXPIRE] = '20 minutes';
129+
$dependencies[Cache::Expire] = '20 minutes';
130130
return ...;
131131
]);
132132
```
@@ -141,27 +141,27 @@ The simplest exiration is the time limit. Here's how to cache data valid for 20
141141

142142
```php
143143
// it also accepts the number of seconds or the UNIX timestamp
144-
$dependencies[Cache::EXPIRE] = '20 minutes';
144+
$dependencies[Cache::Expire] = '20 minutes';
145145
```
146146

147147
If we want to extend the validity period with each reading, it can be achieved this way, but beware, this will increase the cache overhead:
148148

149149
```php
150-
$dependencies[Cache::SLIDING] = true;
150+
$dependencies[Cache::Sliding] = true;
151151
```
152152

153153
The handy option is the ability to let the data expire when a particular file is changed or one of several files. This can be used, for example, for caching data resulting from procession these files. Use absolute paths.
154154

155155
```php
156-
$dependencies[Cache::FILES] = '/path/to/data.yaml';
156+
$dependencies[Cache::Files] = '/path/to/data.yaml';
157157
// nebo
158-
$dependencies[Cache::FILES] = ['/path/to/data1.yaml', '/path/to/data2.yaml'];
158+
$dependencies[Cache::Files] = ['/path/to/data1.yaml', '/path/to/data2.yaml'];
159159
```
160160

161161
We can let an item in the cache expired when another item (or one of several others) expires. This can be used when we cache the entire HTML page and fragments of it under other keys. Once the snippet changes, the entire page becomes invalid. If we have fragments stored under keys such as `frag1` and `frag2`, we will use:
162162

163163
```php
164-
$dependencies[Cache::ITEMS] = ['frag1', 'frag2'];
164+
$dependencies[Cache::Items] = ['frag1', 'frag2'];
165165
```
166166

167167
Expiration can also be controlled using custom functions or static methods, which always decide when reading whether the item is still valid. For example, we can let the item expire whenever the PHP version changes. We will create a function that compares the current version with the parameter, and when saving we will add an array in the form `[function name, ...arguments]` to the dependencies:
@@ -172,16 +172,16 @@ function checkPhpVersion($ver): bool
172172
return $ver === PHP_VERSION_ID;
173173
}
174174

175-
$dependencies[Cache::CALLBACKS] = [
175+
$dependencies[Cache::Callbacks] = [
176176
['checkPhpVersion', PHP_VERSION_ID] // expire when checkPhpVersion(...) === false
177177
];
178178
```
179179

180180
Of course, all criteria can be combined. The cache then expires when at least one criterion is not met.
181181

182182
```php
183-
$dependencies[Cache::EXPIRE] = '20 minutes';
184-
$dependencies[Cache::FILES] = '/path/to/data.yaml';
183+
$dependencies[Cache::Expire] = '20 minutes';
184+
$dependencies[Cache::Files] = '/path/to/data.yaml';
185185
```
186186

187187

@@ -192,22 +192,22 @@ Invalidation using Tags
192192
Tags are a very useful invalidation tool. We can assign a list of tags, which are arbitrary strings, to each item stored in the cache. For example, suppose we have an HTML page with an article and comments, which we want to cache. So we specify tags when saving to cache:
193193

194194
```php
195-
$dependencies[Cache::TAGS] = ["article/$articleId", "comments/$articleId"];
195+
$dependencies[Cache::Tags] = ["article/$articleId", "comments/$articleId"];
196196
```
197197

198198
Now, let's move to the administration. Here we have a form for article editing. Together with saving the article to a database, we call the `clean()` command, which will delete cached items by tag:
199199

200200
```php
201201
$cache->clean([
202-
Cache::TAGS => ["article/$articleId"],
202+
Cache::Tags => ["article/$articleId"],
203203
]);
204204
```
205205

206206
Likewise, in the place of adding a new comment (or editing a comment), we will not forget to invalidate the relevant tag:
207207

208208
```php
209209
$cache->clean([
210-
Cache::TAGS => ["comments/$articleId"],
210+
Cache::Tags => ["comments/$articleId"],
211211
]);
212212
```
213213

@@ -222,14 +222,14 @@ Invalidation by Priority
222222
We can set the priority for individual items in the cache, and it will be possible to delete them in a controlled way when, for example, the cache exceeds a certain size:
223223

224224
```php
225-
$dependencies[Cache::PRIORITY] = 50;
225+
$dependencies[Cache::Priority] = 50;
226226
```
227227

228228
Delete all items with a priority equal to or less than 100:
229229

230230
```php
231231
$cache->clean([
232-
Cache::PRIORITY => 100,
232+
Cache::Priority => 100,
233233
]);
234234
```
235235

@@ -239,11 +239,11 @@ Priorities require so-called [Journal](#Journal).
239239
Clear Cache
240240
-----------
241241

242-
The `Cache::ALL` parameter clears everything:
242+
The `Cache::All` parameter clears everything:
243243

244244
```php
245245
$cache->clean([
246-
Cache::ALL => true,
246+
Cache::All => true,
247247
]);
248248
```
249249

src/Bridges/CacheLatte/CacheMacro.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static function initRuntime(Latte\Runtime\Template $template): void
9494
if (!empty($template->global->cacheStack)) {
9595
$file = (new \ReflectionClass($template))->getFileName();
9696
if (@is_file($file)) { // @ - may trigger error
97-
end($template->global->cacheStack)->dependencies[Cache::FILES][] = $file;
97+
end($template->global->cacheStack)->dependencies[Cache::Files][] = $file;
9898
}
9999
}
100100
}
@@ -119,7 +119,7 @@ public static function createCache(
119119
}
120120

121121
if ($parents) {
122-
end($parents)->dependencies[Cache::ITEMS][] = $key;
122+
end($parents)->dependencies[Cache::Items][] = $key;
123123
}
124124

125125
$cache = new Cache($cacheStorage, 'Nette.Templating.Cache');
@@ -150,8 +150,8 @@ public static function endCache(array &$parents, ?array $args = null): void
150150
$args['expiration'] = $args['expire']; // back compatibility
151151
}
152152

153-
$helper->dependencies[Cache::TAGS] = $args['tags'] ?? null;
154-
$helper->dependencies[Cache::EXPIRATION] = $args['expiration'] ?? '+ 7 days';
153+
$helper->dependencies[Cache::Tags] = $args['tags'] ?? null;
154+
$helper->dependencies[Cache::Expire] = $args['expiration'] ?? '+ 7 days';
155155
$helper->end();
156156
}
157157

src/Caching/Cache.php

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,32 @@ class Cache
2121

2222
/** dependency */
2323
public const
24-
PRIORITY = 'priority',
25-
EXPIRATION = 'expire',
26-
EXPIRE = 'expire',
27-
SLIDING = 'sliding',
28-
TAGS = 'tags',
29-
FILES = 'files',
30-
ITEMS = 'items',
31-
CONSTS = 'consts',
32-
CALLBACKS = 'callbacks',
33-
NAMESPACES = 'namespaces',
34-
ALL = 'all';
24+
Priority = 'priority',
25+
Expire = 'expire',
26+
Sliding = 'sliding',
27+
Tags = 'tags',
28+
Files = 'files',
29+
Items = 'items',
30+
Constants = 'consts',
31+
Callbacks = 'callbacks',
32+
Namespaces = 'namespaces',
33+
All = 'all';
34+
35+
public const
36+
PRIORITY = self::Priority,
37+
EXPIRATION = self::Expire,
38+
EXPIRE = self::Expire,
39+
SLIDING = self::Sliding,
40+
TAGS = self::Tags,
41+
FILES = self::Files,
42+
ITEMS = self::Items,
43+
CONSTS = self::Constants,
44+
CALLBACKS = self::Callbacks,
45+
NAMESPACES = self::Namespaces,
46+
ALL = self::All;
3547

3648
/** @internal */
37-
public const NAMESPACE_SEPARATOR = "\x00";
49+
public const NamespaceSeparator = "\x00";
3850

3951
/** @var Storage */
4052
private $storage;
@@ -46,7 +58,7 @@ class Cache
4658
public function __construct(Storage $storage, ?string $namespace = null)
4759
{
4860
$this->storage = $storage;
49-
$this->namespace = $namespace . self::NAMESPACE_SEPARATOR;
61+
$this->namespace = $namespace . self::NamespaceSeparator;
5062
}
5163

5264

@@ -156,13 +168,13 @@ public function bulkLoad(array $keys, ?callable $generator = null): array
156168
/**
157169
* Writes item into the cache.
158170
* Dependencies are:
159-
* - Cache::PRIORITY => (int) priority
160-
* - Cache::EXPIRATION => (timestamp) expiration
161-
* - Cache::SLIDING => (bool) use sliding expiration?
162-
* - Cache::TAGS => (array) tags
163-
* - Cache::FILES => (array|string) file names
164-
* - Cache::ITEMS => (array|string) cache items
165-
* - Cache::CONSTS => (array|string) cache items
171+
* - Cache::Priortiy => (int) priority
172+
* - Cache::Exprie => (timestamp) expiration
173+
* - Cache::Sliding => (bool) use sliding expiration?
174+
* - Cache::Tags => (array) tags
175+
* - Cache::Files => (array|string) file names
176+
* - Cache::Items => (array|string) cache items
177+
* - Cache::Consts => (array|string) cache items
166178
*
167179
* @param mixed $key
168180
* @param mixed $data
@@ -187,7 +199,7 @@ public function save($key, $data, ?array $dependencies = null)
187199
$this->storage->remove($key);
188200
} else {
189201
$dependencies = $this->completeDependencies($dependencies);
190-
if (isset($dependencies[self::EXPIRATION]) && $dependencies[self::EXPIRATION] <= 0) {
202+
if (isset($dependencies[self::Expire]) && $dependencies[self::Expire] <= 0) {
191203
$this->storage->remove($key);
192204
} else {
193205
$this->storage->write($key, $data, $dependencies);
@@ -201,41 +213,41 @@ public function save($key, $data, ?array $dependencies = null)
201213
private function completeDependencies(?array $dp): array
202214
{
203215
// convert expire into relative amount of seconds
204-
if (isset($dp[self::EXPIRATION])) {
205-
$dp[self::EXPIRATION] = Nette\Utils\DateTime::from($dp[self::EXPIRATION])->format('U') - time();
216+
if (isset($dp[self::Expire])) {
217+
$dp[self::Expire] = Nette\Utils\DateTime::from($dp[self::Expire])->format('U') - time();
206218
}
207219

208220
// make list from TAGS
209-
if (isset($dp[self::TAGS])) {
210-
$dp[self::TAGS] = array_values((array) $dp[self::TAGS]);
221+
if (isset($dp[self::Tags])) {
222+
$dp[self::Tags] = array_values((array) $dp[self::Tags]);
211223
}
212224

213225
// make list from NAMESPACES
214-
if (isset($dp[self::NAMESPACES])) {
215-
$dp[self::NAMESPACES] = array_values((array) $dp[self::NAMESPACES]);
226+
if (isset($dp[self::Namespaces])) {
227+
$dp[self::Namespaces] = array_values((array) $dp[self::Namespaces]);
216228
}
217229

218230
// convert FILES into CALLBACKS
219-
if (isset($dp[self::FILES])) {
220-
foreach (array_unique((array) $dp[self::FILES]) as $item) {
221-
$dp[self::CALLBACKS][] = [[self::class, 'checkFile'], $item, @filemtime($item) ?: null]; // @ - stat may fail
231+
if (isset($dp[self::Files])) {
232+
foreach (array_unique((array) $dp[self::Files]) as $item) {
233+
$dp[self::Callbacks][] = [[self::class, 'checkFile'], $item, @filemtime($item) ?: null]; // @ - stat may fail
222234
}
223235

224-
unset($dp[self::FILES]);
236+
unset($dp[self::Files]);
225237
}
226238

227239
// add namespaces to items
228-
if (isset($dp[self::ITEMS])) {
229-
$dp[self::ITEMS] = array_unique(array_map([$this, 'generateKey'], (array) $dp[self::ITEMS]));
240+
if (isset($dp[self::Items])) {
241+
$dp[self::Items] = array_unique(array_map([$this, 'generateKey'], (array) $dp[self::Items]));
230242
}
231243

232244
// convert CONSTS into CALLBACKS
233-
if (isset($dp[self::CONSTS])) {
234-
foreach (array_unique((array) $dp[self::CONSTS]) as $item) {
235-
$dp[self::CALLBACKS][] = [[self::class, 'checkConst'], $item, constant($item)];
245+
if (isset($dp[self::Constants])) {
246+
foreach (array_unique((array) $dp[self::Constants]) as $item) {
247+
$dp[self::Callbacks][] = [[self::class, 'checkConst'], $item, constant($item)];
236248
}
237249

238-
unset($dp[self::CONSTS]);
250+
unset($dp[self::Constants]);
239251
}
240252

241253
if (!is_array($dp)) {
@@ -259,15 +271,15 @@ public function remove($key): void
259271
/**
260272
* Removes items from the cache by conditions.
261273
* Conditions are:
262-
* - Cache::PRIORITY => (int) priority
263-
* - Cache::TAGS => (array) tags
264-
* - Cache::ALL => true
274+
* - Cache::Priority => (int) priority
275+
* - Cache::Tags => (array) tags
276+
* - Cache::All => true
265277
*/
266278
public function clean(?array $conditions = null): void
267279
{
268280
$conditions = (array) $conditions;
269-
if (isset($conditions[self::TAGS])) {
270-
$conditions[self::TAGS] = array_values((array) $conditions[self::TAGS]);
281+
if (isset($conditions[self::Tags])) {
282+
$conditions[self::Tags] = array_values((array) $conditions[self::Tags]);
271283
}
272284

273285
$this->storage->clean($conditions);

0 commit comments

Comments
 (0)