This weekend I just release SisoDb v16.1.0 and about a week before that v16.0.0. I thought we might take a look at some new features with regards to caching. From version 16.0.0 of SisoDb you can not only make use of a cache provider for
GetById, GetByIds but also for queries. First you need to enable caching. You can easily write one on your own, by implementing
SisoDb.Caching.ICache. But for this post we will use an existing one, provided via NuGet.
PM > Install-package SisoDb.MsMemoryCache
Now you can enable it to you
db.CacheProvider = new MsMemCacheProvider ();
By default caching is of. You could either activate it per entity/structure or for all:
db.CacheProvider.AutoEnable = true;
This is something you would do while configuring your singleton instance of your
ISisoDatabase e.g while bootstraping your app and configuring your IoC.
Now, when the cache is enabled you get implicit cache for:
session.GetById | GetByIds | GetByQuery
but you can also make specific queries cacheable:
session.Query() .Where(i => i.Score > minScore) .OrderBy(i => i.Score) .Cacheable();
That’s it! If you now execute this query two times and have the same value on
minScore, it will only hit the Db the first time.