c# Cache在应用程序里边的应用

求个例子!
2025-03-04 06:18:36
推荐回答(3个)
回答1:

using System;
using System.Web;
using System.Web.Caching;
using System.Data;
using System.Collections.Generic;
using System.Text;

namespace Common
{
///


/// 网站碎片缓存处理类
///

public class WebCache
{
///
/// 网站碎片缓存处理类
///

/// Cache对象的键名
public WebCache(string key) { this.KeyName = key; }

private string KeyName = "";
private Cache c = new Cache();

public void SetCache(object obj, int Seconds)
{
c.Insert(this.KeyName, obj, null, DateTime.Now.AddSeconds(Seconds), TimeSpan.Zero);
}

public object GetCache()
{
return c[this.KeyName];
}

}
}

用我这个类就可以了,比楼上的封装性更好

回答2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace ETIMES.B2B.Common
{

///


/// 缓存相关的操作类
/// Copyright (C) Maticsoft
///

public class DataCache
{
///
/// 获取当前应用程序指定CacheKey的Cache值
///

///
///
public static object GetCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;

return objCache[CacheKey];
}

///
/// 设置当前应用程序指定CacheKey的Cache值
///

///
///
public static void SetCache(string CacheKey, object objObject)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
}

///
/// 设置当前应用程序指定CacheKey的Cache值
///

///
///
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);
}

///
/// 移除指定的key
///

///
public static void Remove(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Remove(CacheKey);

}

}

}

回答3:

一个双向链表
+
一条检查线程
+
一个哈希表,做成一个超时优先级队列。