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 }