zoukankan      html  css  js  c++  java
  • 自己动手写缓存Version1


    实现一个最简单最主要的缓存系统。



    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Collections;
    using System.Text;
    using System.Threading;
    using System.Collections.Generic;
    using System.Reflection;
    
    
    namespace ConsoleApplication1
    {
        public class MyCache<T> where T : new()
        {
            //缓存的数据
            private List<T> data;
    
            private void SetData(List<T> data)
            {
                this.data = data;
            }
    
            public List<T> GetData()
            {
                return this.data;
            }
    
            //缓存过期时间
            public int timeoutSecond;
    
            //缓存读取数据库的SQL
            public string sql;
    
            /// <summary>
            /// 启动缓存
            /// </summary>
            public void StartCache()
            {
                Console.WriteLine("缓存启动,開始初始化缓存数据...");
    
                DataTable dt = GetDataFromDB(this.sql);
                
                SetData(ConvertHelper<T>.ConvertToList(dt));
    
                Console.WriteLine("缓存启动,初始化数据完毕.");
    
                //定时更新缓存
                TimerCallback tcb = new TimerCallback(UpdateCache);
                Timer tmr = new Timer(tcb, null, timeoutSecond, timeoutSecond);
    
            }
    
            /// <summary>
            /// 依据配置的SQL从数据库取得数据
            /// </summary>
            /// <param name="sql"></param>
            /// <returns></returns>
            private DataTable GetDataFromDB(string sql)
            {
                //TODO:依据SQL从数据库取得数据
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
                dt.Columns.Add(new DataColumn("UserName", typeof(String)));
                DataRow dr;
    
                int n = new Random().Next(6);
                for (int i = 0; i < n; i++)
                {
                    dr = dt.NewRow();
                    dr[0] = i;
                    dr[1] = "第" + i.ToString() + "个记录的内容";
                    dt.Rows.Add(dr);
                }
                return dt;
            }
    
            /// <summary>
            /// 更新缓存
            /// </summary>
            /// <param name="data"></param>
            private void UpdateCache(object data)
            {
                DataTable dt = GetDataFromDB(this.sql);
                List<T> newList= ConvertHelper<T>.ConvertToList(dt);
                SetData(newList);
    
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("缓存更新完毕...");
                foreach (T t in newList)
                {
                    Console.WriteLine(t.ToString());
                }
            }
        }
    }
    



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Reflection;
    
    namespace ConsoleApplication1
    {
        /// <summary>
        /// DataTable转换为List的辅助类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class ConvertHelper<T> where T : new()
        {
            /// <summary>  
            /// 利用反射和泛型  
            /// </summary>  
            /// <param name="dt"></param>  
            /// <returns></returns>  
            public static List<T> ConvertToList(DataTable dt)
            {
    
                // 定义集合  
                List<T> ts = new List<T>();
    
                // 获得此模型的类型  
                Type type = typeof(T);
                //定义一个暂时变量  
                string tempName = string.Empty;
                //遍历DataTable中全部的数据行  
                foreach (DataRow dr in dt.Rows)
                {
                    T t = new T();
                    // 获得此模型的公共属性  
                    PropertyInfo[] propertys = t.GetType().GetProperties();
                    //遍历该对象的全部属性  
                    foreach (PropertyInfo pi in propertys)
                    {
                        tempName = pi.Name;//将属性名称赋值给暂时变量  
                        //检查DataTable是否包括此列(列名==对象的属性名)    
                        if (dt.Columns.Contains(tempName))
                        {
                            // 推断此属性是否有Setter  
                            if (!pi.CanWrite) continue;//该属性不可写,直接跳出  
                            //取值  
                            object value = dr[tempName];
                            //假设非空,则赋给对象的属性  
                            if (value != DBNull.Value)
                                pi.SetValue(t, value, null);
                        }
                    }
                    //对象加入到泛型集合中  
                    ts.Add(t);
                }
    
                return ts;
    
            }
        }
    }
    




    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class UserInfo
        {
            public int ID { get; set; }
            public string UserName { get; set; }
    
            public override string ToString()
            {
                return "ID:" + this.ID + ",UserName:" + this.UserName;
            }
        }
    }
    



    測试方法:


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    using System.Threading;
    
    namespace ConsoleApplication1
    {
    
        class MyCacheTest
        {
            public static void Main()
            {
                MyCache<UserInfo> mycache = new MyCache<UserInfo>();
                
                //5秒更新一次缓存
                mycache.timeoutSecond = 5000;
                mycache.sql = "";
                //启动缓存
                mycache.StartCache();
    
    
                for (int i = 0; i < 100; i++)
                {
                    //读取缓存
                    Console.WriteLine("_______________读取缓存,缓存内容例如以下:");
                    List<UserInfo> userList = mycache.GetData();
                    foreach (UserInfo userInfo in userList)
                    {
                        Console.WriteLine(userInfo.ToString());
                    }
    
                    //1秒读取一次缓存
                    Thread.Sleep(1000);
     
                }
                   
            }
    
        }
    }
    



    測试结果例如以下:


    这仅仅是一个最简单的缓存系统。下一个版本号希望可以实现例如以下功能:

    1、支持key-value的查找、加入、删除。

    2、加入缓存配置文件。

    详细的配置文件例如以下:

    <?xml version="1.0" encoding="utf-8" ?

    > <SQLCommands> <!--全部的用户缓存--> <SQLCommand ListType="UserInfo" CommandString="SELECT * FROM UserInfo" CacheOverTime="0,5,0"></SQLCommand> <!--未处理的订单缓存--> <SQLCommand ListType="UnHandleOrder" CommandString="SELECT * FROM OrderInfo WHERE Orderstatus = 0" CacheOverTime="0,5,0"></SQLCommand> </SQLCommands>











  • 相关阅读:
    云计算大会有感—MapReduce和UDF
    C++对象模型
    oracle中LAG()和LEAD()等分析统计函数的使用方法(统计月增长率)
    .NET MVC学习笔记(一)
    解决mysql控制台查询数据乱码的问题,有图有真相
    java linux 项目常常无故被关闭 进程无故消息
    android点滴之ContentObserver的使用
    GEF-whole-upload教程中遇到的问题及解决方案
    第四届蓝桥杯JavaB组省赛真题
    第四届蓝桥杯JavaA组省赛真题
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5339808.html
Copyright © 2011-2022 走看看