HttpRuntime.Cache與HttpContext.Current.Cache 為同一個對象
HttpRuntime.Cache.Add 存在相同的鍵會異常,返回緩存成功的對象 HttpRuntime.Cache.Insert存在相同的鍵會替換無返回值
HttpRuntime.Cache["key"] 使用字典的方式也可以讀取和設置
HttpRuntime.Cache.Insert(key, value, null, DateTime.Now.AddSeconds(seconds), TimeSpan.Zero); //設置絕對過期時間 到了指定時間以后會失效 ps: TimeSpan.Zero == System.Web.Caching.Cache.NoSlidingExpiration
HttpRuntime.Cache.Insert(key, value, null, DateTime.MaxValue, TimeSpan.FromSeconds(seconds)); //設置相對過期時間 指定時間內無訪問會失效 ps: DateTime.MaxValue == System.Web.Caching.Cache.NoAbsoluteExpiration
本文參考:http://blog.csdn.net/ttotcs/article/details/7476234
System.Web.HttpRuntime.Cache的方法:
Add
Insert
Get
Remove
緩存的操作包括:讀、寫。
讀取緩存內容調用System.Web.HttpRuntime.Cache.Get(Key)方法,插入緩存數據調用Add或Insert方法。
Add與Insert的不同
HttpRuntime.Cache.Add 存在相同的鍵會異常,返回緩存成功的對象。
HttpRuntime.Cache.Insert存在相同的鍵會替換原值,無返回值。
如果您希望某個緩存項目一旦放入緩存后,就不要再被修改,那么調用Add確實可以防止后來的修改操作。而調用Insert方法,則永遠會覆蓋已存在項。
緩存的過期時間
緩存過期時間包括:絕對過期和滑動過期。
絕對過期:到了指定時間以后便會失效。
滑動過期:在指定時間內無訪問請求便失效。
實例:
絕對過期:
HttpRuntime.Cache.Insert(key, value, null, DateTime.Now.AddSeconds(seconds),System.Web.Caching.Cache.NoSlidingExpiration);
滑動過期:
HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration
, TimeSpan.FromSeconds(seconds));
緩存項移除優先級
// 指定 Cache 對象中存儲的項的相對優先級。
public enum CacheItemPriority
{
// 在服務器釋放系統內存時,具有該優先級級別的緩存項最有可能被從緩存刪除。
Low = 1,
// 在服務器釋放系統內存時,具有該優先級級別的緩存項比分配了 CacheItemPriority.Normal
// 優先級的項更有可能被從緩存刪除。
BelowNormal = 2,
// 在服務器釋放系統內存時,具有該優先級級別的緩存項很有可能被從緩存刪除,
// 其被刪除的可能性僅次於具有 CacheItemPriority.Low
// 或 CacheItemPriority.BelowNormal 優先級的那些項。這是默認選項。
Normal = 3,
// 緩存項優先級的默認值為 CacheItemPriority.Normal。
Default = 3,
// 在服務器釋放系統內存時,具有該優先級級別的緩存項被刪除的可能性
// 比分配了 CacheItemPriority.Normal 優先級的項要小。
AboveNormal = 4,
// 在服務器釋放系統內存時,具有該優先級級別的緩存項最不可能被從緩存刪除。
High = 5,
// 在服務器釋放系統內存時,具有該優先級級別的緩存項將不會被自動從緩存刪除。
// 但是,具有該優先級級別的項會根據項的絕對到期時間或可調整到期時間與其他項一起被移除。
NotRemovable = 6,
}
參考文章 http://kb.cnblogs.com/page/69483/
http://www.cnblogs.com/zgx/archive/2009/03/16/1413643.html
http://www.cnblogs.com/zhangxp1129/archive/2012/09/05/2671522.html
MSDN: http://msdn.microsoft.com/zh-cn/magazine/system.web.caching.cache(VS.85).aspx
對於每個應用程序域均創建該類的一個實例,並且只要對應的應用程序域保持活動,該實例便保持有效。
注意: |
|---|
| Cache 類不能在 ASP.NET 應用程序外使用。它是為在 ASP.NET 中用於為 Web 應用程序提供緩存而設計和測試的。在其他類型的應用程序(如控制台應用程序或 Windows 窗體應用程序)中,ASP.NET 緩存可能無法正常工作。 |
System.Web.Caching是用來管理緩存的命名空間,其父級空間是System.Web,由此可見,緩存通常用於Web網站的開發,包括在B/S項目中的開發。
緩存的設計主要是考慮到網絡帶寬可能會延緩數據的提交與回發,如果把數據保存在客戶端,用戶就可以直接從客戶端讀取數據,減少客戶端與服務器端的數據交互,提高程序的性能。
那么System.Web.Caching可以使用到WinForm程序中嗎?
如果用的是winform,基本上不用想這個問題,因為你的程序本身就在內存里運行着。winfrom 直接用內存用 數據字典如果是
web,緩存就是將常用的數據放到服務器的內存中,當有不同的客戶請求相同的數據時,直接從內存讀取,以此提高性能。
簡單點:WebForm是“瘦客戶端”,占用服務器資源。WinForm是“胖客戶單”,占用的是本地客戶端內存。
推薦兩種寫法:
一、是web項目中如何使用。
本文轉載:http://www.cnblogs.com/wgx0428/p/3181307.html
/// <summary>
/// 獲取數據緩存
/// </summary>
/// <param name="CacheKey">鍵</param>
public static object GetCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
}
/// <summary>
/// 設置數據緩存
/// </summary>
public static void SetCache(string CacheKey, object objObject)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
}
/// <summary>
/// 設置數據緩存
/// </summary>
public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
}
/// <summary>
/// 設置數據緩存
/// </summary>
public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
}
/// <summary>
/// 移除指定數據緩存
/// </summary>
public static void RemoveAllCache(string CacheKey)
{
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
_cache.Remove(CacheKey);
}
/// <summary>
/// 移除全部緩存
/// </summary>
public static void RemoveAllCache()
{
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
_cache.Remove(CacheEnum.Key.ToString());
}
}
二、是winform程序中
(1)靜態變量緩存:
View Code
{
using System.Collections.Generic;
/// <summary>
/// 全局統一的緩存類
/// </summary>
public class Cache
{
private SortedDictionary< string, string> dic = new SortedDictionary< string, string>();
private static volatile Cache instance = null;
private static object lockHelper = new object();
private Cache()
{
}
public void Add( string key, string value)
{
dic.Add(key, value);
}
public void Remove( string key)
{
dic.Remove(key);
}
public string this[ string index]
{
get
{
if (dic.ContainsKey(index))
return dic[index];
else
return null;
}
set { dic[index] = value; }
}
public static Cache Instance
{
get
{
if (instance == null)
{
lock (lockHelper)
{
if (instance == null)
{
instance = new Cache();
}
}
}
return instance;
}
}
}
}
(2)內存緩存MemoryCach:表示實現內存中的緩存的類型。(注意此類僅僅NET4.0以上支持)
本文轉載:http://www.cnblogs.com/jinzhao/archive/2012/06/11/2545450.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
namespace CNBlogs.Zzk.Domain.Entities
{
public class DictionaryCacheManager<TK, TV>
{
private ObjectCache memoryCache;
public DictionaryCacheManager():this(null){}
public DictionaryCacheManager(string name)
{
memoryCache = new MemoryCache(string.Format("{0}-{1}-{2}", typeof (TK).Name, typeof (TV).Name, name));
}
public TV Get(TK key,Func<TV> getValue)
{
if(memoryCache.Contains(key.ToString()))
{
return (TV)memoryCache[key.ToString()];
}
else
{
var policy = new CacheItemPolicy();
var v = getValue();
object o = v;
memoryCache.Set(key.ToString(), o, policy);
return v;
}
}
public TV Get(TK key, Func<TV> getValue,DateTimeOffset dateTimeOffset)
{
if (memoryCache.Contains(key.ToString()))
{
return (TV)memoryCache[key.ToString()];
}
else
{
var v = getValue();
object o = v;
memoryCache.Set(key.ToString(), o, dateTimeOffset);
return v;
}
}
public void Clear()
{
memoryCache.ToList().ForEach(kv => memoryCache.Remove(kv.Key));
}
public void Clear(TK key)
{
memoryCache.Remove(key.ToString());
}
}
}

注意: