Skip to content
This repository has been archived by the owner on Oct 8, 2020. It is now read-only.

Caching

Daniel Wertheim edited this page Nov 29, 2012 · 7 revisions

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.

Premade cache providers

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.

Hook in the cache

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();

AutoEnable

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.

Manually enable

If AutoEnable=false you need to manually register what type of structures should be cached.

db.CacheProvider.EnableFor(typeof(MyStructure));

That's it.

Clone this wiki locally