Don't like ads? PRO users don't see any ads ;-)
Guest

lazy and cache

By: ronklein on Aug 12th, 2012  |  syntax: C#  |  size: 0.36 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public static MyData GetMyDataFromDb() { ... }
  2.  
  3. Lazy<MyData> lazy = null;
  4. lock(myStaticLock) {
  5.         lazy = (Lazy<MyData>)Cache["myKey"];
  6.         if (null == lazy) {
  7.                 lazy = new Lazy<MyData>(GetMyDataFromDb, LazyThreadSafetyMode.ExecutionAndPublication);
  8.                 Cache["myKey"] = lazy;
  9.         }
  10. }
  11. // now we have an instance of Lazy<MyData> what so ever
  12. MyData data = lazy.Value; // !!