我的缓存实例—工作记录

      缓存使用记录。

      在平时的开发中,我想缓存是极其重要的,他能很大程度上提高程序的效率,所以这里我记录下来我在盛大使用缓存的情况。

      1、这是缓存的原始类Webcache,其中有缓存的时间设置

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections;

namespace Shanda.Utility
{
    
/// <summary>
    
/// WebCache
    
/// </summary>
    public static class WebCache
    {
        
/// <summary>
        
/// 一般配置Chche过期时间,秒钟计
        
/// </summary>
        
/// <value>The common cache time out.</value>
        public static int CommonCacheTimeOut
        {
            
get
            {
                
if (_CommonCacheTimeOut == null)
                {
                    
string settings = ConfigurationManager.AppSettings["CommonCacheTimeOut"];
                    
int timeOut;
                    
if (!int.TryParse(settings, out timeOut))
                        timeOut 
= 3600;   //默认1小时过期
                    _CommonCacheTimeOut = timeOut;
                }

                
return _CommonCacheTimeOut.Value;
            }
        }
        
private static int? _CommonCacheTimeOut;

        
/// <summary>
        
/// 查询配置Chche过期时间,秒钟计
        
/// </summary>
        
/// <value>The query cache time out.</value>
        public static int QueryCacheTimeOut
        {
            
get
            {
                
if (_QueryCacheTimeOut == null)
                {
                    
string settings = ConfigurationManager.AppSettings["QueryCacheTimeOut"];
                    
int timeOut;
                    
if (!int.TryParse(settings, out timeOut))
                        timeOut 
= 10;   //默认10秒钟过期
                    _QueryCacheTimeOut = timeOut;
                }

                
return _QueryCacheTimeOut.Value;
            }
        }
        
private static int? _QueryCacheTimeOut;

        
/// <summary>
        
/// Cache插入锁
        
/// </summary>
        private static object CacheLocker = new object();

        
/// <summary>
        
/// Sql数据库缓存依赖项
        
/// </summary>
        private static List<string> SqlCacheDependencyItems = new List<string>();

        
/// <summary>
        
/// 获取缓存的对象。当没有缓存的时候,自动创建对象并进行缓存。只支持引用类型的缓存。
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="key">The key.</param>
        
/// <param name="timeOutSeconds">单位:秒</param>
        
/// <param name="onCreateInstance">The on create instance.</param>
        
/// <returns></returns>
        public static T GetCachedObject<T>(string key, int timeOutSeconds, Func<T> onCreateInstance)
        {
            
return GetCachedObject<T>(key, null, timeOutSeconds, onCreateInstance);
        }

        
/// <summary>
        
/// 获取缓存的对象。当没有缓存的时候,自动创建对象并进行缓存。只支持引用类型的缓存。
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="key">The key.</param>
        
/// <param name="onCreateInstance">The on create instance.</param>
        
/// <returns></returns>
        public static T GetCachedObject<T>(string key, Func<T> onCreateInstance)
        {
            
return GetCachedObject<T>(key, null, CommonCacheTimeOut, onCreateInstance);
        }

        
/// <summary>
        
/// 获取二级缓存的对象。当没有缓存的时候,自动创建对象并进行缓存。只支持引用类型的缓存。
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="key1">第一级缓存健</param>
        
/// <param name="key2">第二级缓存健</param>
        
/// <param name="onCreateInstance"></param>
        
/// <returns></returns>
        public static T GetCachedObject<T>(string key1, string key2, Func<T> onCreateInstance) where T : classnew()
        {
            Dictionary
<string, T> dictionary = GetCachedObject<Dictionary<string, T>>(key1, null, CommonCacheTimeOut,
                
delegate
                {
                    
return new Dictionary<string, T>();
                });

            
return Singleton<T>.GetInstance(dictionary, key2, onCreateInstance);
        }

        
/// <summary>
        
/// 获取缓存的对象。当没有缓存的时候,自动创建对象并进行缓存。只支持引用类型的缓存。
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="key"></param>
        
/// <param name="databaseEntryName"></param>
        
/// <param name="tableName"></param>
        
/// <param name="onCreateInstance"></param>
        
/// <returns></returns>
        public static T GetCachedObject<T>(string key, string databaseEntryName, string tableName, Func<T> onCreateInstance)
        {
            
if (!SqlCacheDependencyItems.Contains(databaseEntryName))
            {
                System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(GetConnectionString(databaseEntryName));
                SqlCacheDependencyItems.Add(databaseEntryName);
                SqlCacheDependencyItems.Sort();
            }
            
if (!SqlCacheDependencyItems.Contains(databaseEntryName + '#' + tableName))
            {
                System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(GetConnectionString(databaseEntryName), tableName);
                SqlCacheDependencyItems.Add(databaseEntryName 
+ '#' + tableName);
                SqlCacheDependencyItems.Sort();
            }

            System.Web.Caching.SqlCacheDependency dependency 
= new System.Web.Caching.SqlCacheDependency(databaseEntryName, tableName);
            
return GetCachedObject<T>(key, dependency, 0, onCreateInstance);
        }

        
/// <summary>
        
/// 获取缓存的查询。当没有缓存的时候,自动创建对象并进行缓存。只支持引用类型的缓存。
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="key">The key.</param>
        
/// <param name="onCreateInstance">The on create instance.</param>
        
/// <returns></returns>
        public static T GetCachedQuery<T>(string key, Func<T> onCreateInstance)
        {
            
return GetCachedObject<T>(key, null, QueryCacheTimeOut, onCreateInstance);
        }

        
/// <summary>
        
/// 移除缓存的对象
        
/// </summary>
        
/// <param name="key">The key.</param>
        
/// <returns></returns>
        public static object Remove(string key)
        {
            
//当前Cache对象
            System.Web.Caching.Cache webCache = System.Web.HttpRuntime.Cache;

            
lock (CacheLocker)
            {
                
return webCache.Remove(key);
            }
        }

        
/// <summary>
        
/// 清除所有缓存
        
/// </summary>
        public static void RemoveAll()
        {
            
//当前Cache对象           
            var cache = System.Web.HttpRuntime.Cache;
            
foreach (DictionaryEntry de in cache.Cast<DictionaryEntry>())
            {
                
string key = de.Key as string;
                cache.Remove(key);
            }
        }

        
/// <summary>
        
/// 获取缓存的对象。当没有缓存的时候,自动创建对象并进行缓存。只支持引用类型的缓存。
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="key"></param>
        
/// <param name="dependency"></param>
        
/// <param name="timeOutSeconds">单位:秒</param>
        
/// <param name="onCreateInstance"></param>
        
/// <returns></returns>
        private static T GetCachedObject<T>(string key, System.Web.Caching.CacheDependency dependency, int timeOutSeconds, Func<T> onCreateInstance)
        {
            
if (timeOutSeconds > 0 || dependency != null)
            {
                
//当前Cache对象
                System.Web.Caching.Cache webCache = System.Web.HttpRuntime.Cache;
                
if (webCache == null)
                    
return onCreateInstance();

                
//获取缓存的对象
                T cachedObject = (T)webCache.Get(key);

                
if (cachedObject == null)
                {
                    
//加锁确保多线程安全
                    lock (CacheLocker)
                    {
                        cachedObject 
= (T)webCache.Get(key);
                        
if (cachedObject == null)
                        {
                            
//创建新的对象
                            cachedObject = onCreateInstance();
                            
//将创建的对象进行缓存
                            if (cachedObject != null)
                                webCache.Insert(key, cachedObject, dependency, System.Web.Caching.Cache.NoAbsoluteExpiration, 
new TimeSpan(00, timeOutSeconds));
                        }
                    }
                }
                
return cachedObject;
            }
            
else
            {
                
//不设置缓存,则创建新的对象
                return onCreateInstance();
            }
        }

        
/// <summary>
        
/// 获取数据库连接字符串
        
/// </summary>
        
/// <param name="connectionEntryName"></param>
        
/// <returns></returns>
        private static string GetConnectionString(string connectionEntryName)
        {
            
string connString = System.Configuration.ConfigurationManager.AppSettings[connectionEntryName];
            
if (!string.IsNullOrEmpty(connString))
            {
                
if (System.Configuration.ConfigurationManager.ConnectionStrings[connString] != null)
                    
return System.Configuration.ConfigurationManager.ConnectionStrings[connString].ConnectionString;
                
else
                    
return connString;
            }
            
else
            {
                
return System.Configuration.ConfigurationManager.ConnectionStrings[connectionEntryName].ConnectionString;
            }
        }
    }
}

 

        2、缓存的使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shanda.Utility;
