gpt4 book ai didi

ASP.NET缓存处理类实例

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 24 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章ASP.NET缓存处理类实例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例讲述了ASP.NET缓存处理类。分享给大家供大家参考。具体如下: ASP.NET 缓存处理类.

用法:

Just copy this code into a new class file (.cs) and add it to your ASP .NET website. One thing to keep in mind is that data stored in ASP .NET Cache can be accessible across all sessions. So when creating a cacheID for the object to be stored, it must be unique (or it could be overwritten). I usually store the unique cacheID in the session and then use that to referrence the cacheID. (e.g. CacheHandler.Write(Session["MyCacheData"], myData);) 。

具体代码如下:

?
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
using System;
using System.Collections.Generic;
using System.Web.Caching;
using System.Web;
/// <summary>
/// This class reads/writes to ASP .NET server cache. For the sake of
/// simplicity, the class writes objects to cache with no expirateion.
/// Use the Remove() function to programmatically remove objects stored
/// from the server cache. This class was created as an alternative to
/// storing large objects in the session.
/// </summary>
public class CacheHandler
{
   public static bool Write(string cacheID, object data)
   {
     if (HttpContext.Current == null )
       return false ;
     if (cacheID == null || cacheID.Equals( "" ))
       return false ;
     HttpRuntime.Cache.Insert(
         cacheID, data, null , Cache.NoAbsoluteExpiration,
         Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null
         );
     return true ;
   }
   public static object Read(string cacheID)
   {
     if (HttpContext.Current == null )
       return null ;
     return HttpRuntime.Cache.Get(cacheID);
   }
   public static void Remove(string cacheID)
   {
     if (HttpContext.Current == null )
       return ;
     if (cacheID == null || cacheID.Equals( "" ))
       return ;
     HttpRuntime.Cache.Remove(cacheID);
   }
}

希望本文所述对大家的asp.net程序设计有所帮助.

最后此篇关于ASP.NET缓存处理类实例的文章就讲到这里了,如果你想了解更多关于ASP.NET缓存处理类实例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com