winform中Cache的使用,支持生命周期

winform中Cache的使用,支持缓存生命周期的设置, 实现原理其实是使用了静态集合对象来存储.

废话少说, 直接上代码

public class Cache
    {
        private static Hashtable list_data;
        private static Hashtable key_data;
        public static void Register()
        {
            list_data = Hashtable.Synchronized(new Hashtable());
            key_data = Hashtable.Synchronized(new Hashtable());
        }
        public static void Set(string key, int value, int life)
        {
            object okey = (object)key;
            object ovalue = (object)value;
            Cache._Set(okey, ovalue, life);
        }
        public static int GetInt(string key)
        {
            object okey = (object)key;
            object obj = Cache._Get(okey);
            if (obj == null)
            {
                return 0;
            }
            int val = 0;
            int.TryParse(obj.ToString(), out val);
            return val;
        }
        public static void Set(string key, string value, int life)
        {            
            object okey = (object)key;
            object ovalue = (object)value;            
            Cache._Set(okey, ovalue, life);
        }
        public static string GetString(string key)
        {
            object okey = (object)key;
            object obj = Cache._Get(okey);
            return obj == null ? string.Empty : obj.ToString();
        }
        public static bool Exists(string key)
        {
            object okey = (object)key;
            object obj = Cache._Get(okey);
            return obj != null;
        }
        private static void _Set(object key , object value , int life)
        {
            life += Cache.L();
            object olife = (object)life;
            if (list_data.ContainsKey(key))
            {
                list_data[key] = value;
            }
            else
            {
                list_data.Add(key, value);
            }
            if (key_data.ContainsKey(key))
            {
                key_data[key] = olife;
            }
            else
            {
                key_data.Add(key, olife);
            }
        }
        private static object _Get(object key)
        {
            object obj = null;
            if (key_data.ContainsKey(key))
            {
                object olife = key_data[key];
                if (olife == null)
                {
                    Cache.Remove(key);
                    return obj;
                }
                int life = 0;
                int.TryParse(olife.ToString(), out life);
                if (life < L())
                {
                    Cache.Remove(key);
                    return obj;
                }
            }
            else
            {
                return obj;
            }
            if (list_data.ContainsKey(key))
            {
                obj = list_data[key];
            }
            return obj;
        }
        public static void Remove(object key)
        {
            list_data.Remove(key);
            key_data.Remove(key);
        }
        public static void Remove(string key)
        {
            Cache.Remove((object)key);
        }
        private static int L()
        {
            return Convert.ToInt32((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000);        
        }

    }
标签: cache
本文链接:http://likelys.com/article/10547 posted @ 2015-11-19 17:36:17
top