Sử dụng Caching API trong ASP.NET
Trả lời :
Đây là cách xác định thời gian hết hạn (expiration) của các dữ liệu trong cache dưới client . Sau khi thời gian hết hạn bất cứ hành động để truy xuất dữ liệu sẽ không thành công. Xin mời các bạn tham khảo.
art009_cachingdemo.aspx
<%@ Page Language=''C#'' %> <script language=''C#'' runat=''server''> protected void Page_Load(object sender, EventArgs e) { // Absolute expiration. Specify the EXACT TIME // when the cached content will expire string cacheItem1 = (string)Cache[''absolute'']; if(cacheItem1 == null) { cacheItem1 = DateTime.Now.ToString(); Cache.Insert(''absolute'', cacheItem1, null, DateTime.Now.AddSeconds(10), TimeSpan.Zero); } absoluteData.Text = cacheItem1; string cacheItem2 = (string)Cache[''sliding'']; if(cacheItem2 == null) { cacheItem2 = DateTime.Now.ToString(); Cache.Insert(''sliding'', cacheItem2, null, DateTime.Empty, new TimeSpan(0,0,10)); } slidingData.Text = cacheItem2; string cacheItem3 = (string)Cache[''dependency'']; if(cacheItem3 == null) { cacheItem3 = DateTime.Now.ToString(); Cache.Insert(''dependency'', cacheItem3, new CacheDependency(Server.MapPath(''art009_cachekey.txt''))); } dependencyData.Text = cacheItem3; } </script> <html> <head><title>Caching API Demo</title></head> <body> Absolute expiration cached value: <asp:label id=''absoluteData'' runat=''server'' /> <br /> Sliding expiration cached value: <asp:label id=''slidingData'' runat=''server'' /> <br /> File Dependency expiration cached value: <asp:label id=''dependencyData'' runat=''server'' /> </body> </html> |
Khi refresh trang, và bạn sẽ thấy dữ liệu trong cache thay đổi mỗi 10 giây ngoại từ trường hợp cuối cùng, nó thay tậ ptin kèm theo sẽ thay đổi theo
art009_cachekey.txt
art009_cachekey.txt
Bạn có thể tải source tại đây
Không có nhận xét nào:
Đăng nhận xét