Suppose there are methods in the UserRepository class:
findByFirstname($firstname)
findByCity($city)
findByAge($minAge, $maxAge)

Also, let's say there is a PostRepository with a method:
findByUser($id)
findByName($postName)

Suppose all these methods have already been executed and the results of their execution are already in the cache.It is worth noting that, for example, if the findByCity() method was executed several times with different parameters, thencache after each execution>new result.

Suppose a user(User) deletes his account, and his posts are deleted along with it(I don’t know if this can be done, but that’s not the point).Now you need to delete the results from the cache in which this user is present, so that no one gets outdated data.But how to do it right? Does Doctrine have such functionality that only those results are automatically deleted from the cache in which this user is present that no longer exists?
I know that Doctrine has a QueryBuilder, which is used in the methods above.This object builds a query into the database and returns a query object(Query), from which you can call the useResultCache() method and pass an ID to this method, which you can then use delete the result(s) of this query from the cache.I know that it is possible to pass the same ID to different requests(Query objects) in useResultCache() and delete all the results of these queries from the cache by this ID.
I used EventManager and created a listener whose method works after processing EntityManager :: flush(), but the sad thing is that you need to write the delete logic yourself, that is, see which entity has been saved, deleted or updated and based on this information, delete certain results from the cache that contain outdated data.I doubt that Doctrine itself can figure it out, but maybe there are some ways that make this easier?

And another very important detail: suppose the cache contains 2 different results returned by the findByCity() method, the first result returned by the findByCity('Moscow') method contains User, which is already deleted, and in the second result, which the findByCity('Kyev') method returned, this User is not, because it is from Moscow.That is, the second result does not need to be deleted, it does not have a non-existent User, and therefore there is no outdated data.
But in order to selectively delete only those results that accurately contain outdated data, you need to check whether the remote User is in them.This is all too hemorrhaging, so it seems to me that it is better not to do such checks, but simply delete all results that could potentially contain outdated data.But even this seems too unpleasant to me, which will take a lot of time and in which you can make a lot of mistakes.

If there is literature or an article where it is adequately written about this, then tell me this.I found such an article https://www.gregfreeman.io/2012/invalidating-the-r... I have not tried it yet, because I still hope there are better ways.

0 Answers 0