|
1 | 1 | # Caching metadata in Symfony cache |
| 2 | + |
| 3 | +Networking and I/O are often the source of performance problems in applications. |
| 4 | +On top of this, Flysystem generally aims to be as reliable as possible, which |
| 5 | +means it will often check and validate operations before they are done. This |
| 6 | +involves an additional overhead on top of the classical slowness of I/O. |
| 7 | + |
| 8 | +When your application needs to scale, you may need to cache metadata in order to |
| 9 | +improve performances on this level. To do this, you can use the `cache` adapter |
| 10 | +in this bundle. |
| 11 | + |
| 12 | +### Installation |
| 13 | + |
| 14 | +``` |
| 15 | +composer require league/flysystem-cached-adapter |
| 16 | +``` |
| 17 | + |
| 18 | +### Usage |
| 19 | + |
| 20 | +```yaml |
| 21 | +# config/packages/flysystem.yaml |
| 22 | + |
| 23 | +flysystem: |
| 24 | + storages: |
| 25 | + users.storage.source: |
| 26 | + adapter: 'aws' |
| 27 | + options: |
| 28 | + client: 'aws_client_service' |
| 29 | + bucket: 'bucket_name' |
| 30 | + prefix: 'optional/path/prefix' |
| 31 | + |
| 32 | + users.storage: |
| 33 | + adapter: 'cache' |
| 34 | + options: |
| 35 | + store: 'psr6_cache_pool' # A service ID implementing Psr\Cache\CacheItemPoolInterface |
| 36 | + source: 'users.storage.source' |
| 37 | +``` |
| 38 | +
|
| 39 | +However, this configuration is generic. Most of the time you are only |
| 40 | +going to need one of two possibilities: |
| 41 | +
|
| 42 | +* memory cache: this cache will expire at the end of the current CLI process or |
| 43 | + HTTP request ; |
| 44 | +* persistant cache: this cache will only expire when you clear it ; |
| 45 | +
|
| 46 | +#### Memory caching |
| 47 | +
|
| 48 | +To enable memory cache, you can create a dedicated service based on the Symfony |
| 49 | +Cache component: |
| 50 | +
|
| 51 | +```yaml |
| 52 | +# config/packages/flysystem.yaml |
| 53 | + |
| 54 | +services: |
| 55 | + users.storage.cache: |
| 56 | + class: 'Symfony\Component\Cache\Adapter\ArrayAdapter' |
| 57 | + |
| 58 | +flysystem: |
| 59 | + storages: |
| 60 | + # ... |
| 61 | + |
| 62 | + users.storage: |
| 63 | + adapter: 'cache' |
| 64 | + options: |
| 65 | + store: 'users.storage.cache' |
| 66 | + source: 'users.storage.source' |
| 67 | +``` |
| 68 | +
|
| 69 | +#### Persistent caching |
| 70 | +
|
| 71 | +To enable persistent cache, you can either define your own service in the same way |
| 72 | +as the memory cache, or rely on the `cache.app` service provided automatically by |
| 73 | +Symfony: |
| 74 | + |
| 75 | +```yaml |
| 76 | +# config/packages/flysystem.yaml |
| 77 | +
|
| 78 | +flysystem: |
| 79 | + storages: |
| 80 | + # ... |
| 81 | +
|
| 82 | + users.storage: |
| 83 | + adapter: 'cache' |
| 84 | + options: |
| 85 | + store: 'cache.app' |
| 86 | + source: 'users.storage.source' |
| 87 | +``` |
0 commit comments