SisoDb – Cacheable queries

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.ICacheProvider and 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 ISisoDatabase instance.

db.CacheProvider = new MsMemCacheProvider ();

By default caching is of. You could either activate it per entity/structure or for all:

db.CacheProvider.EnableFor();

or

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.

//Daniel

4 thoughts on “SisoDb – Cacheable queries

  1. Hi.
    Under what conditions is the cache invalidated?
    Is that something users must do, or something that happens automatically?
    Jamie.

      • Can you share a little about how this applies?
        Does the entire cache get invalidated when 1 document is updated?
        Or all documents of that type?
        Or does just that 1 document get removed (and any queries that contained it)?

      • Hi,

        I will document it in the wiki and add a link here during tomorrow.

        //Daniel

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s