-
Notifications
You must be signed in to change notification settings - Fork 25
Caching
SisoDb is designed to be simple and to be low consuming on memory. Hence by default there is no caching turned on or configured. But you can easily assign a cache-provider to Db.CacheProvider
or using `Db.Configure().UseCacheProvider(myCacheProvider).
All you need to do is to implement the two interfaces ICacheProvider
and ICache
. Then SisoDb will call the methods on these as needed. By default, when there is an activated cache-provider for a type, only: GetById
, GetByIds
and GetByQuery
constructs makes use of the cache. You can of course make queries be cached as well:
session.Query<Customer>()
.Where(c => c.Points >= 1000 and c.Points <= 2000)
.Cacheable(); //Activates cache for this query
By using Cacheable()
a checksum will be generated for the SQL-query as well as its parameters. The next time a cache-enabled query matches this signature, the cache-provider is used instead of hitting the DB. SisoDb will maintain the contents of the cache for you, when you insert, delete and update structures.
There are currently only one simple premade cache-provider: SisoDb.MsMemoryCache
; which is implemented against the Microsoft memory cache, and therefore can be used in both web- and non-web enironments.
It's dead simple. When creating your long lived database (one per application), you just assign an implementation like this:
var cnInfo = new Sql2008ConnectionInfo("my connectionString");
var db = new Sql2008DbFactory().CreateDatabase(cnInfo);
db.CacheProvider = new MsMemCacheProvider();
By default, the official providers has Provider.AutoEnable=false
this is of course something you can change, and when sat to true
everything is enabled to be cached.
If AutoEnable=false
you need to manually register what type of structures should be cached.
db.CacheProvider.EnableFor(typeof(MyStructure));
That's it.