Skip to content
A memoization library for .net
C# Batchfile
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.


Type Name Latest commit message Commit time
Failed to load latest commit information.
Build .NET standard 2.0 support Aug 13, 2019
MbCache.ProxyImpl.LinFu .NET standard 2.0 support Aug 13, 2019
MbCache Minor improvement of code doc Jan 28, 2020
MbCacheTest .NET standard 2.0 support Aug 13, 2019
.editorconfig Adding settings file for EditorConfig. Apr 16, 2013
.gitignore .NET standard 2.0 support Aug 13, 2019 Adding MIT license file Jan 27, 2016
MbCache.sln adding createRelease.bat to sln Aug 13, 2019
MbCache.sln.DotSettings following naming conventions from R# file. Jun 25, 2014 Update Apr 18, 2019
ReleaseNotes.txt typo in release notes Aug 13, 2019

MbCache, or Method based Cache, is a memoization library for .net. It is used to define certain methods to be cached in a centralized way, independent on the underlying caching framework.

How often have you written c# similar to this?

public class Calculator : ICalculator
 public SomeType Calculate()
   var cachedValue = tryGetFromCache(yourKey);
   if(cachedValue != null)
     return cachedValue;
   var value = someExpensiveOperation();
   putInCache(yourKey, value);
   return value;

It sure works. There are some drawback though:

  • A snippet like this tends to repeat itself the next time you want to cache some return value in some other method. Not really DRY-ish.
  • You (might) have a strong dependency to your cache framework. With code like this it's not easy to reuse code among different sort of clients. You might, for example, want to use cache when code is run in web enviroment but something completely different in a desktop application.
  • What if you suddenly want to turn off caching for a method? Or some of them? All of them?
  • SRP is broken. It can eg be solved using decorator pattern and split calculation and caching part, but it will lead to even more handwritten code.
  • Can be hard to test.

There's an alternative that solves this for you - MbCache!

  1. Write your business logic. No need to mess this up with cache logic.

    public SomeType Calculate()
      return someExpensiveOperation();  
  2. Tell MbCache at start up what methods to cache (preferably together with your favorite ioc container).

    var builder = new CacheBuilder();
      .CacheMethod(c => c.Calculate())
    _factory = builder.BuildFactory();
  3. Use your objects. ICalculate.Calculate() will now use caching.

    var yourService =_factory.Create<ICalculator>()
    var value = yourService.Calculate();
You can’t perform that action at this time.