c# - Fody - method caching -


i using fody method cache (https://github.com/dresel/methodcache) first time. doing wrong because following code not work:

static void main() {   console.writeline("begin calc 1...");   var v = calc(5);   console.writeline("begin calc 2..."); //it last same first function call   v = calc(5);   console.writeline("end calc 2..."); }   [cache]  static int calc(int b)  {    thread.sleep(5000);    return b + 5;  } 

what should use following: first call: cache arguments keys , return value value. other call: if cache[arg1, arg2,...] exist return cache value without completing function ? (using cache attribute)

as stated in github issue, static method caching added in 1.3.1.

as methodcache.fody designed, have add cache getter class contains methods should cached , implement cache. can program own cache or use adapter existing cache solutions (see documentation of https://github.com/dresel/methodcache).

the minimum code sample (with basic dictionary cache implementation) this:

namespace consoleapplication {     using system;     using system.collections.generic;     using system.threading;     using methodcache.attributes;      public class program     {         private static dictionarycache cache { get; set; }           [cache]         private static int calc(int b)         {             thread.sleep(5000);             return b + 5;         }          private static void main(string[] args)         {             cache = new dictionarycache();              console.writeline("begin calc 1...");             var v = calc(5);              // return cached value             console.writeline("begin calc 2...");             v = calc(5);              console.writeline("end calc 2...");         }     }      public class dictionarycache     {         public dictionarycache()         {             storage = new dictionary<string, object>();         }          private dictionary<string, object> storage { get; set; }          // note: methods contains, retrieve, store must following:          public bool contains(string key)         {             return storage.containskey(key);         }          public t retrieve<t>(string key)         {             return (t)storage[key];         }          public void store(string key, object data)         {             storage[key] = data;         }     } } 

however more sophisticated solution use service class, icache interface getter , contructor injection of cache. icache wrap existing caching solutions.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo