asp.NetにおけるキャッシュクラスDataCache(依存ファイルキャッシュとタイムキャッシュ、または両方)

9175 ワード

更新:2013-12-29
継続的な修正とテストの実行を経て、実際のプロジェクトでよく使われています.
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
    }
}