Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the entire second level cache in NHibernate

Tags:

I wish to clear the entire second level cache in NHibernate via code. Is there a way to do this which is independent of the cache provider being used? (we have customers using both memcache and syscache within the same application).

We wish to clear the entire cache because of changes external to the database may have occurred (and we have no guarantees re: which tables/entities were affected, so we have to assume the worst).

like image 284
Bittercoder Avatar asked Apr 18 '10 01:04

Bittercoder


People also ask

What is second-level caching?

A second-level cache is a local store of entity data managed by the persistence provider to improve application performance. A second-level cache helps improve performance by avoiding expensive database calls, keeping the entity data local to the application.

How does NHibernate cache work?

An NHibernate session has an internal (first-level) cache where it keeps its entities. There is no sharing between these caches - a first-level cache belongs to a given session and is destroyed with it. NHibernate provides a second-level cache system; it works at the session factory level.


1 Answers

This should do:

sessionFactory.EvictQueries(); foreach (var collectionMetadata in sessionFactory.GetAllCollectionMetadata())          sessionFactory.EvictCollection(collectionMetadata.Key); foreach (var classMetadata in sessionFactory.GetAllClassMetadata())          sessionFactory.EvictEntity(classMetadata.Key); 
like image 170
Diego Mijelshon Avatar answered Sep 28 '22 02:09

Diego Mijelshon