示例#1
0
        public static bool CacheObject(KeysetId setId, string key, object obj, TimeSpan validFor)
        {
            bool result;

            if (setId != null)
            {
                if (!CacheSet.AddKey(setId, key))
                {
                    Log.Warning("cacheKeyset for {0} could not be added. (key={1})", setId, key);
                }
            }

            if (validFor == TimeSpan.MinValue)
            {
                result = Program.DataCacheInstance.Instance.Set(key, obj);
            }
            else
            {
                result = Program.DataCacheInstance.Instance.Set(key, obj, validFor);
            }

            if (!result)
            {
                Log.Warning("obj({0}) could not be cached for key:{1}", obj.GetType(), key);
                return(false);
            }

            return(true);
        }
示例#2
0
        internal static bool Invalidate(KeysetId setId)
        {
            HashSet <string> sets;

            CacheKeySetContext cksc = GetKeysetContext(setId, false);

            if (cksc == null)
            {
                return(false);
            }

            lock (cksc.lck)
            {
                if (!CacheManager.TryGetCachedResult <HashSet <string> >(cksc.CacheSetKey, out sets))
                {
                    Log.Warning("Not expected situation. cache key set does not exists for setId:{0}", setId);
                    RemoveKeysetContext(setId);
                    return(false);
                }

                foreach (var key in sets)
                {
                    CacheManager.Remove(key);
                }

                Log.Info("{0} key records invalidated from the keyset ({1})", sets.Count, setId);

                sets.Clear();

                CacheManager.CacheObject(cksc.CacheSetKey, sets);
            }


            return(true);
        }
示例#3
0
        internal static bool AddKey(KeysetId setId, string key)
        {
            CacheKeySetContext cskc;
            HashSet <string>   set;

            cskc = GetKeysetContext(setId);

            if (!CacheManager.TryGetCachedResult <HashSet <string> >(cskc.CacheSetKey, out set))
            {
                set = new HashSet <string>();
                set.Add(key);
                CacheManager.CacheObject(cskc.CacheSetKey, set);
            }
            else
            {
                lock (cskc.lck)
                {
                    if (setId.ValidateKeys)
                    {
                        CheckSetKeys(set);
                    }

                    if (!set.Contains(key))
                    {
                        set.Add(key);
                    }
                }

                CacheManager.CacheObject(cskc.CacheSetKey, set);
            }

            return(true);
        }
示例#4
0
 internal static void RemoveKeysetContext(KeysetId setId)
 {
     lock (setLock)
     {
         if (CacheSetKeys.ContainsKey(setId.SetId))
         {
             CacheSetKeys.Remove(setId.SetId);
         }
     }
 }
示例#5
0
        public static bool CreateKeysetIfNotExists(KeysetId setId)
        {
            if (CacheSet.GetKeysetContext(setId, false) == null)
            {
                CacheSet.GetKeysetContext(setId, true);
                return(true);
            }

            return(false);
        }
示例#6
0
        public static bool CacheObject(KeysetId setId, bool hashKeyDataBeforeCache, string cacheKeyData, object result, TimeSpan validFor)
        {
            string key;

            if (hashKeyDataBeforeCache)
            {
                key = CalculateCacheKey(cacheKeyData);
            }
            else
            {
                key = cacheKeyData;
            }

            return(CacheObject(setId, key, result, validFor));
        }
示例#7
0
        public static string[] GetKeysFromKeySet(KeysetId setId)
        {
            HashSet <string> set;

            var cksc = CacheSet.GetKeysetContext(setId, false);

            if (cksc == null)
            {
                return(null);
            }

            TryGetCachedResult <HashSet <string> >(cksc.CacheSetKey, out set);

            if (set == null)
            {
                return(null);
            }

            return(set.ToArray());
        }
示例#8
0
        internal static CacheKeySetContext GetKeysetContext(KeysetId setId, bool createIfNotExists)
        {
            CacheKeySetContext cskc = null;

            lock (setLock)
            {
                if (!CacheSetKeys.TryGetValue(setId.SetId, out cskc))
                {
                    if (createIfNotExists)
                    {
                        cskc             = new CacheKeySetContext();
                        cskc.CacheSetKey =
                            "CS_" + Helper.Md5(Config.Get().CacheSetSalt + setId.SetId);

                        CacheSetKeys.Add(setId.SetId, cskc);
                    }
                }
            }

            return(cskc);
        }
示例#9
0
 public static bool InvalidateCacheSet(KeysetId setId)
 {
     return(CacheSet.Invalidate(setId));
 }
示例#10
0
 public static bool CacheObject(KeysetId setId, bool hashKeyDataBeforeCache, string cacheKeyData, object result)
 {
     return(CacheObject(setId, hashKeyDataBeforeCache, cacheKeyData, result, TimeSpan.MinValue));
 }
示例#11
0
 public static bool CacheObject(KeysetId setId, string key, object obj)
 {
     return(CacheObject(setId, key, obj, TimeSpan.MinValue));
 }
示例#12
0
 internal static CacheKeySetContext GetKeysetContext(KeysetId setId)
 {
     return(GetKeysetContext(setId, true));
 }