zoukankan      html  css  js  c++  java
  • MVCCache2.应用程序缓存(Cache:1.输出缓存[2].应用程序缓存)

    2.应用数据缓存-Cache

    1.引入CacheHelper.cs
    CacheHelper.cs文件源码在下面;
    2.介绍用法:

    //键
    string ips = "键";
    //值;得到当前时间
    long Now_time = DateTime.Now.ToFileTime();
    //存;设置过期缓存时间2s;键值对形式存入;Insert方法存入
    CacheHelper.SetCache(ips, Now_time,TimeSpan.FromSeconds(2));    
    //取;通过键取值,取出来的是object类型,我这里进行了强转Convert.ToInt64;
    long Get_time = Convert.ToInt64(CacheHelper.GetCache(ips));
    //清除全部缓存
    CacheHelper.RemoveAllCache();
    
    





    Ps:CacheHelper.cs文件源码

    using System;
    using System.Collections;
    using System.Web;
    
    namespace JJQ_Model
    {
        /// <summary>
        /// Cache缓存帮助类
        /// </summary>
        public class CacheHelper
        {
            /// <summary>
            /// 获取数据缓存
            /// </summary>
            /// <param name="CacheKey">键</param>
            public static object GetCache(string CacheKey)
            {
                System.Web.Caching.Cache objCache = HttpRuntime.Cache;
                return objCache[CacheKey];
            }
    
            /// <summary>
            /// 设置数据缓存
            /// 向 Cache 对象插入项,该项带有一个缓存键引用其位置,并使用 CacheItemPriority 枚举提供的默认值。
            /// </summary>
            public static void SetCache(string CacheKey, object objObject)
            {
                System.Web.Caching.Cache objCache = HttpRuntime.Cache;
                objCache.Insert(CacheKey, objObject);
            }
    
            /// <summary>
            /// 设置数据缓存
            /// </summary>
            public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)
            {
                System.Web.Caching.Cache objCache = HttpRuntime.Cache;
                objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
            }
    
            /// <summary>
            /// 设置数据缓存
            /// </summary>
            public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
            {
                System.Web.Caching.Cache objCache = HttpRuntime.Cache;
                objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
            }
    
            /// <summary>
            /// 移除指定数据缓存
            /// </summary>
            public static void RemoveAllCache(string CacheKey)
            {
                System.Web.Caching.Cache _cache = HttpRuntime.Cache;
                _cache.Remove(CacheKey);
            }
    
            /// <summary>
            /// 移除全部缓存
            /// </summary>
            public static void RemoveAllCache()
            {
                System.Web.Caching.Cache _cache = HttpRuntime.Cache;
                IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
                while (CacheEnum.MoveNext())
                {
                    _cache.Remove(CacheEnum.Key.ToString());
                }
    
            }
        }
    }
    
    
    
  • 相关阅读:
    linux查看端口被占用等常用命令
    python批量修改文件内容及文件编码方式的处理
    为什么在Python里推荐使用多进程而不是多线程
    python小练习之读取文件写入excel
    python 小练习之生成手机号码
    python 小练习之删除文件夹下的所有文件,包括子文件夹中的文件
    python中 字符 字典 列表之间的转换
    统计表中 重复出现 XX次以上的数据
    java数据类型
    字符编码和python使用encode,decode转换utf-8, gbk, gb2312
  • 原文地址:https://www.cnblogs.com/jsll/p/11619306.html
Copyright © 2011-2022 走看看