using Shanda.CloudBar.DataAccess;
using Shanda.CloudBar.Business.myHome;
using Shanda.CloudBar.Business.Entitys;
using Shanda.CloudBar.Business.JsonHelper;

namespace Shanda.CloudBar.Business
{
    
public class CacheBusiness
    {
        
/// <summary>
        
/// CacheBusiness的单例
        
/// </summary>
        public static CacheBusiness Instance { get { return Singleton<CacheBusiness>.GetInstance(); } }

        
/// <summary>
        
/// 获取变量配置
        
/// </summary>
        
/// <param name="variableType"></param>
        
/// <returns></returns>
        public Dev_VariableSetting[] GetVariableSetting(string variableType)
        {
            
return WebCache.GetCachedObject<Dev_VariableSetting[]>(typeof(Dev_VariableSetting).FullName + "#" + variableType.ToLower(),
                
delegate()
                {
                    var ctx 
= CloudBarDataContext.Create();
                    ctx.ObjectTrackingEnabled 
= false;
                    
return ctx.Dev_VariableSettings.Where(p => p.VariableType == variableType).OrderBy(p => p.DisplayIndex).ToArray();
                });
        }

        
/// <summary>
        
/// 清除变量配置
        
/// </summary>
        
/// <param name="variableType"></param>
        public void RemoveVariableSetting(string variableType)
        {
            WebCache.Remove(
typeof(Dev_VariableSetting).FullName + "#" + variableType.ToLower());
        }

        
/// <summary>
        
/// 获取缓存的数据表
        
/// </summary>
        
/// <typeparam name="TEntity"></typeparam>
        
/// <returns></returns>
        public TEntity[] GetCachedTable<TEntity>() where TEntity : class
        {
            
return WebCache.GetCachedObject<TEntity[]>(typeof(TEntity).FullName,
                
delegate()
                {
                    var ctx 
= CloudBarDataContext.Create();
                    ctx.ObjectTrackingEnabled 
= false;
                    
return ctx.GetTable<TEntity>().ToArray();
                });
        }

        
/// <summary>
        
/// 清除的数据表
        
/// </summary>
        
/// <typeparam name="TEntity"></typeparam>
        public void RemoveCachedTable<TEntity>() where TEntity : class
        {
            WebCache.Remove(
typeof(TEntity).FullName);
        }
        
//获取个人微博
        public userStatus[] GetLoadUserStatuses(string variableType)
        {
            
return WebCache.GetCachedObject<userStatus[]>(typeof(userStatus).FullName + "#" + variableType.ToLower(),
                
delegate()
                {

                    List
<userStatus> list = HomeBLL.LoadUserStatuses(variableType);
                    
if (list == null)
                        
return null;
                    userStatus[] userarr 
= new userStatus[list.Count];
                    
for (var i = 0; i < list.Count; i++)
                    {
                        userarr[i] 
= list[i];
                    }
                    
return userarr;
                });
        }
   }
}

         以上就是一点心得,记录下来以备今后查阅~~

posted @ 2011-05-04 10:30  弗雷德瑞克杨  阅读(524)  评论(0编辑  收藏  举报