C# 缓存操作类

2021-05-07 09:27

阅读:562

标签:run   text   move   pru   log   hash   object c   col   object   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;
 
namespace Utility
{
    /// 
    /// 缓存操作,默认缓存1分钟
    /// 
    public static class CacheHelper
    {
        static int cacheTime = 1;
 
        /// 
        /// 读取缓存项
        /// 
        /// 
        public static object CacheReader(string cacheKey)
        {
            return HttpRuntime.Cache[cacheKey];
        }
 
        /// 
        /// 写入缓存项
        /// 
        public static void CacheWriter(string cacheKey, object cacheValue, int cache_time = 0)
        {
            HttpRuntime.Cache.Insert(cacheKey, cacheValue, null,
                DateTime.Now.AddMinutes(cache_time 
        /// 移除指定缓存项
        /// 
        public static void CacheRemove(string cacheName)
        {
            HttpRuntime.Cache.Remove(cacheName);
        }
 
        /// 
        /// 缓存对象泛型实现
        /// 
        public static T ObjectReader(string cacheKey = null)
            where T : class
        {
            string cachekey = typeof(T).GetHashCode() + StringHelper.ToString(cacheKey);
            var obj = CacheReader(cachekey) as T;
            return obj;
        }
 
        /// 
        /// 缓存对象泛型实现
        /// 
        public static void ObjectWriter(T cacheValue, string cacheKey = null, int cache_time = 0)
            where T : class
        {
            string cachekey = typeof (T).GetHashCode() + StringHelper.ToString(cacheKey);
            CacheWriter(cachekey, cacheValue, cache_time);
        }
    }
}

  

C# 缓存操作类

标签:run   text   move   pru   log   hash   object c   col   object   

原文地址:http://www.cnblogs.com/XuPengLB/p/7644619.html

上一篇:MDI窗体应用

下一篇:C#基础之--类型基础


评论


亲,登录后才可以留言!