Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #4
0
 internal static void RemoveKeysetContext(KeysetId setId)
 {
     lock (setLock)
     {
         if (CacheSetKeys.ContainsKey(setId.SetId))
         {
             CacheSetKeys.Remove(setId.SetId);
         }
     }
 }
Beispiel #5
0
        public static bool CreateKeysetIfNotExists(KeysetId setId)
        {
            if (CacheSet.GetKeysetContext(setId, false) == null)
            {
                CacheSet.GetKeysetContext(setId, true);
                return(true);
            }

            return(false);
        }
Beispiel #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));
        }
Beispiel #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());
        }
Beispiel #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);
        }
Beispiel #9
0
 public static bool InvalidateCacheSet(KeysetId setId)
 {
     return(CacheSet.Invalidate(setId));
 }
Beispiel #10
0
 public static bool CacheObject(KeysetId setId, bool hashKeyDataBeforeCache, string cacheKeyData, object result)
 {
     return(CacheObject(setId, hashKeyDataBeforeCache, cacheKeyData, result, TimeSpan.MinValue));
 }
Beispiel #11
0
 public static bool CacheObject(KeysetId setId, string key, object obj)
 {
     return(CacheObject(setId, key, obj, TimeSpan.MinValue));
 }
Beispiel #12
0
 internal static CacheKeySetContext GetKeysetContext(KeysetId setId)
 {
     return(GetKeysetContext(setId, true));
 }