asp.net에서 캐시 클래스 DataCache (파일 캐시와 시간 캐시에 의존하거나 둘 다)
끊임없는 수정과 운행 테스트를 거쳐 실제 프로젝트에서 잘 사용되었다.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.IO;
namespace Tools.Web
{
/// <summary>
/// , :
/// object obj = DataCache.GetCache("file1",depfile);
///if (obj == null)
///{
/// string txt = " ";//
/// DataCache.SetCacheDepFile("file1", txt, depfile);
/// }
/// else
/// {
/// string txt=obj.ToString();
/// }
/// </summary>
public partial class DataCache
{
#region web.config
private static string _webconfigfile = string.Empty;
/// <summary>
/// web.config
/// </summary>
public static string webconfigfile
{
get
{
if (string.IsNullOrEmpty(_webconfigfile)) _webconfigfile = HttpContext.Current.Server.MapPath("/web.config");
return _webconfigfile;
}
}
#endregion
#region App_Data/ShopConfig.config
private static string _shopconfigfile = string.Empty;
/// <summary>
/// App_Data/ShopConfig.config
/// </summary>
public static string shopconfigfile
{
get
{
if (string.IsNullOrEmpty(_shopconfigfile)) _shopconfigfile = HttpContext.Current.Server.MapPath("/App_Data/ShopConfig.config");
return _shopconfigfile;
}
}
#endregion
#region App_Data/SiteConfig.config
private static string _siteconfigfile = string.Empty;
/// <summary>
/// App_Data/SiteConfig.config
/// </summary>
public static string siteconfigfile
{
get
{
if (string.IsNullOrEmpty(_siteconfigfile)) _siteconfigfile = HttpContext.Current.Server.MapPath("/App_Data/SiteConfig.config");
return _siteconfigfile;
}
}
#endregion
#region App_Data/Template.config
private static string _templateconfigfile = string.Empty;
/// <summary>
/// App_Data/Template.config
/// </summary>
public static string templateconfigfile
{
get
{
if (string.IsNullOrEmpty(_templateconfigfile)) _templateconfigfile = HttpContext.Current.Server.MapPath("/App_Data/Template.config");
return _templateconfigfile;
}
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="CacheKey"> </param>
public static void DeleteCache(string CacheKey)
{
HttpRuntime.Cache.Remove(CacheKey);
}
#endregion
#region ,
/// <summary>
/// ,
/// </summary>
/// <param name="CacheKey"> </param>
/// <returns></returns>
public static object GetCache(string CacheKey)
{
object obj_time=HttpRuntime.Cache[CacheKey + "_time"];
object obj_cache=HttpRuntime.Cache[CacheKey];
if (obj_time != null && obj_cache!=null)
{
if (Convert.ToDateTime(obj_time) < DateTime.Now)
{
DeleteCache(CacheKey);
DeleteCache(CacheKey + "_time");
return null;
}
else return obj_cache;
}
else
{
DeleteCache(CacheKey);
DeleteCache(CacheKey+"_time");
return null;
}
}
#endregion
#region ,
/// <summary>
/// ,
/// </summary>
/// <param name="CacheKey"> </param>
/// <param name="depFile"> </param>
/// <returns></returns>
public static object GetCache(string CacheKey, string depFile)
{
object obj_time = HttpRuntime.Cache[CacheKey + "_time"];
object obj_cache = HttpRuntime.Cache[CacheKey];
if (File.Exists(depFile))
{
FileInfo fi = new FileInfo(depFile);
if (obj_time != null && obj_cache != null)
{
if (Convert.ToDateTime(obj_time) != fi.LastWriteTime)
{
DeleteCache(CacheKey);
DeleteCache(CacheKey + "_time");
return null;
}
else return obj_cache;
}
else
{
DeleteCache(CacheKey);
DeleteCache(CacheKey + "_time");
return null;
}
}
else
{
throw new Exception(" (" + depFile + ") !");
}
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="CacheKey"> </param>
/// <param name="objObject"> </param>
public static void SetCache(string CacheKey, object objObject)
{
HttpRuntime.Cache.Insert(CacheKey, objObject);
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="CacheKey"> </param>
/// <param name="objObject"> </param>
/// <param name="absoluteExpiration"> </param>
/// <param name="slidingExpiration"> , null </param>
public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
if (slidingExpiration == null) slidingExpiration = Cache.NoSlidingExpiration;
HttpRuntime.Cache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
HttpRuntime.Cache.Insert(CacheKey + "_time", absoluteExpiration, null, absoluteExpiration, slidingExpiration);//
}
#endregion
#region ,
/// <summary>
/// ,
/// </summary>
/// <param name="CacheKey"> </param>
/// <param name="objObject"> </param>
/// <param name="seconds"> </param>
/// <param name="slidingExpiration"> null </param>
public static void SetCacheSecond(string CacheKey, object objObject, int seconds, TimeSpan slidingExpiration)
{
DateTime absoluteExpiration = DateTime.Now.AddSeconds(seconds);
if (slidingExpiration == null) slidingExpiration = Cache.NoSlidingExpiration;
HttpRuntime.Cache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
HttpRuntime.Cache.Insert(CacheKey + "_time", absoluteExpiration, null, absoluteExpiration, slidingExpiration);//
}
#endregion
#region ,
/// <summary>
/// ,
/// </summary>
/// <param name="CacheKey"> </param>
/// <param name="objObject"> </param>
/// <param name="depfilename"> , DataCache </param>
public static void SetCacheDepFile(string CacheKey, object objObject, string depfilename)
{
//
System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(depfilename);
DateTime absoluteExpiration = System.Web.Caching.Cache.NoAbsoluteExpiration;
TimeSpan slidingExpiration=System.Web.Caching.Cache.NoSlidingExpiration;
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(
CacheKey,
objObject,
dep,
System.Web.Caching.Cache.NoAbsoluteExpiration, //
slidingExpiration, //
System.Web.Caching.CacheItemPriority.Default,
null);
if (File.Exists(depfilename))
{
FileInfo fi = new FileInfo(depfilename);
DateTime lastWriteTime = fi.LastWriteTime;
HttpRuntime.Cache.Insert(CacheKey + "_time", lastWriteTime, null, absoluteExpiration, slidingExpiration);//
}
}
#endregion
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.