10

Our project uses Entity Framework and has 2 type of cache (in-memory, Redis) without any cache provider. Due to lack of second-level cache support in EF we implemented it ourself. So our in-memory cache is simple set of key value pair where key is Id of cache and value is the cached object. We also implemented similar caching to use Redis. For query we look at in-memory cache list and if it is not ther we look at redis and if it does not there we query against the database.

Because entity framework entities has reference to context we can not use entity of DbContext in caching and we need to map it. So we need create a lot of DTOs.

I know that cache is cross-cutting concern so I look for cleaner solution. For this reason, first I decide to use Memcached for In-Memory (instead of using simple list). As second and most importantly I may migrate from EF to NHibernate for its Second-Cache support. I know that first level cache is occupied by session object. So I want to use Memcached for second level cache. But is there any third-level cache for Redis?

Community
  • 1
  • 1
Seyed Morteza Mousavi
  • 6,855
  • 8
  • 43
  • 69
  • About DTOs do you have problems using entities with proxy (of DbContext)? Usually the problems arise when you try to access to related properties not loaded because of the lazy loader that cannot access to a disposed context but you have the same issue with detached entities (in both cases you need to use Include). Again about DTO I implement always Clone starting implementing a shallow clone (but it depends on entity) and is often (always) a fields values copy (using a custom function similar to MemberwiseClone but not MemberwiseClone otherwise you have again the whole proxy). – bubi Nov 16 '15 at 14:56
  • @bubi I don't want compromise my domain model for persistence framework. Creating DTOs is something like Momento Pattern. Secondly I want a good cache provider that entity framework has'nt. – Seyed Morteza Mousavi Nov 17 '15 at 08:24
  • You don't compromise the model using the approach above. I think you did but if you didn't you could have a look to 2nd level cache implementations for EF. About Hibernate I suggest you to try if for your purpose works fine. I can say that now (for new projects) I'm using EF while old projects are still in Hibernate. The problem about Hibernate is that often you need to materialize queries very early. If you think to work in memory often/always it works fine but if you need to work on DB you sometimes need to write HQL or SQL (or to insert ToList() early in the query). – bubi Nov 17 '15 at 13:35
  • @bubi Can you elaborate more in details in an answer post? Thanks anyway. – Seyed Morteza Mousavi Nov 22 '15 at 07:40

1 Answers1

2

One way of implementing a second level cache without creating a lot of DTOs is to use property name value list in a key value list instead of storing a DTO. As per my understanding there is no third level cache in NHibernate. One way of doing this is to implement a custom cache provider which could first look in the Redis and if it can't find it then get values from the Memcached.

Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43