码迷,mamicode.com
首页 > Web开发 > 详细

Asp.Net Cache辅助类

时间:2014-07-14 00:23:19      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   文件   

辅助类代码

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

namespace BusinessRules.Common
{
    /// <summary>
    /// Cache高度缓存
    /// </summary>
    public class CacheManager
    {
        #region --Property--
        private static string _cacheKey = String.Empty;
        private static String _sqlDepName = "CacheData";

        /// <summary>
        /// 缓存索引键值
        /// </summary>
        public static string CacheKey
        {
            get { return CacheManager._cacheKey; }
            set { CacheManager._cacheKey = value; }
        }

        /// <summary>
        /// 缓存依赖名称
        /// </summary>
        public static String SqlDepName
        {
            get { return CacheManager._sqlDepName; }
            set { CacheManager._sqlDepName = value; }
        }
        #endregion

        #region --Method--
        /// <summary>
        /// 获取当前应用程序指定cacheKey的Cache对象值
        /// </summary>
        /// <param name="cacheKety">索引键值</param>
        /// <returns>返回对象</returns>
        public static Object GetCache(String cacheKey)
        {
            if (!String.IsNullOrEmpty(cacheKey))
            {
                Cache objCache = HttpRuntime.Cache;
                return objCache[cacheKey];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取当前应用程序指定cacheKey的Cache对象值
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="cacheKey">索引键值</param>
        /// <returns>返回对象类型</returns>
        public static T GetCache<T>(String cacheKey)
        {
            object obj = GetCache(cacheKey);
            return obj == null ? default(T) : (T)obj;
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache对象值
        /// </summary>
        /// <param name="cacheKey">索引键值</param>
        /// <param name="obj">c</param>
        public static void SetCache(String cacheKey, Object obj)
        {
            Cache objCache = HttpRuntime.Cache;
            objCache.Insert(cacheKey, obj);
        }

        /// <summary>
        /// 创建缓存依赖项
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="obj"></param>
        /// <param name="fileName"></param>
        public static void SetCache(String cacheKey, Object obj, String fileName)
        {
            CacheDependency cdep = new CacheDependency(fileName);
            Cache objCache = HttpRuntime.Cache;
            objCache.Insert(cacheKey, obj, cdep);
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache对象值
        /// </summary>
        /// <param name="cacheKey">索引键值</param>
        /// <param name="obj">索引键值</param>
        /// <param name="absoluteExpiration">绝对过期时间</param>
        /// <param name="slidingExpiration">最后一次访问所插入对象时与该对象过期时之间的时间间隔</param>
        public static void SetCache(String cacheKey, Object obj, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            Cache objCache = HttpRuntime.Cache;
            objCache.Insert(cacheKey, obj, null, absoluteExpiration, slidingExpiration);
        }

        /// <summary>
        /// 创建缓存项过期
        /// </summary>
        /// <param name="cacheKey">索引键值</param>
        /// <param name="obj">索引对象</param>
        /// <param name="expires">过期时间</param>
        public static void SetCache(String cacheKey, Object obj, Int32 expires)
        {
            Cache objCache = HttpRuntime.Cache;
            objCache.Insert(
            cacheKey,
            obj,
            null,
            Cache.NoAbsoluteExpiration,//从不过期
            new TimeSpan(0, expires, 0)
            );
        }

        /// <summary>
        /// 设置依赖的方式缓存数据
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="obj"></param>
        /// <param name="cdep"></param>
        public static void SetCache(String cacheKey, Object obj, CacheDependency cdep)
        {
            Cache objCache = HttpRuntime.Cache;
            objCache.Insert(
                cacheKey,
                obj,
                cdep,
                Cache.NoAbsoluteExpiration,//从不过期
                Cache.NoSlidingExpiration,//禁用可调过期
                CacheItemPriority.Default,
                null
                );
        }
        //==========调用方式Start===============
        //    文件依赖使用说明
        //    System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency("C:\\test.txt");
        //   SetCache(CacheKey, objModel, dep);//写入缓存
        //
        //  数据库依赖项使用说明
        //   System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency("codematic", "P_Product");
        //  SetCache(CacheKey, objModel, dep);//写入缓存
        //
        //==========调用方式end===============
        #endregion
    }
}

调用方式

 

        //设置缓存的键值
            CacheManager.CacheKey = "NavMenue";
            //获取当前键值下的缓存数据
            Object cacheModel = CacheManager.GetCache(CacheManager.CacheKey);
            //如果缓存中不存在当前键值对象
            if (cacheModel == null)
            {
                //将数据存储到缓存中
                cacheModel = bsnManage.Obj_List(navPosition);
                if (cacheModel != null)
                {
                    //依赖数据库的sc_navigation 表变化来更新
                    SqlCacheDependency scdep = new SqlCacheDependency(CacheManager.SqlDepName, "sc_navigation");

                    //写入缓存
                    CacheManager.SetCache(CacheManager.CacheKey, cacheModel, scdep);
                }
                return (DataTable)cacheModel;
            }
            else
                return (DataTable)CacheManager.GetCache(CacheManager.CacheKey);

 

Asp.Net Cache辅助类,布布扣,bubuko.com

Asp.Net Cache辅助类

标签:style   blog   http   color   使用   文件   

原文地址:http://www.cnblogs.com/acoll/p/3840277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!