Click here to Skip to main content
15,936,466 members
Articles / General Programming / Usability

Using PostSharp to cache methods

Rate me:
Please Sign up or sign in to vote.
4.80/5 (2 votes)
9 May 2014CPOL2 min read 18.6K   176   7   6


Using PostSharp's assembly modifications you can quite easily cut method consumption time down by caching methods. The library will find a marked cached method, and every call will be checked for with similar arguments, if the same arguments were found, it will immediately return the cached return value of the function call earlier.


This trick uses a library called PostSharp, which has modifies your assembly. This allows you to track method calls and much more. Caching is not an old trick and has been around for a long time. Most web browsers use web page caching, and in this article I will show you how to cache methods within your assembly. Caching methods is a very interesting concept. To use this trick you must know what "caching your method" really means. Caching your method stores your method's return value after a call. Once the cached method is called once the arguments, return value, and method go into a dictionary.

Setting up your project

Before you start modifying your project you need to have included the article's source package. Make sure that you have also installed PostSharp via NuGet or by download. After installation, you also need to add PostSharp as a reference to your project.

Image 1

After clicking "Add Reference...", A dialog should then appear. Make sure you click Browse -> Browse...

Then find PostSharp and click "OK", If you have done everything successfully, PostSharp should now be listed in your references.

Repeat this process instead by adding the CacheLibrary to your project's references.

Using the code

This library will work the best for methods that produce constant results, given the same arguments. This means that your method(s) should not use variables that involve time or randomness. To mark a method simply add the attribute StoreInCache to your method.

public static int Foo(int bar)
    return bar;


5/8/14 - Updated CacheLibrary with new functionalities.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionGreat! Pin
Volynsky Alex8-May-14 23:48
professionalVolynsky Alex8-May-14 23:48 
QuestionOne query regarding method caching Pin
Tridip Bhattacharjee7-May-14 20:57
professionalTridip Bhattacharjee7-May-14 20:57 
AnswerRe: One query regarding method caching Pin
austinbox8-May-14 11:51
austinbox8-May-14 11:51 
GeneralRe: One query regarding method caching Pin
Tridip Bhattacharjee8-May-14 21:18
professionalTridip Bhattacharjee8-May-14 21:18 
GeneralRe: One query regarding method caching Pin
austinbox9-May-14 13:04
austinbox9-May-14 13:04 
The library just got updated into the Post. Try redownloading it now.
bool cached = CacheManager.IsCached("Mycachedfunction");

The function returns wether the method has any cached data given the method name and arguments.
I this case Mycachedfunction() is a parameterless function so I just called the function just using the method name.
With this function you can determine that method is not hitting db rather return 100 records from cache.

It seems you're trying to cache from a database, if you want Mycachedfunction to stop giving cached results you can call:

This method clears all of the cached memory, this way your function "Mycachedfunction" will now give results from the database. Remember, the library is open-source, if there is a functionality you want, you can try messing around with it. Smile | :)
QuestionNice! Pin
Volynsky Alex6-May-14 22:33
professionalVolynsky Alex6-May-14 22:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.