Allow changing the CacheMode when using 2nd level cache #7403

Open
opened 2026-01-22 15:51:19 +01:00 by admin · 0 comments
Owner

Originally created by @petrisorciprian-vitals on GitHub (Aug 8, 2024).

Feature Request

Q A
New Feature yes
RFC no
BC Break no

Summary

The query cache mode using in L2 cache is defined in-line, when constructing the QueryCacheKey:

https://github.com/doctrine/orm/blob/3.2.x/src/Cache/Persister/Entity/AbstractEntityPersister.php#L305

https://github.com/doctrine/orm/blob/3.2.x/src/Cache/Persister/Entity/AbstractEntityPersister.php#L344

https://github.com/doctrine/orm/blob/3.2.x/src/Cache/Persister/Entity/AbstractEntityPersister.php#L436

This is hinted in the documentation and is a useful concept for controlling how the cache is being used, here:
https://www.doctrine-project.org/projects/doctrine-orm/en/3.2/reference/second-level-cache.html#cache-mode

However, this only works in context of AbstractQuery and QueryBuilder, it is not directly modifiable for queries such as $this->repository->findBy(<>), which use a hardcoded cache mode value of CacheMode::NORMAL.

My use case: I am remotely controlling the cache mode for my application, and would like L2 cache to be responsive of this too, not just queries issue via AbstractQuery or QueryBuilder.

To achieve this, I would like to roll my own 2nd level CacheFactory, which is allowed by the Doctrine Bundle for Symfony via orm.entity_managers.default.second_level_cache.factory, and decorate the EntityPersisters created by the factory.

Then, if the CachedPersister interface would expose a setter and getter for the 'cacheMode', and the AbstractEntityPersister used the getter when constructing the QueryCacheKey instead of hard-coding the cache mode, it would also be possible to control the caching mode for L2 cache.

So, in summary, this would require:

  • two new interface methods for the CachedPersister, getCacheMode and setCacheMode
  • changing the AbstractEntityPersister to use getCacheMode instead of hard-coding the value to CacheMode::NORMAL at all times

I can raise a PR with this change, provided a maintainer reviews this idea and approves of it.

Thank you!

Originally created by @petrisorciprian-vitals on GitHub (Aug 8, 2024). ### Feature Request | Q | A |------------ | ------ | New Feature | yes | RFC | no | BC Break | no #### Summary The query cache mode using in L2 cache is defined in-line, when constructing the QueryCacheKey: https://github.com/doctrine/orm/blob/3.2.x/src/Cache/Persister/Entity/AbstractEntityPersister.php#L305 https://github.com/doctrine/orm/blob/3.2.x/src/Cache/Persister/Entity/AbstractEntityPersister.php#L344 https://github.com/doctrine/orm/blob/3.2.x/src/Cache/Persister/Entity/AbstractEntityPersister.php#L436 This is hinted in the documentation and is a useful concept for controlling how the cache is being used, here: https://www.doctrine-project.org/projects/doctrine-orm/en/3.2/reference/second-level-cache.html#cache-mode However, this only works in context of AbstractQuery and QueryBuilder, it is not directly modifiable for queries such as `$this->repository->findBy(<>)`, which use a hardcoded cache mode value of `CacheMode::NORMAL`. My use case: I am remotely controlling the cache mode for my application, and would like L2 cache to be responsive of this too, not just queries issue via `AbstractQuery` or `QueryBuilder`. To achieve this, I would like to roll my own 2nd level [CacheFactory](https://github.com/doctrine/orm/blob/3.2.x/src/Cache/CacheFactory.php), which is allowed by the Doctrine Bundle for Symfony via `orm.entity_managers.default.second_level_cache.factory`, and decorate the `EntityPersisters` created by the factory. Then, if the [ CachedPersister](https://github.com/doctrine/orm/blob/3.2.x/src/Cache/Persister/CachedPersister.php#L12) interface would expose a setter and getter for the 'cacheMode', and the AbstractEntityPersister used the getter when constructing the QueryCacheKey instead of hard-coding the cache mode, it would also be possible to control the caching mode for L2 cache. So, in summary, this would require: - two new interface methods for the `CachedPersister`, `getCacheMode` and `setCacheMode` - changing the AbstractEntityPersister to use `getCacheMode` instead of hard-coding the value to `CacheMode::NORMAL` at all times I can raise a PR with this change, provided a maintainer reviews this idea and approves of it. Thank you!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7403