2

I am planning to implement an identity map for a small project which is not using any ORM tool.

The standard implementation in most examples I have seen is just a hash by object id, however it is obvious that the hash will grow without limits. I was thinking in using memcahed or redis with a cache expiration, but that also means that some objects will expire in the cache and their data will be fetched once time again from the database under a new and different object (same entity under different objects in memory).

Considering that most ORMs do not require a running memcached/redis. How do they solve this problem? Do they actually solve it? is it not a concern having an entity represented by repeated instances?

The only solution I know of is in languages supporting smart pointers and storing weak references within the hash. It does not look to me that such approach could be taken with Ruby, so I am wondering how is this pattern normally implemented by Ruby ORMs.

SystematicFrank
  • 16,555
  • 7
  • 56
  • 102
  • In my experience the issue with having to lean on identity maps is directly tied to ActiveRecord making it easy and convenient to query objects into memory. Consider using the Repository pattern which Uncle Bob talks about it in [Architecture: The Lost Years](http://www.confreaks.com/videos/759-rubymidwest2011-keynote-architecture-the-lost-years). If you abstract your repository into its own class you can build your identity map directly into that. – Mario Aug 14 '14 at 16:28

1 Answers1

1

I think they do use a Hash, certainly appears that DataMapper uses a hash. My presumption is that the identity map is per 'session', which is probably flushed after each request (also ensures transactions are flushed by the end of request boundary). Thus it can grow unbounded but has a fixed horizon that clears it. If the intention is to have a session that lasts longer and needs periodic cleaning then WeakRef might be useful. I would be cautious about maintaining an identity map for an extended period however, particularly if concurrency is involved and there are any expectations for consistent transactional changes. I know ActiveRecord considered adding an IdentityMap and then abandoned that effort. Depending on how rows are fetched there may be duplication, but it's probably less then you would think, OR the query should be re-thought.

dgtized
  • 3,192
  • 2
  • 24
  • 23
  • I thought about identity map per session before, but I do not remember any ORM where the "session" concept of a multi tenant system was handed down to the ORM. My code follows some DDD practices where identity is based on a unique ID field, not the instance memory address... but not trusting the uniqueness of my objects means more data out of sync than I originally gave for assured. I made a more generic question at http://programmers.stackexchange.com/questions/253285/limitations-of-the-identity-map-pattern – SystematicFrank Aug 14 '14 at 10:14