zoukankan      html  css  js  c++  java
  • 配置表分析和缓存的应用

    一、配置表分析和缓存的应用

    配置表就是对前端中可能经常修改的数据配置在数据库中。其实就是类于配置文件一样,但是此处是将该数据放置在数据库中进行管理。

    只需要在数据库中添加一张表,并且对表添加三列字段,分别为,id,key,value列。

    二、 缓存的应用,对于需要经常使用的值,需要放置在缓存中,可以提高数据提取的速度。

    1. 封装的缓存的方法。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    
    namespace Common
    {
       public class CacheHelper
        {
           /// <summary>
           /// 从缓存中获取数据
           /// </summary>
           /// <param name="key"></param>
           /// <returns></returns>
           public static object Get(string key)
           {
               System.Web.Caching.Cache cache = HttpRuntime.Cache;
               return cache[key];
    
           }
           /// <summary>
           /// 向缓存中添加数据
           /// </summary>
           /// <param name="key"></param>
           /// <param name="value"></param>
           public static void Set(string key, object value)
           {
               System.Web.Caching.Cache cache = HttpRuntime.Cache;
               cache[key] = value;
           }
           //重载,设置过期事件
           public static void Set(string key, object value, DateTime time)
           {
               System.Web.Caching.Cache cache = HttpRuntime.Cache;
               //其中第三个值,是缓存依赖,此处不需要,所以填null,第四个是设置过期时间,第五个表示滑动过期时间
               cache.Insert(key, value, null, time, TimeSpan.Zero );
           }
           /// <summary>
           /// 删除缓存,如果需要更新表中某一项的值,切记要把缓存中的值删除掉
           /// </summary>
           /// <param name="key"></param>
           public static void Rmeove(string key)
           {
               System.Web.Caching.Cache cache = HttpRuntime.Cache;
               cache.Remove(key);
           }
        }
    }

     获取缓存中的数据

    /// <summary>
          /// 根据配置项的名称,获取具体的配置的值
          /// </summary>
          /// <param name="key">配置项的名称</param>
          /// <returns></returns>
            public string GetValue(string key)
            {
                //判断缓存中是否有数据
                if (Common.CacheHelper.Get("setting_" + key) == null)
                {
                    string value = dal.GetModel(key).Value;   //GetModel是封装的查询该该数据,并且存放在缓存中
                    Common.CacheHelper.Set("setting_" + key, value);
                    return value;
                }
                else
                {
                    return Common.CacheHelper.Get("setting_" + key).ToString();
                }
            }

    GetModel方法:

     /// <summary>
            ///根据配置的名称 得到一个对象实体
            /// </summary>
            public BookShop.Model.Settings GetModel(string name)
            {
    
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select  top 1 Id,Name,Value from Settings ");
                strSql.Append(" where Name=@Name ");
                SqlParameter[] parameters = {
                        new SqlParameter("@Name", SqlDbType.NVarChar,50)};
                parameters[0].Value = name;
    
                BookShop.Model.Settings model = new BookShop.Model.Settings();
                DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                    {
                        model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                    }
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                    model.Value = ds.Tables[0].Rows[0]["Value"].ToString();
                    return model;
                }
                else
                {
                    return null;
                }
            }
            

    调用

    bll.GetValue("Key的值")
  • 相关阅读:
    vue的特点 关键字
    小程序技术实现
    SpringCloud简历模板
    SpringBoot简历模板
    SpringCloud+Eureka快速搭建微服架构
    Docker 面试题
    说说mysql的存储引擎,有什么区别?索引的介绍
    mysql语句
    fail-fast 与 fail-save 机制的区别
    动态规划总结
  • 原文地址:https://www.cnblogs.com/wangjinya/p/10940835.html
Copyright © 2011-2022 走看看