WEB开发网
开发学院软件开发C语言 用匿名函数来更方便的使用缓存 阅读

用匿名函数来更方便的使用缓存

 2009-03-19 08:21:05 来源:WEB开发网   
核心提示: 实现代码如下:缓存代码usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Linq;namespaceEvlon.Utils{publicinterfaceICache<TData>{

实现代码如下:

缓存代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace Evlon.Utils
{
    public interface ICache<TData>
    {
        TData Get(object key);
        void Set(object key, TData data);
    }

    public interface ICacheManager<TData> : ICache<TData>
    {
        Func<object, TData> SourceGetter { get; }
    }


    public class SimpleCache<TData> : ICache<TData>
    {
        private Dictionary<object, TData> cache = new Dictionary<object, TData>();



        #region ICacheManager<TData> 成员

        public TData Get(object key)
        {
            return cache[key];
        }

        public void Set(object key, TData data)
        {
            cache[key] = data;
        }

        #endregion
    }


    public class TimeoutCache<TData> : ICache<TData>
    {
        public TimeSpan Expire {get;set;}
        private DateTime timeLastScan = DateTime.Now;
        private Dictionary<object, KeyValuePair<TData, DateTime>> cache = new Dictionary<object, KeyValuePair<TData, DateTime>>();

        public TimeoutCache()
        {
            Expire = TimeSpan.FromMinutes(5);
        }

        private void TryClearExpireObject()
        { 
            TimeSpan ts = DateTime.Now - timeLastScan;
            if (ts.TotalSeconds > Expire.TotalSeconds / 2)
            {
                timeLastScan = DateTime.Now;
                cache.Where(kv => kv.Value.Value  > DateTime.Now.Subtract(Expire)).ToList().ForEach(kv =>
                    {
                        cache.Remove(kv.Key);
                    });
            }
        }

        #region ICacheManager<TData> 成员

        public TData Get(object key)
        {
            TryClearExpireObject();

            if(!cache.ContainsKey(key))
                return default(TData);


            KeyValuePair<TData, DateTime> kv = cache[key];
            if (kv.Value.Add(Expire) < DateTime.Now)
            {
                cache.Remove(key);
                return default(TData);
            }
            return kv.Key;
        }

        public void Set(object key, TData data)
        {
            cache[key] = new KeyValuePair<TData, DateTime>(data, DateTime.Now);
        }

        #endregion
    }

    public class CacheManager<TData> : ICacheManager<TData>
    {
        private ICache<TData> cache = null;

        public Func<object, TData> SourceGetter { get; private set; }
        public CacheManager(ICache<TData> cache, Func<object, TData> sourceGetter)
        {
            this.cache = cache;
            this.SourceGetter = sourceGetter;
        }

        #region ICacheGetter<TData> 成员

        public TData Get(object key)
        {
            TData data = cache.Get(key);
            if (data != null)
                return data;

            data = this.SourceGetter(key);

            cache.Set(key, data);

            return data;
        }



        public void Set(object key, TData data)
        {
            cache.Set(key, data);
        }

        #endregion
    }



}

用匿名函数来更方便的使用缓存

技术交流,欢迎转载。转载请注明出处 http://evlon.cnblogs.com QQ:evlion@qq.com

上一页  1 2 

Tags:匿名 函数 方便

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接