This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Cache
Nicolas Grekas b22a58449c feature #35362 [Cache] Add LRU + max-lifetime capabilities to ArrayCache (nicolas-grekas)
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
-------

48a5d5e8a9 [Cache] Add LRU + max-lifetime capabilities to ArrayCache
2020-01-27 11:48:06 +01:00
..
Adapter [Cache] Add LRU + max-lifetime capabilities to ArrayCache 2020-01-24 18:00:05 +01:00
DataCollector Merge branch '4.4' into 5.0 2020-01-09 13:59:02 +01:00
DependencyInjection fix processing chain adapter based cache pool 2020-01-07 11:39:48 +01:00
Exception Revert "minor #32054 Prepare for PHP 7.4 preload (nicolas-grekas)" 2019-06-20 08:42:33 +02:00
Marshaller Merge branch '4.4' into 5.0 2020-01-23 12:07:12 +01:00
Tests feature #35362 [Cache] Add LRU + max-lifetime capabilities to ArrayCache (nicolas-grekas) 2020-01-27 11:48:06 +01:00
Traits Merge branch '4.4' into 5.0 2020-01-10 22:57:37 +01:00
.gitattributes Add .gitignore to .gitattributes 2019-10-12 01:35:04 +01:00
.gitignore
CacheItem.php Merge branch '4.4' 2019-09-11 10:39:10 +02:00
CHANGELOG.md [Cache] Add LRU + max-lifetime capabilities to ArrayCache 2020-01-24 18:00:05 +01:00
composer.json Merge branch '5.0' 2019-11-21 08:02:52 +01:00
DoctrineProvider.php Merge branch '4.3' into 4.4 2019-08-26 11:00:56 +02:00
LICENSE Update year in license files 2020-01-01 12:03:25 +01:00
LockRegistry.php Merge branch '4.4' 2019-09-27 00:15:24 +02:00
phpunit.xml.dist [Cache] Add optimized FileSystem & Redis TagAware Adapters 2019-04-24 07:47:35 +02:00
PruneableInterface.php add (pdo|chain) cache (adapter|simple) prune method 2017-08-30 11:10:37 -04:00
Psr16Cache.php Merge branch '4.3' into 4.4 2019-10-04 23:43:27 +02:00
README.md
ResettableInterface.php renamed Contract to Contracts 2018-07-13 19:06:58 +02:00

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.

Resources