cache 訪問頻率的思考

BigRain發表於2016-07-15

網際網路的專案使用者基數很大,有時候瞬間併發量非常大,這個時候對於資料訪問來說是個災難。為了應對這種場景,一般都會大量採用web伺服器叢集,快取叢集。採用叢集后基本上就能解決大量併發的資料訪問。當然這個時候內網的網速會成為快取速度的瓶頸。

當然我們希望能有更好的快取結構,比如一級快取和二級快取。一級快取直接快取在宿主主機記憶體上,二級快取快取在redis叢集上,如果一個快取例項被訪問的頻率非常高,我們希望這個快取例項能快取在宿主主機記憶體上,如果一個例項的訪問頻率非常低,我們甚至可能不會為此例項進行快取處理。

基於這種設想,我們希望能夠跟蹤監視快取例項,並根據監視結果,對例項的快取級別進行動態調整,以達到最佳的快取效果。(事實上dotNet4.0裡面的System.Runtime.Caching.MemoryCache對此已經有很好的實現和支援了。當然我們的應用必須知道要快取在宿主主機記憶體上,還是redis叢集上,那就必須實現類似System.Runtime.Caching.MemoryCache的監視功能和動態調整功能)

首先我們需要附加一些監視資訊到快取例項上,

 public class CacheAttach
    {
        public CacheAttach(string key)
        {
            this.Key = key;
            this.InsertedTime = DateTime.Now;
        }
        public string Key { get; set; }
        public DateTime InsertedTime { get; private set; }
        public int QueryTimes { get; set; }
        public int AccessTimes { get; set; }
        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            return obj.GetHashCode() == this.GetHashCode();
        }
        public override int GetHashCode()
        {
            return Key.GetHashCode();
        }
        public static implicit operator CacheAttach(string value)
        {
            return new CacheAttach(value);
        }
    }
    public class CacheAttachCollection : List<CacheAttach>, ICollection<CacheAttach>
    {
        public bool Contains(string Key)
        {
            return this.Find(i => i.Key == Key) == null;
        }
        public CacheAttach this[string key]
        {
            get
            {
                CacheAttach item =this.Find(i => i.Key == key);
                if (item == null)
                {
                    item = new CacheAttach(key);
                    this.Add(item);
                }
                return item;
            }
            set
            {
                CacheAttach item = this.Find(i => i.Key == key);
                if (item == null)
                {
                    item = new CacheAttach(key);
                    this.Add(item);
                }
                item = value;
            }
        }
    }

  這裡採用的是一種附加形式的監視,不去破壞原來的K/V快取方式。這個時候我們可能需要重新包裝一下原有的快取訪問,使得對快取的操作能被監視。

public class MonitorCache: ICache
    {
        private ICache proxyCache;
        CacheAttachCollection cacheMonitor = new CacheAttachCollection();
        public MonitorCache(ICache cache)
        {
            this.proxyCache = cache;
        }
        #region ICache Implement
        public bool Set<T>(string key, T value)
        {
            cacheMonitor[key].QueryTimes++;
            cacheMonitor[key].AccessTimes++;
            return proxyCache.Set(key, value);
        }

        public bool Set<T>(string key, T value, DateTime absoluteTime, TimeSpan slidingTime, Action<string, T> removingHandler)
        {
            cacheMonitor[key].QueryTimes++;
            cacheMonitor[key].AccessTimes++;
            return this.proxyCache.Set(key, value, absoluteTime, slidingTime, removingHandler);
        }

        public object Get(string key)
        {
            cacheMonitor[key].QueryTimes++;
            cacheMonitor[key].AccessTimes++;
            return this.proxyCache.Get(key);
        }

        public T Get<T>(string key)
        {
            cacheMonitor[key].QueryTimes++;
            cacheMonitor[key].AccessTimes++;
            return this.proxyCache.Get<T>(key);
        }

        public bool Contains(string key)
        {
            cacheMonitor[key].QueryTimes++;
            return this.proxyCache.Contains(key);
        }

        public bool Remove(string key)
        {
            if (this.proxyCache.Remove(key))
            {
                cacheMonitor.Remove(key);
                return true;
            }
            return false;
        }
        #endregion

        public object this[string key]
        {
            get
            {
                return this.Get(key);
            }
            set
            {
                this.Set(key, value);
            }
        }

        public CacheAttachCollection Monitor
        {
            get
            {
                return this.cacheMonitor;
            }
        }

    }

  通過對原有的快取訪問進行包裝,我們已經實現對原有快取的重構,實現監視的意圖。

 public class CacheHelper : ICache
    {
        private MonitorCache level1 = null;
        private MonitorCache level2 = null;

        private CacheHelper()
        {
            this.level1 = new MonitorCache(new MemoryCache());
            this.level2 = new MonitorCache(new RedisCache());
        }

        public bool Set<T>(string key, T value)
        {
            if (this.level1.Set(key, value))
                return true;
            if (this.level2.Set(key, value))
                return true;
            return false;
        }

        public bool Set<T>(string key, T value, DateTime absoluteTime, TimeSpan slidingTime, Action<string, T> removingHandler)
        {
            if (this.level1.Set(key, value, absoluteTime, slidingTime, removingHandler))
                return true;
            if (this.level2.Set(key, value, absoluteTime, slidingTime, removingHandler))
                return true;
            return false;
        }

        public object Get(string key)
        {
            return this.level1.Get(key) ?? this.level2.Get(key) ?? null;
        }

        public T Get<T>(string key)
        {
            if (this.level1.Contains(key))
                return this.level1.Get<T>(key);
            if (this.level2.Contains(key))
                return this.level2.Get<T>(key);
            return default(T);
        }

        public T Get<T>(string key, Func<T> valueGetter)
        {
            var result = default(T);
            if (this.level1.Contains(key))
                result = this.level1.Get<T>(key);
            else if (this.level2.Contains(key))
                result = this.level2.Get<T>(key);

            if (result == null && valueGetter != null)
                result = valueGetter();
            return result;
        }

        public bool Contains(string key)
        {
            if (this.level1.Contains(key))
                return true;
            if (this.level2.Contains(key))
                return true;
            return false;
        }

        public bool Remove(string key)
        {
            if (this.level1.Contains(key))
                this.level1.Remove(key);
            if (this.level2.Contains(key))
                this.level2.Remove(key);
            return true;
        }

        public object this[string key]
        {
            get
            {
                return this.Get(key);
            }
            set
            {
                this.Set(key, value);
            }
        }
        public void Trim()
        {
            //對一級快取進行整理
            for (int i = 0, lengh = this.level1.KeyMonitor.Count; i < lengh; i++)
            {
                CacheAttach item = this.level1.KeyMonitor[i];

                //頻率小於10次/秒的快取需要移除一級快取
                if (item.AccessRate < 10)
                {
                    //頻率大於1次/秒的快取移到二級快取
                    if (item.AccessRate >= 1)
                    {
                        this.level2.Set(item.Key, this.level1[item.Key]);
                        this.level2.KeyMonitor[item.Key] = item;
                    }
                    this.level1.Remove(item.Key);
                }
            }

            //對二級快取進行整理
            for (int i = 0, lengh = this.level2.KeyMonitor.Count; i < lengh; i++)
            {
                CacheAttach item = this.level1.KeyMonitor[i];

                //頻率大於等於10次/秒的快取需要移至一級快取
                if (item.AccessRate >= 10)
                {
                    this.level1.Set(item.Key, this.level2[item.Key]);
                    this.level1.KeyMonitor[item.Key] = item;
                    this.level1.Remove(item.Key);
                    continue;
                }
                if (item.AccessRate < 1)
                {
                    this.level2.Remove(item.Key);
                    continue;
                }
            }
        }

        private static CacheHelper _Current = new CacheHelper();
        public static CacheHelper Current
        {
            get { return _Current; }
        }
        public static CacheHelper()
        {
            System.Threading.Timer timer = new System.Threading.Timer(delegate
            {
                Current.Trim();
            });
        }
    }

  

相關文章