コード例 #1
0
        private static void CacheClear()
        {
            string[] typesKeys = GetTypesKeys();

            foreach (string typeName in typesKeys)
            {
                SingleTypeCacheConfig config = SingleTypeCacheConfig.Get(typeName);
                Dictionary <string, MyCacheObject> caches = GetCaches(typeName);

                #region 固定缓存
                if (config.IsFix)
                {
                    if (config.NextFixLoggingTime < DateTime.Now)
                    {
                        config.NextFixLoggingTime = DateTime.Now.AddMinutes(FixLoggingInterval);
                        LogProxy.InfoFormat(CACHEISFIX, typeName, caches.Count);
                    }

                    continue;
                }
                #endregion

                OverdueTimeCacheClear(caches, typeName, config);

                if (config.Size > 0)
                {
                    OverSizeCacheClear(caches, typeName, config);
                }
            }
        }
コード例 #2
0
        public ICacheable Get(Type type, string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return(null);
            }

            string fullName = type.FullName;

            if (!IsNeedCached(fullName))
            {
                return(null);
            }

            SingleTypeCacheConfig config = SingleTypeCacheConfig.Get(fullName);

            Dictionary <string, MyCacheObject> caches = GetCaches(fullName);

            config.LockObj.EnterReadLock();

            try
            {
                id = id.ToLower();
                if (!caches.ContainsKey(id))
                {
                    return(null);
                }

                MyCacheObject myCacheObject = caches[id];

                if (!config.IsFix && myCacheObject.OverdueTime < DateTime.Now)
                {
                    return(null);
                }

                myCacheObject.LastGetTime = DateTime.Now;

                try
                {
                    config.Getted(myCacheObject.Obj);
                }
                catch (Exception ex)
                {
                    LogProxy.Error(ex, false);
                }

                return(myCacheObject.Obj);
            }
            catch (Exception ex)
            {
                LogProxy.Error(ex, false);
            }
            finally
            {
                config.LockObj.ExitReadLock();
            }

            return(null);
        }
コード例 #3
0
        public void AddEvent <T>(OnCacheCleared cacheCleared, OnCacheSetted cacheSetted, OnCacheGetted cacheGetted) where T : ICacheable
        {
            if (!IsNeedCached <T>())
            {
                return;
            }

            SingleTypeCacheConfig config = SingleTypeCacheConfig.Get <T>();

            if (cacheCleared != null)
            {
                config.CacheCleared += cacheCleared;
            }
            if (cacheSetted != null)
            {
                config.CacheSetted += cacheSetted;
            }
            if (cacheGetted != null)
            {
                config.CacheGetted += cacheGetted;
            }
        }
コード例 #4
0
        public void Set(ICacheable obj)
        {
            if (obj == null)
            {
                return;
            }

            string fullName = obj.GetType().FullName;

            if (!IsNeedCached(fullName))
            {
                return;
            }

            SingleTypeCacheConfig config = SingleTypeCacheConfig.Get(fullName);
            Dictionary <string, MyCacheObject> caches = GetCaches(fullName);

            if (!config.LockObj.TryEnterWriteLock(WriteLockTimeout))
            {
                return;
            }

            try
            {
                string        id = obj.Id.ToLower();
                MyCacheObject myCacheObject;
                if (caches.ContainsKey(id))
                {
                    myCacheObject = caches[id];
                    if (myCacheObject.Obj.Version >= obj.Version)
                    {
                        return;
                    }

                    myCacheObject.Obj         = obj;
                    myCacheObject.OverdueTime = DateTime.Now.AddSeconds(config.Second);
                }
                else
                {
                    myCacheObject = new MyCacheObject(obj, DateTime.Now.AddSeconds(config.Second));
                    caches.Add(id, myCacheObject);
                }

                try
                {
                    config.Setted(obj);
                }
                catch (Exception ex)
                {
                    LogProxy.Error(ex, false);
                }
            }
            catch (Exception ex)
            {
                LogProxy.Error(ex, false);
            }
            finally
            {
                config.LockObj.ExitWriteLock();
            }
        }