DOGNET

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace DogNet.Common.CacheService
{
    public class DataAccessCached
    {
        private static object markAllEmployee = new object();
        /// <summary>
        /// 得到所有的员工(带缓存)
        /// </summary>
        /// <returns></returns>
        public static IList<object> GetAllEmployees()
        {
            string key = "AllEmployee";
            int cacheTime = Convert.ToInt32(ConfigurationManager.AppSettings["secondOfoutTime"]);
            IList<object> listResult = HttpRuntime.Cache[key] as List<object>;
            if (listResult == null)
            {
                lock (markAllEmployee)
                {
                    if (listResult == null)
                    {
                        //从DB或其它持久访问层得到需要缓存的数据
                        //JDHrmDB hrDB = new JDHrmDB();
                        //listResult = hrDB.GetAllEmployee();
                        HttpRuntime.Cache.Add(key, listResult, null, DateTime.Now.AddSeconds(cacheTime)
                            , Cache.NoSlidingExpiration, CacheItemPriority.High, new CacheItemRemovedCallback(CacheItemRemovedCallback));
                    }
                }
            }
            return listResult;
        }

        static void CacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason)
        {
            if (key == "AllEmployee")
            {
                System.Threading.Thread th = new System.Threading.Thread(ReLoadAllEmployees);
                th.Start();
            }
        }

        public static void ReLoadAllEmployees()
        {
            try
            {
                GetAllEmployees();
            }
            catch (Exception ex)
            {
            }
        }
    }
}
posted on 2010-05-18 11:07  DOGNET  阅读(309)  评论(0编辑  收藏  举报