Search code examples
nhibernatecachingsyscache

NHibernate second-level cache with external updates


I've got a web application that is 99% read-only, with a separate service that updates the database at specific intervals (like every 10 minutes). How can this service tell the application to invalidate it's second-level cache? Is it actually important? (I don't actually care if I have too much stale data) If I don't invalidate the cache how much time is needed to the records to get updated (if using SysCache)


Solution

  • You can manually dispose your 2nd level cache for a specific entity, entity type or a collection.

    From http://knol.google.com/k/fabio-maulo/nhibernate-chapter-16-improving/1nr4enxv3dpeq/19#

    For the second-level cache, there are methods defined on ISessionFactory for evicting the cached state of an instance, entire class, collection instance or entire collection role.

    sessionFactory.Evict(typeof(Cat), catId); //evict a particular Cat
    sessionFactory.Evict(typeof(Cat));  //evict all Cats
    sessionFactory.EvictCollection("Eg.Cat.Kittens", catId); //evict a particular collection of kittens
    sessionFactory.EvictCollection("Eg.Cat.Kittens"); //evict all kitten collections