问题
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)
回答1:
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
回答2:
If you are OK with the possibility of having some stale data, just set the default expiration to something you are comfortable with, and you'll be set.
Example:
<property name="cache.default_expiration">120</property>
This sets the default expiration to two minutes, so you'll never see stale data older than that.
来源:https://stackoverflow.com/questions/2985749/nhibernate-second-level-cache-with-external-updates