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. 【转载】碰碰彭碰彭Jingxuan —— 带中国古筝走上戛纳红毯

    视频地址: https://www.youtube.com/shorts/gl796527H1I

  2. jenkins集成cucumber-resport报告

    需要安装的jenkins插件-Cucumber Reports jenkins版本:2.273 jenkins插件下载地址:点击下载 下载插件后通过jenkins插件管理上传已下载好的插件即可 等待j ...

  3. delphi Image32 动画演示2

    Image 32 自带的Demo,添加一些注解. unit uFrmAnimation2; interface uses Winapi.Windows, Winapi.Messages, System ...

  4. P3523 POI2011 DYN-Dynamite

    P3523 POI2011 DYN-Dynamite 小 trick,加双倍经验. 思路 使 \(dis\) 的最大值最小,可以想到二分 \(dis\),然后根据 \(dis\) 判断可行性. 那么可 ...

  5. Vue 实现图片下拉选择控件

    element-ui 的组件库中没有图片下拉选择组件,基于 el-select 组件做的改动并不能完全满足需求,因此决定重写一个. 从头到尾做下来收获很多,我决定把实现过程中遇到的问题记录一下. 效果 ...

  6. 1、oracle实例、软件、库简单讲解

    oracle的基本结构 oracle软件(RDBMS) oracle软件:关系型数据库管理系统 在linux系统上,oracle软件安装在:/u01/app/oracle这个目录下 oracle数据库 ...

  7. 腾讯AICR : 智能化代码评审技术探索与应用实践(下)

  8. C#-JavaScript-base64加密解码

    C# //base64加密 //调用方式:Helper.EncodeToBase64(需要加密字符串) public static string EncodeToBase64(string data) ...

  9. Django之gunicorn部署

    安装: pip install gunicorn 启动应用: gunicorn -w 3 -k gthread -e DJANGO_SETTINGS_MODULE=settings.prod Serv ...

  10. python之异步任务框架Celery

    官网参考: Celery 官网:http://www.celeryproject.org/ Celery 官方文档英文版:http://docs.celeryproject.org/en/latest ...