1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Web;
7 using System.Web.Caching;
8
9 namespace ODBCConsoleApp
10 {
11 class CacheHelper
12 {
13
14 /// <summary>
15 /// 获取数据缓存
16 /// </summary>
17 /// <param name="cacheKey">键</param>
18 public static object GetCache(string cacheKey)
19 {
20 var objCache = HttpRuntime.Cache.Get(cacheKey);
21 return objCache;
22 }
23 /// <summary>
24 /// 设置数据缓存
25 /// </summary>
26 public static void SetCache(string cacheKey, object objObject)
27 {
28 var objCache = HttpRuntime.Cache;
29 objCache.Insert(cacheKey, objObject);
30 }
31 /// <summary>
32 /// 设置数据缓存
33 /// </summary>
34 public static void SetCache(string cacheKey, object objObject, int timeout = 7200)
35 {
36 try
37 {
38 if (objObject == null) return;
39 var objCache = HttpRuntime.Cache;
40 //相对过期
41 //objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue,  new TimeSpan(0, 0, timeout), CacheItemPriority.NotRemovable, null);
42 //绝对过期时间
43 objCache.Insert(cacheKey, objObject, null, DateTime.UtcNow.AddSeconds(timeout), TimeSpan.Zero, CacheItemPriority.High, null);
44 }
45 catch (Exception)
46 {
47 //throw;
48 }
49 }
50 /// <summary>
51 /// 移除指定数据缓存
52 /// </summary>
53 public static void RemoveAllCache(string cacheKey)
54 {
55 var cache = HttpRuntime.Cache;
56 cache.Remove(cacheKey);
57 }
58 /// <summary>
59 /// 移除全部缓存
60 /// </summary>
61 public static void RemoveAllCache()
62 {
63 var cache = HttpRuntime.Cache;
64 var cacheEnum = cache.GetEnumerator();
65 while (cacheEnum.MoveNext())
66 {
67 cache.Remove(cacheEnum.Key.ToString());
68 }
69 }
70
71 }
72 }
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace ODBCConsoleApp
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 // CacheHelper.RemoveAllCache();
14 var uid = Guid.NewGuid().ToString();
15 //CacheHelper.SetCache(uid, uid, 1);
16 uid = "ed19c366-8af3-4379-a140-733e290bef4e";
17 var value = CacheHelper.GetCache(uid);
18
19 Console.WriteLine(value);
20 Console.ReadLine();
21 }
22 }
23 }

C# Caching---Cache 缓存的更多相关文章

  1. System.Web.Caching.Cache缓存帮助类

    /// <summary> /// 缓存帮助类 /// </summary> public class CacheHelper { /// <summary> // ...

  2. 第一节:从程序集的角度分析System.Web.Caching.Cache ,并完成基本封装。

    一. 揭开迷雾 1. 程序集准备 a.  需要给项目添加 System.Web 程序集. b.  需要给使用的地方添加两个引用. 2. 程序集探究      在对应的类中输入关键字 Cache,选中点 ...

  3. System.Web.Caching.Cache类 缓存

    1.文件缓存依赖 public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender ...

  4. System.Web.Caching.Cache类 缓存 各种缓存依赖

    原文:System.Web.Caching.Cache类 缓存 各种缓存依赖 Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.C ...

  5. System.Web.Caching.Cache类 缓存 各种缓存依赖(转)

    转自:http://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntime ...

  6. System.Web.Caching.Cache类 Asp.Net缓存 各种缓存依赖

    Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.Cache都是该类的实例. 一.属性 属性 说明 Count 获取存储在缓存中的 ...

  7. C# System.Web.Caching.Cache类 缓存 各种缓存依赖

    原文:https://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntim ...

  8. Spring Cache缓存技术,Cacheable、CachePut、CacheEvict、Caching、CacheConfig注解的使用

    前置知识: 在Spring Cache缓存中有两大组件CacheManager和Cache.在整个缓存中可以有多个CacheManager,他们负责管理他们里边的Cache.一个CacheManage ...

  9. 清除 System.Web.Caching.Cache 以"xxx"开头的缓存

    public static void ClearStartCache(string keyStart) { List<string> cacheKeys = new List<str ...

  10. caching与缓存

    通常,应用程序可以将那些频繁访问的数据,以及那些需要大量处理时间来创建的数据存储在内存中,从而提高性能.例如,如果应用程序使用复杂的逻辑来处理大量数据,然后再将数据作为用户频繁访问的报表返回,避免在用 ...

随机推荐

  1. 模算术 modular arithmetic

    https://en.wikipedia.org/wiki/Modular_arithmetic#Integers_modulo_n 模算术: 整数达到特定值时会' 折返 ' 回来-- 模数 modu ...

  2. 《刚刚问世》系列初窥篇-Java+Playwright自动化测试-4-启动浏览器-基于Maven(详细教程)

    1.简介 上一篇文章,宏哥已经在搭建的java项目环境中添加jar包实践了如何启动浏览器,今天就在基于maven项目的环境中给小伙伴们或者童鞋们演示一下如何启动浏览器. 2.eclipse中新建mav ...

  3. 值得推荐的IT公司名单(广州篇)

    广州,作为中国南方的重要城市,不仅历史悠久,而且在科技创新方面也具有极高的活力.特别是在IT领域,广州孕育了许多知名企业和创业公司,成为众多技术人才的聚集地. 本文将介绍一些在广州值得去的IT公司,为 ...

  4. git 暂存区问题

    如果需要合别人的代码进来 需要暂时把自己的代码stash一下,用 git stash 放入暂存 如果需要释放出来用 git stash pop 当暂存用的越来越多,问题出现了需要清理暂存区队列,使用代 ...

  5. golang WEB框架Hertz --- 获取参数

    安装Hertz命令行工具 请确保您的Go版本在1.15及以上版本,笔者用的版本是1.18 配置好GO的环境后,按照Hertz的命名行工具 go install github.com/cloudwego ...

  6. 性能检测工具之Lighthouse

    转载:https://mp.weixin.qq.com/s?src=11&timestamp=1618929340&ver=3020&signature=oXyx*RDLXjN ...

  7. 深度变分信息瓶颈——Deep Variational Information Bottleneck

    Deep Variational Information Bottleneck (VIB) 变分信息瓶颈 论文阅读笔记.本文利用变分推断将信息瓶颈框架适应到深度学习模型中,可视为一种正则化方法. 变分 ...

  8. vue表格轮播插件

    1.前言 需求:制作大屏看板时,经常要展示表格数据,通常一页时放不下的,表格需要自动滚动,并维持表头固定 为何自己封装:网上的滚动组件有2类,一种传入json数据进行滚动(DataV),优点是可以做到 ...

  9. 《JavaScript 模式》读书笔记(4)— 函数3

    这篇,我们来学习下自定义函数以及即时函数的内容. 四.自定义函数 函数可以动态定义,也可以分配给变量.如果创建了一个新函数,并且将其分配给保存了另外函数的同一个变量,那么就以一个新函数覆盖了旧函数.在 ...

  10. Redis应用—4.在库存里的应用

    大纲 1.库存模块设计 2.库存缓存分片和渐进式同步方案 3.基于缓存分片的下单库存扣减方案 4.商品库存设置流程与异步落库的实现 6.库存入库时"缓存分片写入 + 渐进式写入 + 写入失败 ...