public static void AddChat(string room_name, string speaker, string words) { //Trace.TraceInformation("ChatPond.AddChat. room_name={0}.", room_name); ChatRoll roll = get(room_name); roll.Add(speaker, words); }
public static IEnumerable <UserWords> Get(string room_name) { // When Get and AddChat are called in different threads at very close time, both will trigger insertNew. // 2 rolls of the same room are inserted into cache, latter one kicks former one out of cache. //Trace.TraceInformation("ChatPond.Get. room_name={0}.", room_name); ChatRoll roll = get(room_name); return(roll.Get()); }
private static ChatRoll get(string room_name) { ChatRoll obj = (ChatRoll)HttpRuntime.Cache.Get(key(room_name)); if (obj == null) { obj = insertNew(room_name); } return(obj); }
private static ChatRoll insertNew(string room_name) { string key2 = key(room_name); //Trace.TraceInformation("ChatPond.insertNew. key={0}.", key2); ChatRoll obj = new ChatRoll(room_name); // if concurrency happens, this will be done more than once. HttpRuntime.Cache.Insert(key2, obj, null, DateTime.Now.AddSeconds(Config.DEFAULT_CACHE_SECONDS), Cache.NoSlidingExpiration, CacheItemPriority.Default, removedCallback); // expires after some time for multi-node synchronization (unreliable). return(obj); }
private static void removedCallback(string key, Object value, CacheItemRemovedReason reason) { // Reason=Removed happens when an existing key is inserted again. //Trace.TraceInformation("ChatPond.removedCallback. key={0}, reason={1}.", key, reason); ChatRoll obj = (ChatRoll)value; obj.Save(); if (reason == CacheItemRemovedReason.Expired) { // insertNew(key); // Not needed to keep it always in memory. } }