OpenAuth.Net/Infrastructure/Cache/CacheProvider.cs

68 lines
1.9 KiB
C#
Raw Normal View History

2016-07-08 11:28:38 +08:00
using System;
using System.Globalization;
2016-12-27 11:25:51 +08:00
namespace Infrastructure.Cache
2016-07-08 11:28:38 +08:00
{
2016-11-17 19:48:12 +08:00
/// <summary>
/// 缓存工厂
/// <para>李玉宝新增于2016-11-09 9:42:52</para>
/// </summary>
public abstract class CacheProvider : IDisposable
2016-07-08 11:28:38 +08:00
{
/// <summary>
/// 缓存组件
/// </summary>
2016-11-17 19:48:12 +08:00
public ICacheContext CacheContext { get; private set; }
2016-07-08 11:28:38 +08:00
/// <summary>
/// 动态设置缓存对象的新实例
/// </summary>
/// <param name="cacheContext">缓存实例对象</param>
2016-11-17 19:48:12 +08:00
public void SetCacheInstance(ICacheContext cacheContext)
2016-07-08 11:28:38 +08:00
{
//先释放现有的缓存组件
if (CacheContext != null)
{
CacheContext = null;
}
//初始化缓存组件新的实例
CacheContext = cacheContext;
}
public void SetCacheInstance(Type cacheContextType)
{
if (cacheContextType == null)
{
throw new ArgumentNullException("cacheContextType");
}
2016-11-17 19:48:12 +08:00
if (!typeof(ICacheContext).IsAssignableFrom(cacheContextType))
2016-07-08 11:28:38 +08:00
{
throw new ArgumentException(
string.Format(CultureInfo.CurrentCulture, "该类型 {0} 必须继承自抽象类CacheContext", cacheContextType),
"cacheContextType");
}
try
{
2016-11-17 19:48:12 +08:00
CacheContext = Activator.CreateInstance(cacheContextType) as ICacheContext;
2016-07-08 11:28:38 +08:00
}
catch (Exception ex)
{
throw new InvalidOperationException(
String.Format(
CultureInfo.CurrentCulture,
"创建抽象类 CacheContext 的实例 {0} 失败",
cacheContextType),
ex);
}
}
public void Dispose()
{
}
}
}