b22a58449c
This PR was merged into the 5.1-dev branch.
Discussion
----------
[Cache] Add LRU + max-lifetime capabilities to ArrayCache
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Tickets | Fix https://github.com/orgs/symfony/projects/1#card-30686676
| License | MIT
| Doc PR | -
In https://github.com/symfony/symfony/pull/32294#issuecomment-508067745, @andrerom writes:
> if you plan to expose use of ArrayAdapter to a wider audience you should probably also add the following features to it:
> - max item limit to avoid reaching memory limits
> - own (very low, like default 100-500ms) TTL for in-memory caching, as it's in practice stale data when used in concurrent scenarios
>
> If you want to be advance you can also:
>
> - keep track of use, and evict cache items based on that using LFU when reaching limit
> - in-memory cache is domain & project specific in terms of how long it's somewhat "safe" to keep items in memory, so either describe when to use and not use on a per pool term, or allow use of pool to pass in flags to opt out of in-memory cache for cases developer knows it should be ignored
This PR implements these suggestions, via two new constructor arguments: `$maxLifetime` and `$maxItems`.
In Yaml:
```yaml
services:
app.lru150_cache:
parent: cache.adapter.array
arguments:
$maxItems: 150
$maxLifetime: 0.150
framework:
cache:
pools:
my_chained_pool:
adapters:
- app.lru150_cache
- cache.adapter.filesystem
```
This configuration adds a local memory cache that keeps max 150 elements for 150ms on top of a filesystem cache.
/cc @lyrixx since you were also interested in it.
Commits
-------
|
||
---|---|---|
.. | ||
Adapter | ||
DataCollector | ||
DependencyInjection | ||
Exception | ||
Marshaller | ||
Tests | ||
Traits | ||
.gitattributes | ||
.gitignore | ||
CacheItem.php | ||
CHANGELOG.md | ||
composer.json | ||
DoctrineProvider.php | ||
LICENSE | ||
LockRegistry.php | ||
phpunit.xml.dist | ||
PruneableInterface.php | ||
Psr16Cache.php | ||
README.md | ||
ResettableInterface.php |
Symfony PSR-6 implementation for caching
This component provides an extended PSR-6
implementation for adding cache to your applications. It is designed to have a
low overhead so that caching is fastest. It ships with a few caching adapters
for the most widespread and suited to caching backends. It also provides a
doctrine/cache
proxy adapter to cover more advanced caching needs and a proxy
adapter for greater interoperability between PSR-6 implementations.