orientdb-odm
orientdb-odm copied to clipboard
Implement Identity Map pattern
An identity map is a database access design pattern used to improve performance by providing a context-specific in-memory cache to prevent duplicate retrieval of the same object data from the database
http://en.wikipedia.org/wiki/Identity_map
@davidino this is not trivial
what I mean is that if you have a record, you change a property, save it. Then you fetch it back with the identity map and you have the outdated version
im totally up for this, lets just bare in mind these cases
The idea with the identity map is that you can then rely on the fact that one entry from the DB means just one object on the PHP side. So if you modify it and save it, it is still in memory, so you won't need to "fetch it back": the identity map will return the already modified version. No problem here.
However it may be difficult to make sure that every object returned by queries are either:
- added to the identity map if they are not in it
- fetched from the identity map if they are already in it
One thing cool with that pattern too is that you can do: if ($object1 === $object2)
rather than if ($object1->getId() == $object2->getId())
.
There are memory issues to consider as well, since the lifecycle of records fetched from the database becomes entirely different: records now live for much longer in the cache, which can become an issue for someone performing batch operations on large quantities of objects - automatic caching can add complexity for the programmer in those cases, when they have to manually manage the cache, and transaction boundaries can sort of blur in code that needs to manage the cache.