博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CacheManager操作缓存
阅读量:4650 次
发布时间:2019-06-09

本文共 10391 字,大约阅读时间需要 34 分钟。

ExpandedBlockStart.gif
View Code
  1 
using
 System;
  2 
using
 System.Collections.Generic;
  3 
using
 System.Text;
  4 
  5 
namespace
 Secom.Framework.Common
  6 
{
  7 
    
///
 
<summary>
  8 
    
///
 缓存服务的基本接口,定义缓存服务的基本功能
  9 
    
///
 
</summary>
 10 
    
 11 
    
public
 
interface
 ICacheManager:IHttpRunTimeCacheManager
 12 
    {
 13 
        
///
 
<summary>
 14 
        
///
 
 15 
        
///
 
</summary>
 16 
        
///
 
<param name="cacheManagerName"></param>
 17 
        
///
 
<param name="key"></param>
 18 
        
///
 
<param name="obj"></param>
 19 
        
///
 
<param name="duration"></param>
 20 
        
void
 Add(
string
 cacheManagerName, 
string
 key, Object obj, 
int
 duration);
 21 
        
///
 
<summary>
 22 
        
///
 
 23 
        
///
 
</summary>
 24 
        
///
 
<param name="cacheManagerName"></param>
 25 
        
///
 
<param name="key"></param>
 26 
        
///
 
<param name="obj"></param>
 27 
        
///
 
<param name="file"></param>
 28 
        
void
 Add(
string
 cacheManagerName, 
string
 key, Object obj, 
string
 file);
 29 
        
///
 
<summary>
 30 
        
///
 
 31 
        
///
 
</summary>
 32 
        
///
 
<param name="key"></param>
 33 
        
///
 
<param name="obj"></param>
 34 
        
void
 Add(
string
 key, Object obj);
 35 
        
///
 
<summary>
 36 
        
///
 
 37 
        
///
 
</summary>
 38 
        
///
 
<param name="cacheManagerName"></param>
 39 
        
///
 
<param name="key"></param>
 40 
        
///
 
<param name="obj"></param>
 41 
        
void
 Add(
string
 cacheManagerName, 
string
 key, Object obj);
 42 
        
///
 
<summary>
 43 
        
///
 
 44 
        
///
 
</summary>
 45 
        
void
 Flush();
 46 
        
///
 
<summary>
 47 
        
///
 
 48 
        
///
 
</summary>
 49 
        
///
 
<param name="cacheManagerName"></param>
 50 
        
void
 Flush(
string
 cacheManagerName);
 51 
        
///
 
<summary>
 52 
        
///
 
 53 
        
///
 
</summary>
 54 
        
///
 
<param name="key"></param>
 55 
        
///
 
<returns></returns>
 56 
        Object GetData(
string
 key);
 57 
        
///
 
<summary>
 58 
        
///
 
 59 
        
///
 
</summary>
 60 
        
///
 
<param name="cacheManagerName"></param>
 61 
        
///
 
<param name="key"></param>
 62 
        
///
 
<returns></returns>
 63 
        Object GetData(
string
 cacheManagerName, 
string
 key);
 64 
        
///
 
<summary>
 65 
        
///
 
 66 
        
///
 
</summary>
 67 
        
///
 
<typeparam name="T"></typeparam>
 68 
        
///
 
<param name="key"></param>
 69 
        
///
 
<returns></returns>
 70 
        T GetData
<
T
>
(
string
 key);
 71 
        
///
 
<summary>
 72 
        
///
 
 73 
        
///
 
</summary>
 74 
        
///
 
<typeparam name="T"></typeparam>
 75 
        
///
 
<param name="cacheManagerName"></param>
 76 
        
///
 
<param name="key"></param>
 77 
        
///
 
<returns></returns>
 78 
        T GetData
<
T
>
(
string
 cacheManagerName, 
string
 key);
 79 
        
///
 
<summary>
 80 
        
///
 
 81 
        
///
 
</summary>
 82 
        
///
 
<param name="cacheManagerName"></param>
 83 
        
///
 
<param name="key"></param>
 84 
        
void
 Remove(
string
 cacheManagerName, 
string
 key);
 85 
        
///
 
<summary>
 86 
        
///
 
 87 
        
///
 
</summary>
 88 
        
///
 
<param name="key"></param>
 89 
        
void
 Remove(
string
 key);
 90 
    }
 91 
 92 
    
public
 
interface
 IHttpRunTimeCacheManager
 93 
    {
 94 
        
///
 
<summary>
 95 
        
///
 往缓存写数据
 96 
        
///
 
</summary>
 
 97 
        
///
 
<param name="key">
缓存的键值
</param>
 98 
        
///
 
<param name="obj">
要放入缓存的数据
</param>
 99 
        
///
 
<param name="sqlDependncy">
与数据库依赖的对象
</param>
100 
        
void
 AddRunTimeCache(
string
 key, Object obj, System.Web.Caching.SqlCacheDependency sqlDependncy);
101 
        
///
 
<summary>
102 
        
///
 获取运行时的缓存
103 
        
///
 
</summary>
104 
        
///
 
<param name="key"></param>
105 
        
///
 
<returns></returns>
106 
        
object
 GetRunTimeCache(
string
 key);
107 
        
///
 
<summary>
108 
        
///
 移除运行时缓存对象
109 
        
///
 
</summary>
110 
        
///
 
<param name="key"></param>
111 
        
void
 RemoveRuntimeCache(
string
 key);
112 
    }
113
ExpandedBlockStart.gif
View Code
  1 
using
 System;
  2 
using
 System.Collections.Generic;
  3 
using
 System.Text;
  4 
using
 Microsoft.Practices.EnterpriseLibrary.Caching;
  5 
using
 Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
  6 
using
 Secom.Framework.Common.Util;
  7 
using
 System.Configuration;
  8 
  9 
namespace
 Secom.Framework.Common
 10 
{
 11 
    
///
 
<summary>
 12 
    
///
 缓存处理类
 13 
    
///
 
</summary>
 14 
    
public
 
class
 CacheManager : ICacheManager
 15 
    {
 16 
        
#region
 常量定义
 17 
        
///
 
<summary>
默认缓存策略 60秒
</summary>
 18 
        
public
 
const
 
string
 DefalutCacheManager 
=
 
"
Default Cache Manager
"
;
 19 
        
///
 
<summary>
基础数据缓存策略 120秒
</summary>
 20 
        
public
 
const
 
string
 BaseDataCacheManager 
=
 
"
Base Data Cache Manager
"
;
 21 
        
///
 
<summary>
临时数据缓存策略 30秒
</summary>
 22 
        
public
 
const
 
string
 TempDataCacheManager 
=
 
"
Temp Data Cache Manager
"
;
 23 
        
///
 
<summary>
业务数据缓存策略 80秒
</summary>
 24 
        
public
 
const
 
string
 BusinessDataCacheManager 
=
 
"
Business Data Cache Manager
"
 25 
        
#endregion
 26 
 27 
        
#region
 构造函数
 28 
        
///
 
<summary>
 29 
        
///
 实例化 
<see cref="T:CacheManager"/>
 类.
 30 
        
///
 
</summary>
 31 
        
protected
 CacheManager()
 32 
        {
 33 
        }
 34 
        
#endregion
 35 
 36 
        
#region
 ICacheManager 实例
 37 
        
///
 
<summary></summary>
 38 
        
public
 
static
 ICacheManager Instance 
=
 
new
 CacheManager();
 39 
 40 
        
#endregion
 41 
 42 
        
#region
 实现ICacheManager
 43 
        
///
 
<summary>
 44 
        
///
 往缓存写数据
 45 
        
///
 
</summary>
 46 
        
///
 
<param name="key">
缓存的键值
</param>
 47 
        
///
 
<param name="obj">
要放入缓存的数据
</param>
 48 
        
public
 
void
 Add(
string
 key, Object obj)
 49 
        {
 50 
            Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager
 51 
                cacheManager 
=
 CacheFactory.GetCacheManager();
 52 
 53 
            cacheManager.Add(key, obj);
 54 
        }
 55 
 56 
        
///
 
<summary>
 57 
        
///
 往缓存写数据
 58 
        
///
 
</summary>
 59 
        
///
 
<param name="cacheManagerName">
cache manager 的名字
</param>
 60 
        
///
 
<param name="key">
缓存的键值
</param>
 61 
        
///
 
<param name="obj">
要放入缓存的数据
</param>
 62 
        
public
 
void
 Add(
string
 cacheManagerName, 
string
 key, Object obj)
 63 
        {
 64 
            Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager
 65 
                cacheManager 
=
 CacheFactory.GetCacheManager(cacheManagerName);
 66 
 67 
            cacheManager.Add(key, obj);
 68 
        }
 69 
 70 
        
///
 
<summary>
 71 
        
///
 往缓存写数据
 72 
        
///
 
</summary>
 73 
        
///
 
<param name="cacheManagerName">
cache manager 的名字
</param>
 74 
        
///
 
<param name="key">
缓存的键值
</param>
 75 
        
///
 
<param name="obj">
要放入缓存的数据
</param>
 76 
        
///
 
<param name="duration">
缓存有效时间
</param>
 77 
        
public
 
void
 Add(
string
 cacheManagerName, 
string
 key, Object obj, 
int
 duration)
 78 
        {
 79 
            Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager
 80 
                cacheManager 
=
 CacheFactory.GetCacheManager();
 81 
 82 
            cacheManager.Add(
 83 
                 key, obj, CacheItemPriority.Normal, 
null
,
 84 
                 
new
 SlidingTime(TimeSpan.FromSeconds(
600
)));
 85 
            
//
cacheManager.Add(
 86 
            
//
     key, obj, CacheItemPriority.Normal, null,
 87 
            
//
     new SlidingTime(TimeSpan.FromSeconds(ParamUtil.getint(
 88 
            
//
         ConfigurationManager.AppSettings[BusinessDataCacheManager]))));
 89 
        }
 90 
 91 
 92 
        
///
 
<summary>
 93 
        
///
 往缓存写数据
 94 
        
///
 
</summary>
 95 
        
///
 
<param name="cacheManagerName">
cache manager 的名字
</param>
 96 
        
///
 
<param name="key">
缓存的键值
</param>
 97 
        
///
 
<param name="obj">
要放入缓存的数据
</param>
 98 
        
///
 
<param name="file">
依赖的文件,当文件被修改后缓存自动失效
</param>
 99 
        
public
 
void
 Add(
string
 cacheManagerName, 
string
 key, Object obj, 
string
 file)
100 
        {
101 
            Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager
102 
                cacheManager 
=
 CacheFactory.GetCacheManager(cacheManagerName);
103 
104 
            cacheManager.Add(key, obj, CacheItemPriority.Normal, 
null
new
 FileDependency(file));
105 
        }
106 
107 
        
///
 
<summary>
108 
        
///
 从缓存移除数据
109 
        
///
 
</summary>
110 
        
///
 
<param name="key">
缓存的键值
</param>
111 
        
public
 
void
 Remove(
string
 key)
112 
        {
113 
            Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager
114 
                cacheManager 
=
 CacheFactory.GetCacheManager( );
115 
            cacheManager.Remove(key);
116 
        }
117 
118 
        
///
 
<summary>
119 
        
///
 从缓存移除数据
120 
        
///
 
</summary>
121 
        
///
 
<param name="cacheManagerName">
cache manager 的名字
</param>
122 
        
///
 
<param name="key">
缓存的键值
</param>
123 
        
public
 
void
 Remove(
string
 cacheManagerName, 
string
 key)
124 
        {
125 
            Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager
126 
                cacheManager 
=
 CacheFactory.GetCacheManager(cacheManagerName);
127 
            cacheManager.Remove(key);
128 
        }
129 
130 
131 
        
///
 
<summary>
132 
        
///
 清空缓存里面的所有数据
133 
        
///
 
</summary>
134 
        
public
 
void
 Flush()
135 
        {
136 
            Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager
137 
                cacheManager 
=
 CacheFactory.GetCacheManager();
138 
            cacheManager.Flush();
139 
        }
140 
141 
        
///
 
<summary>
142 
        
///
 清空缓存里面的所有数据
143 
        
///
 
</summary>
144 
        
///
 
<param name="cacheManagerName">
cache manager 的名字
</param>
145 
        
public
 
void
 Flush(
string
 cacheManagerName)
146 
        {
147 
            Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager
148 
                cacheManager 
=
 CacheFactory.GetCacheManager(cacheManagerName);
149 
            cacheManager.Flush();
150 
        }
151 
152 
        
///
 
<summary>
153 
        
///
 从缓存里面获取数据.
154 
        
///
 
</summary>
155 
        
///
 
<param name="key">
缓存的键值
</param>
156 
        
///
 
<returns></returns>
157 
        
public
 Object GetData(
string
 key)
158 
        {
159 
            Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager
160 
                cacheManager 
=
 CacheFactory.GetCacheManager();
161 
            
return
 cacheManager.GetData(key);
162 
        }
163 
164 
        
///
 
<summary>
165 
        
///
 从缓存里面获取数据.
166 
        
///
 
</summary>
167 
        
///
 
<param name="key">
缓存的键值
</param>
168 
        
///
 
<returns></returns>
169 
        
public
 T GetData
<
T
>
(
string
 key)
170 
        {
171 
            Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager
172 
                cacheManager 
=
 CacheFactory.GetCacheManager();
173 
            T obj 
=
 (T)cacheManager.GetData(key);
174 
            
return
 obj;
175 
        }
176 
177 
        
///
 
<summary>
178 
        
///
 从缓存里面获取数据.
179 
        
///
 
</summary>
180 
        
///
 
<param name="cacheManagerName">
cache manager 的名字
</param>
181 
        
///
 
<param name="key">
缓存的键值
</param>
182 
        
///
 
<returns></returns>
183 
        
public
 Object GetData(
string
 cacheManagerName, 
string
 key)
184 
        {
185 
            Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager
186 
                cacheManager 
=
 CacheFactory.GetCacheManager(cacheManagerName);
187 
            
return
 cacheManager.GetData(key);
188 
        }
189 
190 
        
///
 
<summary>
191 
        
///
 从缓存里面获取数据.
192 
        
///
 
</summary>
193 
        
///
 
<param name="cacheManagerName">
cache manager 的名字
</param>
194 
        
///
 
<param name="key">
缓存的键值
</param>
195 
        
///
 
<returns></returns>
196 
        
public
 T GetData
<
T
>
(
string
 cacheManagerName, 
string
 key)
197 
        {
198 
            Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager
199 
                cacheManager 
=
 CacheFactory.GetCacheManager(cacheManagerName);
200 
            T obj 
=
 (T) cacheManager.GetData(key);
201 
            
return
 obj;
202 
        }
203 
204 
        
#endregion
205 
206 
        
#region
 -- HttpRuntime Cache --
207 
208 
        
///
 
<summary>
209 
        
///
 往缓存写数据
210 
        
///
 
</summary>
 
211 
        
///
 
<param name="key">
缓存的键值
</param>
212 
        
///
 
<param name="obj">
要放入缓存的数据
</param>
213 
        
///
 
<param name="sqlDependncy">
与数据库依赖的对象
</param>
214 
        
public
 
void
 AddRunTimeCache(
string
 key, Object obj, System.Web.Caching.SqlCacheDependency sqlDependncy)
215 
        {
/*
, DateTime.Now.AddDays(100),
216 
                    System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null
*/
217 
            System.Web.HttpRuntime.Cache.Insert(key, obj, sqlDependncy);
218 
        }
219 
220 
        
///
 
<summary>
221 
        
///
 获取运行时的缓存
222 
        
///
 
</summary>
223 
        
///
 
<param name="key"></param>
224 
        
///
 
<returns></returns>
225 
        
public
 
object
 GetRunTimeCache(
string
 key)
226 
        {
227 
            
return
 System.Web.HttpRuntime.Cache[key];
228 
        }
229 
230 
        
///
 
<summary>
231 
        
///
 移除运行时缓存对象
232 
        
///
 
</summary>
233 
        
///
 
<param name="key"></param>
234 
        
public
 
void
 RemoveRuntimeCache(
string
 key)
235 
        {
236 
            System.Web.HttpRuntime.Cache.Remove(key);
237 
        }
238 
239 
        
#endregion
240 
    }
241 
}
 
}

转载于:https://www.cnblogs.com/mybluesky99/archive/2011/06/15/2081535.html

你可能感兴趣的文章