Distributed Lock Service with Windows Server AppFabric Caching -


i have extension method microsoft.applicationserver.caching.datacache object found in windows server appfabric sdk looks this:

using system; using system.collections.generic; using microsoft.applicationserver.caching;  namespace caching {     public static class cacheextensions     {         private static dictionary<string, object> locks = new dictionary<string, object>();          public static t fetch<t>(this datacache @this, string key, func<t> func)         {             return @this.fetch(key, func, timespan.fromseconds(30));         }          public static t fetch<t>(this datacache @this, string key, func<t> func, timespan timeout)         {             var result = @this.get(key);              if (result == null)             {                 lock (getlock(key))                 {                     result = @this.get(key);                      if (result == null)                     {                         result = func();                          if (result != null)                         {                             @this.put(key, result, timeout);                         }                     }                 }             }              return (t)result;         }          private static object getlock(string key)         {             object @lock = null;              if (!locks.trygetvalue(key, out @lock))             {                 lock (locks)                 {                     if (!locks.trygetvalue(key, out @lock))                     {                         @lock = new object();                         locks.add(key, @lock);                     }                 }             }              return @lock;         }     } } 

the intent let developer write code says, "fetch me data trying cache first. if it's not available in cache execute specified function, put results in cache next caller, return results". this:

var data = datacache.fetch("key", () => somelongrunningoperation()); 

the locking limits executing potentially long running function call single thread within single process on same machine. how expand on pattern make locking distributed prevent multiple processes/machines executing function @ once?

appfabric has it's own distributed locking mechanism can access through getandlock/putandunlock family of methods. if item locked, normal get call still succeed , return last value, further getandlock calls throw exception. in case client requesting cached object first time, can still lock key though doesn't exist yet (it's kind of more reservation solid lock).

public static t fetch<t>(this datacache @this, string key, func<t> func, timespan timeout) {     var result = @this.get(key);      if (result == null)     (         datacachelockhandle handle;         // need timespan allow func time run         timespan functimespan = new timespan(0,1,0);          try         {             // lock key             // if goes wrong here unlock @ end of functimespan             var result = @this.getandlock(key, functimespan, handle);              if (result == null)             {                 // still no value go , run func                 result = func();                  @this.putandunlock(key, result, handle, timeout);             }             else             {                 // there's value we'll unlock key , reset it's timeout                 @this.unlock(key, handle, timeout);             }         }         catch (datacacheexception ex)         {             if (ex.errorcode == datacacheerrorcode.objectlocked)             {                 // process has locked key func must running right                 // we'll return null client                 result = null;             }         }          if (result == null)         {             return null;         }         else         {             return (t)result;         }     ) } 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -