缓存原理,自己写一个缓存类(c#版)

.net中的MemoryCache是通过内部封装一个静态Dictionary

自己写一个缓存,来看看内部怎么实现的

public class CustomerCache : ICache
    {

        private static ConcurrentDictionary<string, KeyValuePairobject>> _CustomerCacheDictionary = new ConcurrentDictionary<string, KeyValuePairobject>>();

        static CustomerCache()
        {
            Task.Run(() =>
            {
                while (true)
                {
                    if (_CustomerCacheDictionary.Count > 0)
                    {
                        List<string> list = new List<string>();
                        foreach (var item in _CustomerCacheDictionary)
                        {
                            KeyValuePairobject> keyValuePair = item.Value;
                            if (DateTime.Now > keyValuePair.Key)//过期了
                            {
                                list.Add(item.Key);
                            }
                        }
                        foreach (var key in list)
                        {
                            _CustomerCacheDictionary.TryRemove(key,out var v);
                        }
                    }
                    Thread.Sleep(1000);
                }
            });
        }

        /// 
        /// 
        /// 
        /// 
        /// 
        /// 默认30分钟  单位分钟
        public void Add(string key, object data, int cacheTime = 30)
        {
            if (_CustomerCacheDictionary.ContainsKey(key))
                throw new Exception("相同的key");
            _CustomerCacheDictionary.TryAdd(key, new KeyValuePairobject>(DateTime.Now.AddMinutes(cacheTime), data));
        }

        /// 
        /// 应该先检查,再get
        /// 
        /// 
        /// 
        /// 
        public T Get(string key)
        {
            return (T)_CustomerCacheDictionary[key].Value;
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool Contains(string key)
        {
            if (_CustomerCacheDictionary.ContainsKey(key))
            {
                KeyValuePairobject> keyValuePair = _CustomerCacheDictionary[key];
                if (DateTime.Now > keyValuePair.Key)//过期了
                {
                    _CustomerCacheDictionary.TryRemove(key,out var v);
                    return false;
                }
                else
                    return true;
            }
            return false;
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 真实查询数据源的方法
        /// 
        public T GetT(string key, Func func)
        {
            T t;
            if (!this.Contains(key))
            {
                t = func.Invoke();
                this.Add(key, t);
            }
            else
            {
                t = this.Get(key);
            }
            return t;
        }


        public object this[string key] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

        public int Count {
            get {
                return _CustomerCacheDictionary.Count;
            }
        }

        public void Remove(string key)
        {
            _CustomerCacheDictionary.TryRemove(key, out var v);
        }

        public void RemoveAll()
        {
            _CustomerCacheDictionary.Clear();
        }
    }
public interface ICache
    {
        /// 
        /// Gets or sets the value associated with the specified key.
        /// 
        /// Type
        /// The key of the value to get.
        /// The value associated with the specified key.
        T Get(string key);

        /// 
        /// Adds the specified key and object to the cache.
        /// 
        /// key
        /// Data
        /// Cache time
        void Add(string key, object data, int cacheTime = 30);

        /// 
        /// Gets a value indicating whether the value associated with the specified key is cached
        /// 
        /// key
        /// Result
        bool Contains(string key);

        /// 
        /// Removes the value with the specified key from the cache
        /// 
        /// /key
        void Remove(string key);

        /// 
        /// Clear all cache data
        /// 
        void RemoveAll();

        object this[string key] { get; set; }

        int Count { get; }
    }

你可能感兴趣的:(缓存原理,自己写一个缓存类(c#版))