You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+17-17Lines changed: 17 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -118,15 +118,15 @@ The validity of the data is set at the time of saving using the third parameter
118
118
119
119
```php
120
120
$cache->save($key, $value, [
121
-
Cache::EXPIRE => '20 minutes',
121
+
Cache::Expire => '20 minutes',
122
122
]);
123
123
```
124
124
125
125
Or using the `$dependencies` parameter passed by reference to the callback in the `load()` method, eg:
126
126
127
127
```php
128
128
$value = $cache->load($key, function (&$dependencies) {
129
-
$dependencies[Cache::EXPIRE] = '20 minutes';
129
+
$dependencies[Cache::Expire] = '20 minutes';
130
130
return ...;
131
131
]);
132
132
```
@@ -141,27 +141,27 @@ The simplest exiration is the time limit. Here's how to cache data valid for 20
141
141
142
142
```php
143
143
// it also accepts the number of seconds or the UNIX timestamp
144
-
$dependencies[Cache::EXPIRE] = '20 minutes';
144
+
$dependencies[Cache::Expire] = '20 minutes';
145
145
```
146
146
147
147
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:
148
148
149
149
```php
150
-
$dependencies[Cache::SLIDING] = true;
150
+
$dependencies[Cache::Sliding] = true;
151
151
```
152
152
153
153
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.
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:
162
162
163
163
```php
164
-
$dependencies[Cache::ITEMS] = ['frag1', 'frag2'];
164
+
$dependencies[Cache::Items] = ['frag1', 'frag2'];
165
165
```
166
166
167
167
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
172
172
return $ver === PHP_VERSION_ID;
173
173
}
174
174
175
-
$dependencies[Cache::CALLBACKS] = [
175
+
$dependencies[Cache::Callbacks] = [
176
176
['checkPhpVersion', PHP_VERSION_ID] // expire when checkPhpVersion(...) === false
177
177
];
178
178
```
179
179
180
180
Of course, all criteria can be combined. The cache then expires when at least one criterion is not met.
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:
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:
199
199
200
200
```php
201
201
$cache->clean([
202
-
Cache::TAGS => ["article/$articleId"],
202
+
Cache::Tags => ["article/$articleId"],
203
203
]);
204
204
```
205
205
206
206
Likewise, in the place of adding a new comment (or editing a comment), we will not forget to invalidate the relevant tag:
207
207
208
208
```php
209
209
$cache->clean([
210
-
Cache::TAGS => ["comments/$articleId"],
210
+
Cache::Tags => ["comments/$articleId"],
211
211
]);
212
212
```
213
213
@@ -222,14 +222,14 @@ Invalidation by Priority
222
222
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:
223
223
224
224
```php
225
-
$dependencies[Cache::PRIORITY] = 50;
225
+
$dependencies[Cache::Priority] = 50;
226
226
```
227
227
228
228
Delete all items with a priority equal to or less than 100:
0 commit comments