WEB开发网
开发学院WEB开发ASP.NET 简单的缓存处理法 阅读

简单的缓存处理法

 2010-11-06 14:18:16 来源:WEB开发网   
核心提示:using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Caching; /// <summary>///缓存操作///Date:2010-11-04///created by z
using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Caching;

 

/// <summary>

///缓存操作

///Date:2010-11-04

///created by zhouxg

/// </summary>

public class CacheHelper<T> where T : new()

{

    public CacheHelper()

    {

        //

        //TODO: 在此处添加构造函数逻辑

        //

    }

    /// <summary>

    /// 创建缓存

    /// </summary>

    /// <param name="_key"></param>

    /// <param name="ce"></param>

    /// <returns></returns>

    public bool InsertCache(string _key, T t)

    {

        try

        {

            if (HttpRuntime.Cache.Get(_key) == null)

                HttpRuntime.Cache.Insert(_key, t, null, DateTime.Now.AddMinutes(double.Parse(ConfigurationManager.AppSettings["CheckCodeTime"])), Cache.NoSlidingExpiration);

            else

                return false;

        }

        catch { return false; }

        return true;

    }

 

    /// <summary>

    /// 获取缓存

    /// </summary>

    /// <param name="_key"></param>

    /// <returns></returns>

    public T GetCache(string _key)

    {

        if (HttpRuntime.Cache.Get(_key) != null)

            return (T)HttpRuntime.Cache.Get(_key);

        else

            return default(T);

    }

 

    /// <summary>

    /// 移除缓存

    /// </summary>

    /// <param name="_key"></param>

    /// <returns></returns>

    public bool RemoveCache(string _key)

    {

        try

        {

            if (HttpRuntime.Cache.Get(_key) != null)

                HttpRuntime.Cache.Remove(_key);

        }

        catch { return false; }

        return true;

    }

}

Web缓存技术概述

http://tech.cncms.com/web/asp/21519.html

Tags:缓存

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接