zoukankan      html  css  js  c++  java
  • .net Cache缓存的使用

    前段时间项目中要用到缓存已解决效率的问题,研究了一下.net中的Cache缓存,结果是研究了半天最后还是没有用这种方式,哎,很多问题从业务上就能优化,不说了

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.Caching;
    
    namespace Erp.Ship.Tool
    {
        public class CacheHelper
        {
            /// <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());
                }
            }
        }
    }
    View Code

    其实一提到缓存我就想到了redis缓存,所以特意了解了一下,这两者的区别,redis属于是分布式缓存,Cache只能是单体的,平常的业务管理系统,Cache够用了,redis肯定是功能更加强大的,我了解的也不多

    用法很简单

    1.CacheHelper.SetCache(cacheKey, cacheValue, timeout);//添加缓存

    2.if(CacheHelper.GetCache(cacheKey) ==null){//判断缓存是否存在

    CacheHelper.SetCache(cacheKey, cacheValue, timeout);//不存在重新添加

    }else{

    CacheHelper.GetCache(cacheKey);//获取缓存内容

    }

  • 相关阅读:
    一个业务场景的优化讨论
    关于Box Anemometer的安装配置遇到的几个坑
    CentOS6.5内 MySQL5.7.19编译安装
    CentOS6.5内 Oracle 11GR2静默安装
    始祖公——陈憺 河浦人文 (转载)
    Andriod- 从网络下载文件保存到SDCARD里
    Android Studio 如何添加第三方插件
    Android- SharedPreferences的封装
    Java/Andriod- 常用的 Android Studio 快捷键
    Java/Andriod- 动态权限申请
  • 原文地址:https://www.cnblogs.com/LDJW/p/15687197.html
Copyright © 2011-2022 走看看