@@ -5,7 +5,7 @@ Caching based on resources
5
5
==========================
6
6
7
7
When all configuration resources are loaded, you may want to process the configuration values and
8
- combine them all in one file. This file acts like a cache. It’s contents don’t have to be
8
+ combine them all in one file. This file acts like a cache. Its contents don’t have to be
9
9
regenerated every time the application runs – only when the configuration resources are modified.
10
10
11
11
For example, the Symfony Routing component allows you to load all routes, and then dump a URL
@@ -16,39 +16,41 @@ invalidated and regenerated. This can be accomplished by making use of the
16
16
17
17
The example below shows you how to collect resources, then generate some code based on the
18
18
resources that were loaded, and write this code to the cache. The cache also receives the
19
- collection of resources that were used for generating the code. By looking at the “ last modified”
20
- timestamp of these resources, the cache can tell if it is still fresh or that it’s contents should
19
+ collection of resources that were used for generating the code. By looking at the " last modified"
20
+ timestamp of these resources, the cache can tell if it is still fresh or that its contents should
21
21
be regenerated.
22
22
23
23
.. code-block :: php
24
24
25
25
use Symfony\Component\Config\ConfigCache;
26
+ use Symfony\Component\Config\Resource\FileResource;
26
27
27
- // $yamlUserFiles is filled before with an array of 'users.yml' file paths
28
-
29
- $resources = array();
30
-
31
- foreach ($yamlUserFiles as $yamlUserFile) {
32
- $resources[] = new FileResource($yamlUserFile);
33
- }
34
-
35
- $cachePath = __DIR__ . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'appUserMatcher.php';
28
+ $cachePath = __DIR__.'/cache/appUserMatcher.php';
36
29
37
30
$userMatcherCache = new ConfigCache($cachePath, true);
38
31
// the second constructor argument indicates whether or not we are in debug mode
39
32
40
33
if (!$userMatcherCache->isFresh()) {
41
- foreach ($resources as $resource) {
42
- $delegatingLoader->load($resource->getResource());
34
+ // fill this with an array of 'users.yml' file paths
35
+ $yamlUserFiles = ...;
36
+
37
+ $resources = array();
38
+
39
+ foreach ($yamlUserFiles as $yamlUserFile) {
40
+ $delegatingLoader->load($yamlUserFile);
41
+ $resources[] = new FileResource($yamlUserFile);
43
42
}
44
43
45
44
// The code for the UserMatcher is generated elsewhere
46
- // $code = ...;
45
+ $code = ...;
46
+
47
47
$userMatcherCache->write($code, $resources);
48
48
}
49
49
50
50
// you may want to require the cached code:
51
51
require $cachePath;
52
52
53
- A ``.meta `` file is created in the same directory as the cache file itself. This ``.meta `` file
54
- contains the serialized resources, for later reference.
53
+ In debug mode, a ``.meta `` file will be created in the same directory as the cache file itself.
54
+ This ``.meta `` file contains the serialized resources, whose timestamps are used to determine if
55
+ the cache is still fresh. When not in debug mode, the cache is considered to be "fresh" as soon
56
+ as it exists, and therefore no ``.meta `` file will be generated.
0 commit comments