Entity Framework and NHibernate. Is caching responsible for the service level?

Before using ORM, we always cached objects at our service level. This gave us the ability to switch between different levels of data without having to change our caching implementation.

We currently use both the Entity Framework (mainly the code first) and NHibernate. NHibernate seems to have much better caching features with several second-tier cache servers available.

Another issue that I ran into is that for both of the above ORMs we use lazy loaded properties. Therefore, if we retrieve an object from the cache, we usually need to bind it to the current ObjectContext / ISession, which we cannot do at our service level.

So, I really have to look at the repository / data level caching implementation; and is it likely that I will find a common solution that will work for EF and NH?

Thanks Ben

+4
source share
2 answers

Have you looked at the CQRS template? Since caching is a policy , I am suspiciously trying to automate these decisions with a β€œone size fits all” cache, like a second level cache. For example, if what you really want to cache is HTML for the home page of your site, then the second level cache is just a waste of memory and an error message.

CQRS turns these mechanical considerations into political decisions. And this is an ORM agnostic.

+4
source

My experience is that the second level cache provided by ORM will cache data based on the actual set of results obtained from the database. This can lead to a lot of overhead because the object instance and data set are quite expensive.

The advantage is that lazy loading, etc. will work fine, but great results will still consume a lot of resources when refilling objects.

We use a combination of a second level cache and ASP.NET cache in our web application because of expensive overhead, which in rare cases saves up to several seconds (!), But with the inability of lazy collections to load or update objects.

This is based only on NHibernate, I have never worked with an entity infrastructure, but I assume that all ORM frameworks suffer from this.

+2
source

Source: https://habr.com/ru/post/1338950/


All Articles