开发手册 欢迎您!
软件开发者资料库

C#(.NET Core)使用泛型<T>实现类型数据缓存方法及示例代码

学习泛型发现使用泛型实现类型数据缓存可以方便简单不少,本文主要C#(.NET Core)中使用泛型实现数据缓存的方法及示例代码。

1、基本数据类型的缓存

int,long,DateTime,string等基本数据类型缓存。

    ///     /// 每个不同的T,都会生成一份不同的副本    /// 适合不同类型,需要缓存一份数据的场景,效率高    /// 不能主动释放    ///     ///     public class GenericCache    {        static GenericCache()        {            Console.WriteLine("This is GenericCache 静态构造函数");            _TypeTime = string.Format("{0}_{1}", typeof(T).FullName, DateTime.Now.ToString("yyyyMMddHHmmss.fff"));        }        private static string _TypeTime = "";        public static string GetCache()        {            return _TypeTime;        }    }

2、class类型数据缓存

如从数据库读取的实体数据,也可以参考此方法实现。

    ///     /// 字典缓存:静态属性常驻内存    ///     public class DictionaryCache    {        private static Dictionary _TypeTimeDictionary = null;        static DictionaryCache()        {            Console.WriteLine("This is DictionaryCache 静态构造函数");            _TypeTimeDictionary = new Dictionary();        }        public static string GetCache()        {            Type type = typeof(T);            if (!_TypeTimeDictionary.ContainsKey(type))            {                _TypeTimeDictionary[type] = string.Format("{0}_{1}", typeof(T).FullName, DateTime.Now.ToString("yyyyMMddHHmmss.fff"));            }            return _TypeTimeDictionary[type];        }    }

3、测试项目代码

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace Cjavapy.Extend{    public class CjavapyCacheTest    {        public static void Show()        {            for (int i = 0; i < 5; i++)            {                Console.WriteLine(GenericCache.GetCache());                Thread.Sleep(10);                Console.WriteLine(GenericCache.GetCache());                Thread.Sleep(10);                Console.WriteLine(GenericCache.GetCache());                Thread.Sleep(10);                Console.WriteLine(GenericCache.GetCache());                Thread.Sleep(10);                Console.WriteLine(GenericCache.GetCache());                Thread.Sleep(10);            }        }    }    ///     /// 字典缓存:静态属性常驻内存    ///     public class DictionaryCache    {        private static Dictionary _TypeTimeDictionary = null;        static DictionaryCache()        {            Console.WriteLine("This is DictionaryCache 静态构造函数");            _TypeTimeDictionary = new Dictionary();        }        public static string GetCache()        {            Type type = typeof(Type);            if (!_TypeTimeDictionary.ContainsKey(type))            {                _TypeTimeDictionary[type] = string.Format("{0}_{1}", typeof(T).FullName, DateTime.Now.ToString("yyyyMMddHHmmss.fff"));            }            return _TypeTimeDictionary[type];        }    }    ///     /// 每个不同的T,都会生成一份不同的副本    /// 适合不同类型,需要缓存一份数据的场景,效率高    /// 不能主动释放    ///     ///     public class GenericCache    {        static GenericCache()        {            Console.WriteLine("This is GenericCache 静态构造函数");            _TypeTime = string.Format("{0}_{1}", typeof(T).FullName, DateTime.Now.ToString("yyyyMMddHHmmss.fff"));        }        private static string _TypeTime = "";        public static string GetCache()        {            return _TypeTime;        }    }}