Project Description

A very simple method caching mechanism for deterministic methods on the .Net 4.0 platform.

Given this class:   

  1. public class SomeService
  2. {        
  3.     public int CalculateTheMeaningOfLife()
  4.     {
  5.         //Assume some very time consuming calculations here....
  6.         return 42;
  7.     }
  8. }

We can add caching to this method by simply doing this:

  1. [EnableMethodCaching]
  2. public class SomeService
  3. {        
  4.     [Cached]
  5.     public int CalculateTheMeaningOfLife()
  6.     {
  7.         //Assume some very time consuming calculations here....
  8.         return 42;
  9.     }
  10. }

 

Getting started

For our target project to use the method cache, we need to create a reference to the MethodCaching.dll.

Next task is to determine what methods we would like to be cached.

Note: This caching mechanism should only be user for deterministic methods,
meaning methods that given the same input, they will always produce the same result.


PostWeaving

In order for our methods to be cached, we need to execute a post weaver after our target assembly has been compiled.

A post weaver in this context is an executable that modifies the assembly in such a way that caching is injected into our target assembly.

There is a default post weaver provided along with the library and the code for this (console) application is shown below:

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         var assemblyWeaver = new AssemblyWeaver(new TypeWeaver(new AttributeMethodSelector()),
  6.                                                 new AttributeTypeSelector());
  7.         assemblyWeaver.Weave(args[0]);
  8.     }
  9. }

Next we need to add a post build step to our target assembly so that this console application is executed after each build.

"[PathToExecutable]\MethodCaching.PostWeaver.exe" "$(TargetPath)"

That’it!!

Last edited May 11, 2011 at 7:06 PM by seesharper, version 8