简单的缓存处理类

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;

    }


 

posted @ 2010-11-05 21:56  JasonChou  阅读(361)  评论(0编辑  收藏  举报