-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheHelper.cs
100 lines (91 loc) · 2.99 KB
/
CacheHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Linq;
using System.Web;
namespace SP.GlobalTopMenu
{
public static class CacheHelper
{
#region Methods
/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="o">Item to be cached</param>
/// <param name="key">Name of item</param>
public static void Add<T>(T o, string key) where T : class
{
try
{
key = System.Security.Principal.WindowsIdentity.GetCurrent().User.AccountDomainSid.ToString() + key;
// NOTE: Apply expiration parameters as you see fit.
// In this example, I want an absolute
// timeout so changes will always be reflected
// at that time. Hence, the NoSlidingExpiration.
HttpContext.Current.Cache.Insert(
key,
o,
null,
DateTime.Now.AddMinutes(1440),
System.Web.Caching.Cache.NoSlidingExpiration);
}
catch (Exception ex)
{
throw;
}
}
/// <summary>
/// Remove item from cache
/// </summary>
/// <param name="key">Name of cached item</param>
public static void Clear(string key)
{
try
{
key = System.Security.Principal.WindowsIdentity.GetCurrent().User.AccountDomainSid.ToString() + key;
HttpContext.Current.Cache.Remove(key);
}
catch (Exception ex)
{
throw;
}
}
/// <summary>
/// Check for item in cache
/// </summary>
/// <param name="key">Name of cached item</param>
/// <returns></returns>
public static bool Exists(string key)
{
try
{
key = System.Security.Principal.WindowsIdentity.GetCurrent().User.AccountDomainSid.ToString() + key;
return HttpContext.Current.Cache[key] != null;
}
catch (Exception ex)
{
throw;
//return false;
}
}
/// <summary>
/// Retrieve cached item
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="key">Name of cached item</param>
/// <returns>Cached item as type</returns>
public static T Get<T>(string key) where T : class
{
try
{
key = System.Security.Principal.WindowsIdentity.GetCurrent().User.AccountDomainSid.ToString() + key;
return (T)HttpContext.Current.Cache[key];
}
catch
{
return null;
}
}
#endregion
}
}