ASP.NET学习笔记----缓存数据库中的1张表(20080306)

目标:缓存UserGrade表(字段:编号、等级名称、和头像图表路径)。

实现过程 :

1、创建过程;

namespace XyMembers_com
{
       //级别信息
    public class UserGradeInfo
    {
        public int ZID;
        public string ZNAME;
        public string ZIMG;

        public UserGradeInfo(int ZID, string ZNAME, string ZIMG)
        {
            this.ZID = ZID;
            this.ZNAME = ZNAME;
            this.ZIMG = ZIMG;
        }
    }

    //缓存级别表
    public static class UserGrade
    {

        public static Dictionary<int, UserGradeInfo> GetUserGradeList()
        {

            lock (typeof(UserGrade))
            {
                //如果已经有了
                if (HttpContext.Current.Cache["UserGrade"] != null)
                    return (Dictionary<int, UserGradeInfo>)HttpRuntime.Cache["UserGrade"];
                else
                {
                    Dictionary<int, UserGradeInfo> rst = new Dictionary<int, UserGradeInfo>();
                    SqlData sd = new SqlData();
                    DataTable dt = sd.ExecDS("select * from UserGrade");
                    foreach(DataRow row in dt.Rows)
                    {
                        int index = (int)row[0];
                        UserGradeInfo detail = new UserGradeInfo(index, (string)row[1], (string)row[2]);
                        rst.Add(index, detail);
                    }
                    HttpContext.Current.Cache.Insert("UserGrade", rst, null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);
                    return (Dictionary<int, UserGradeInfo>)HttpRuntime.Cache["UserGrade"];
                }

            }

        }

    }

}

 

2、使用过程:

      。。。

 Dictionary<int, UserGradeInfo> dc = UserGrade.GetUserGradeList();
 UserGradeInfo tmp = (UserGradeInfo)dc[5];

 return tmp.ZNAME.ToString();

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值