• cache缓存


    Cache有以下几条缓存数据的规则。
    第一,数据可能会被频繁的被使用,这种数据可以缓存。
    第二,数据的访问频率非常高,或者一个数据的访问频率不高,但是它的生存周期很长,这样的数据最好也缓存起来。
    第三是一个常常被忽略的问题,有时候我们缓存了太多数据,通常在一台X86的机子上,如果你要缓存的数据超过800M的话,就会出现内存溢出的错误。所以说缓存是有限的。换名话说,你应该估计缓存集的大小,把缓存集的大小限制在10以内,否则它可能会出问题

    1、cache文件

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Caching;
    
    namespace Standard.Helper
    {
        public class Cache
        {
            /// <summary>  
            /// 获取数据缓存  
            /// </summary>  
            /// <param name="cacheKey"></param>  
            public static object GetCache(string cacheKey)
            {
                var objCache = HttpRuntime.Cache.Get(cacheKey);
                return objCache;
            }
    
            /// <summary>  
            /// 设置数据缓存  
            /// </summary>  
            public static void SetCache(string cacheKey, object objObject)
            {
                var objCache = HttpRuntime.Cache;
                objCache.Insert(cacheKey, objObject);
            }
    
            /// <summary>  
            /// 设置数据缓存  
            /// </summary>  
            public static void SetCache(string cacheKey, object objObject, int timeout = 7200)
            {
                try
                {
                    if (objObject == null) return;
                    var objCache = HttpRuntime.Cache;
                    //相对过期  
                    //objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);  
                    //绝对过期时间  
                    objCache.Insert(cacheKey, objObject, null, DateTime.Now.AddSeconds(timeout), TimeSpan.Zero, CacheItemPriority.High, null);
                }
                catch (Exception)
                {
                    //throw;  
                }
            }
    
            /// <summary>  
            /// 移除指定数据缓存  
            /// </summary>  
            public static void RemoveAllCache(string cacheKey)
            {
                var cache = HttpRuntime.Cache;
                cache.Remove(cacheKey);
            }
    
            /// <summary>  
            /// 移除全部缓存  
            /// </summary>  
            public static void RemoveAllCache()
            {
                var cache = HttpRuntime.Cache;
                var cacheEnum = cache.GetEnumerator();
                while (cacheEnum.MoveNext())
                {
                    cache.Remove(cacheEnum.Key.ToString());
                }
            }
        }
    }

    2、cache的使用

      //先查询是否存在相应名的缓存数据,不存在则增加

    var getInfo = Cache.GetCache("_INFO");
    if (getInfo == null)
    {

    DataTable _Info = SqlHlper.ExecuteDt(strSql);

    //定义缓存的时间为20秒
    Cache.SetCache("_INFO",j_Info, 20);

    }

    //将查询的所有的数据存放在cache中

    DataTable dt_INFO = (DataTable)Cache.GetCache("_INFO");

    //查询cache中所需的字段
    DataTable dt_FF = dt_INFO.DefaultView.ToTable(true, new string[] { "code", "name" });

    string J_AcInfo = "";
    for (int i = 0; i < dt_FF.Rows.Count; i++)
    {

    var dr_AC = dt_INFO.Select("rcode='" + (string)dt_FF.Rows[i]["code"] + "'", "cdno");
    foreach (DataRow row in dr_AC)
    {}

    }

  • 相关阅读:
    vim 字符串替换整理
    Linux Server release 7.3 更换阿里网络yum源
    VMware Vsphere 6.0安装部署 Vsphere ESXi安装
    VMware Vsphere 6.0安装部署 总体部署架构
    vsphere client和vsphere web client的区别
    VMware Vsphere 6.0安装部署 vCenter Server安装
    Python的正则表达概述
    centos的dns配置总结
    linux系统下的/proc目录介绍
    linux基础命令学习总结
  • 原文地址:https://www.cnblogs.com/luckys/p/11810019.html
Copyright © 2020-2023  润新知