zoukankan      html  css  js  c++  java
  • 字典缓存和泛型缓存

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace 缓存
     7 {
     8     class Program
     9     {
    10         static void Main(string[] args)
    11         {
    12             //字典缓存
    13             {
    14                 for (int i = 0; i < 5; i++)
    15                 {
    16                     Console.WriteLine(DictionaryCache.GetCache<int>());
    17                     Console.WriteLine(DictionaryCache.GetCache<string>());
    18                     Console.WriteLine(DictionaryCache.GetCache<Peoele>());
    19                     Console.WriteLine(DictionaryCache.GetCache<Student>());
    20                     
    21                 }
    22             }
    23             //泛型缓存
    24             {
    25                 for (int i = 0; i < 5; i++)
    26                 {
    27                     Console.WriteLine(GenericCache<int>.GetCache());
    28                     Console.WriteLine(GenericCache<string>.GetCache());
    29                     Console.WriteLine(GenericCache<Peoele>.GetCache());
    30                     Console.WriteLine(GenericCache<Student>.GetCache());
    31                 }
    32             }
    33            
    34             Console.ReadLine();
    35         }
    36     }
    37 
    38     /// <summary>
    39     /// 字典缓存,静态属性常驻内存
    40     /// </summary>
    41     public class DictionaryCache
    42     {
    43         private static Dictionary<string, object> _directory = null;
    44         static DictionaryCache()
    45         {
    46             Console.WriteLine("这是一个静态构造函数");
    47             _directory = new Dictionary<string, object>();
    48         }
    49         public static object GetCache<T>()
    50         {
    51             Type type = typeof(T);
    52             //如果静态字典中不包含该类型时,即可加入
    53             if (!_directory.ContainsKey(type.Name))
    54             {
    55                 _directory[type.Name] = string.Format("{0}_{1}",typeof(T).FullName,DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss"));
    56             }
    57             return _directory[type.Name]; 
    58         }
    59     }
    60 
    61 
    62     /// <summary>
    63     /// 泛型缓存
    64     /// 每个不同的T类型,都会生成不同的一份副本
    65     /// 适合不同的类型,需要缓存一份数据的场景,效率高
    66     /// 缺点:不能清除缓存
    67     /// </summary>
    68     /// <typeparam name="T"></typeparam>
    69     public class GenericCache<T>
    70     {
    71         private static string _Typetime ="";
    72         static GenericCache()
    73         {
    74             Console.WriteLine("这是一个静态构造函数");
    75             _Typetime = string.Format("{0}_{1}",typeof(T),DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss"));
    76         }
    77         public static string GetCache()
    78         {
    79             return _Typetime;
    80         }
    81     }
    82 
    83     public class Peoele
    84     {
    85         public int id { get; set; }
    86         public string name { get; set; }
    87     }
    88     public class Student
    89     {
    90         public int id { get; set; }
    91         public string name { get; set; }
    92     }
    93 }
    世界上最可怕事情是比我们优秀的人比我们还努力
  • 相关阅读:
    9月9号作业
    9月9号笔记
    jupyter的补充
    jupyter的使用
    9月6号作业
    编程语言的分类
    计算机组成
    计算机组成的补充
    面向对象基础
    9月2号作业
  • 原文地址:https://www.cnblogs.com/AlexOneBlogs/p/7374982.html
Copyright © 2011-2022 走看看