mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-18 17:48:01 +08:00
优化sso
This commit is contained in:
16
Infrastructure/Cache/CacheObj.cs
Normal file
16
Infrastructure/Cache/CacheObj.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
|
||||
namespace Helper.Cache
|
||||
{
|
||||
[Serializable]
|
||||
public class CacheObj<T>
|
||||
{
|
||||
public string key { get; set; }
|
||||
|
||||
public T Obj { get; set; }
|
||||
|
||||
public DateTime InvalidTime { get; set; }
|
||||
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
}
|
67
Infrastructure/Cache/CacheProvider.cs
Normal file
67
Infrastructure/Cache/CacheProvider.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Helper.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存工厂
|
||||
/// <para>李玉宝新增于2016-11-09 9:42:52</para>
|
||||
/// </summary>
|
||||
public abstract class CacheProvider : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存组件
|
||||
/// </summary>
|
||||
public ICacheContext CacheContext { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 动态设置缓存对象的新实例
|
||||
/// </summary>
|
||||
/// <param name="cacheContext">缓存实例对象</param>
|
||||
public void SetCacheInstance(ICacheContext cacheContext)
|
||||
{
|
||||
//先释放现有的缓存组件
|
||||
if (CacheContext != null)
|
||||
{
|
||||
CacheContext = null;
|
||||
}
|
||||
|
||||
//初始化缓存组件新的实例
|
||||
CacheContext = cacheContext;
|
||||
}
|
||||
|
||||
public void SetCacheInstance(Type cacheContextType)
|
||||
{
|
||||
if (cacheContextType == null)
|
||||
{
|
||||
throw new ArgumentNullException("cacheContextType");
|
||||
}
|
||||
|
||||
if (!typeof(ICacheContext).IsAssignableFrom(cacheContextType))
|
||||
{
|
||||
throw new ArgumentException(
|
||||
string.Format(CultureInfo.CurrentCulture, "该类型 {0} 必须继承自抽象类CacheContext", cacheContextType),
|
||||
"cacheContextType");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
CacheContext = Activator.CreateInstance(cacheContextType) as ICacheContext;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
String.Format(
|
||||
CultureInfo.CurrentCulture,
|
||||
"创建抽象类 CacheContext 的实例 {0} 失败",
|
||||
cacheContextType),
|
||||
ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -13,9 +13,9 @@
|
||||
using Enyim.Caching;
|
||||
using Enyim.Caching.Memcached;
|
||||
|
||||
namespace Infrastructure.Cache
|
||||
namespace Helper.Cache
|
||||
{
|
||||
public sealed class EnyimMemcachedContext : CacheContext
|
||||
public sealed class EnyimMemcachedContext : ICacheContext
|
||||
{
|
||||
private readonly MemcachedClient _memcachedClient = new MemcachedClient("memcached");
|
||||
|
||||
|
@@ -1,21 +1,25 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : Infrastructure
|
||||
// Author : yubaolee
|
||||
// Created : 06-21-2016
|
||||
// Assembly : Helper
|
||||
// Author : Administrator
|
||||
// Created : 09-21-2016
|
||||
//
|
||||
// Last Modified By : yubaolee
|
||||
// Last Modified On : 06-21-2016
|
||||
// Last Modified By : Administrator
|
||||
// Last Modified On : 11-09-2016
|
||||
// Contact :
|
||||
// File: EnyimMemcachedContext.cs
|
||||
// File: HttpApplicationContext.cs
|
||||
// ***********************************************************************
|
||||
|
||||
|
||||
using System;
|
||||
using System.Web;
|
||||
|
||||
namespace Infrastructure.Cache
|
||||
namespace Helper.Cache
|
||||
{
|
||||
public sealed class SessionContext : CacheContext
|
||||
/// <summary>
|
||||
/// 基于HttpApplication的存储
|
||||
/// <para>李玉宝新增于2016-11-09 9:30:51</para>
|
||||
/// </summary>
|
||||
public sealed class HttpApplicationContext : ICacheContext
|
||||
{
|
||||
|
||||
public override void Init()
|
@@ -1,48 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Infrastructure.Cache
|
||||
{
|
||||
public interface ICache
|
||||
{
|
||||
/// <summary>
|
||||
/// 加入缓存项
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项标识</param>
|
||||
/// <param name="value">缓存项</param>
|
||||
/// <param name="timeSpan">缓存失效时间</param>
|
||||
void Add(string key, object value, TimeSpan timeSpan);
|
||||
/// <summary>
|
||||
/// 加入依赖物理文件的缓存项
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项标识</param>
|
||||
/// <param name="value">缓存项</param>
|
||||
/// <param name="fullFileNameOfFileDependency">依赖的文件全路径</param>
|
||||
void AddWithFileDependency(string key, object value, string fullFileNameOfFileDependency);
|
||||
/// <summary>
|
||||
/// 获取缓存项
|
||||
/// </summary>
|
||||
/// <param name="cacheKey"></param>
|
||||
/// <returns></returns>
|
||||
object Get(string cacheKey);
|
||||
T Get<T>(string cacheKey) where T : class;
|
||||
void Remove(string cacheKey);
|
||||
/// <summary>
|
||||
/// 如果不存在缓存项则添加,否则更新(相对过期)
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项标识</param>
|
||||
/// <param name="value">缓存项</param>
|
||||
/// <param name="timeSpan">缓存失效时间</param>
|
||||
void Set(string key, object value, TimeSpan timeSpan);
|
||||
/// <summary>
|
||||
/// 设置绝对过期时间
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项标识</param>
|
||||
/// <param name="value">缓存项</param>
|
||||
/// <param name="timeSpan">缓存失效时间</param>
|
||||
void SetAbsoluteExpiration(string key, object value, TimeSpan timeSpan);
|
||||
}
|
||||
}
|
@@ -1,11 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace Infrastructure.Cache
|
||||
namespace Helper.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 策略模式缓存组件,可实现动态插拔
|
||||
/// 缓存接口
|
||||
/// </summary>
|
||||
public abstract class CacheContext : IDisposable
|
||||
public abstract class ICacheContext : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化缓存组件
|
65
Infrastructure/Cache/ObjCacheProvider.cs
Normal file
65
Infrastructure/Cache/ObjCacheProvider.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : OpenAuth.WebApi
|
||||
// Author : yubaolee
|
||||
// Created : 07-11-2016
|
||||
//
|
||||
// Last Modified By : yubaolee
|
||||
// Last Modified On : 07-11-2016
|
||||
// Contact :
|
||||
// File: CacheObjService.cs
|
||||
// ***********************************************************************
|
||||
|
||||
using System;
|
||||
|
||||
namespace Helper.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 带超时结构的缓存
|
||||
/// </summary>
|
||||
public class ObjCacheProvider<T> : CacheProvider
|
||||
{
|
||||
public ObjCacheProvider()
|
||||
{
|
||||
SetCacheInstance(new HttpApplicationContext());
|
||||
}
|
||||
|
||||
public bool Create(string key, T val)
|
||||
{
|
||||
var cacheobj = new CacheObj<T>
|
||||
{
|
||||
key = key,
|
||||
InvalidTime = DateTime.Now.AddMinutes(5),
|
||||
CreateTime = DateTime.Now,
|
||||
Obj = val
|
||||
};
|
||||
//设置缓存
|
||||
return CacheContext.Set(key, cacheobj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据失效时间获取缓存
|
||||
/// <para>李玉宝于2016-11-08 16:54:04</para>
|
||||
/// </summary>
|
||||
/// <param name="key">The key.</param>
|
||||
public T GetCache(string key)
|
||||
{
|
||||
var cache = CacheContext.Get<CacheObj<T>>(key);
|
||||
if (cache == null) return default(T);
|
||||
|
||||
if (cache.InvalidTime > DateTime.Now)
|
||||
{
|
||||
return cache.Obj;
|
||||
}
|
||||
|
||||
//移除无效Session缓存
|
||||
Remove(key);
|
||||
|
||||
return default(T);
|
||||
}
|
||||
|
||||
public void Remove(string key)
|
||||
{
|
||||
CacheContext.Remove(key);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,93 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Caching;
|
||||
namespace Infrastructure.Cache
|
||||
{
|
||||
public class RuntimeMemoryCache : ICache
|
||||
{
|
||||
private readonly MemoryCache memoryCache = MemoryCache.Default;
|
||||
/// <summary>
|
||||
/// 加入缓存项(绝对过期时间)
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项标识</param>
|
||||
/// <param name="value">缓存项</param>
|
||||
/// <param name="timeSpan">缓存失效时间</param>
|
||||
public void Add(string key, object value, TimeSpan timeSpan)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(key) && (value != null))
|
||||
{
|
||||
CacheItemPolicy cip = new CacheItemPolicy()
|
||||
{
|
||||
AbsoluteExpiration = DateTime.Now.Add(timeSpan)
|
||||
};
|
||||
this.memoryCache.Add(key, value, cip, null);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 加入依赖物理文件的缓存项
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项标识</param>
|
||||
/// <param name="value">缓存项</param>
|
||||
/// <param name="fullFileNameOfFileDependency">依赖的文件全路径</param>
|
||||
public void AddWithFileDependency(string key, object value, string fullFileNameOfFileDependency)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(key) && (value != null))
|
||||
{
|
||||
CacheItemPolicy policy = new CacheItemPolicy
|
||||
{
|
||||
AbsoluteExpiration = DateTimeOffset.Now.AddMonths(1)
|
||||
};
|
||||
policy.ChangeMonitors.Add(new HostFileChangeMonitor(new List<string> { fullFileNameOfFileDependency }));
|
||||
this.memoryCache.Add(key, value, policy, null);
|
||||
}
|
||||
}
|
||||
|
||||
public object Get(string cacheKey)
|
||||
{
|
||||
return this.memoryCache[cacheKey];
|
||||
}
|
||||
|
||||
public T Get<T>(string cacheKey) where T : class
|
||||
{
|
||||
object obj = this.Get(cacheKey);
|
||||
if (obj != null)
|
||||
{
|
||||
return (obj as T);
|
||||
}
|
||||
return default(T);
|
||||
}
|
||||
|
||||
public void Remove(string cacheKey)
|
||||
{
|
||||
this.memoryCache.Remove(cacheKey, null);
|
||||
}
|
||||
/// <summary>
|
||||
/// 如果不存在缓存项则添加,否则更新(相对过期)
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项标识</param>
|
||||
/// <param name="value">缓存项</param>
|
||||
/// <param name="timeSpan">缓存失效时间</param>
|
||||
public void Set(string key, object value, TimeSpan timeSpan)
|
||||
{
|
||||
CacheItemPolicy cip = new CacheItemPolicy()
|
||||
{
|
||||
SlidingExpiration = timeSpan,
|
||||
};
|
||||
this.memoryCache.Set(key, value, cip, null);
|
||||
}
|
||||
/// <summary>
|
||||
/// 设置绝对过期时间
|
||||
/// </summary>
|
||||
/// <param name="key">缓存项标识</param>
|
||||
/// <param name="value">缓存项</param>
|
||||
/// <param name="timeSpan">缓存失效时间</param>
|
||||
public void SetAbsoluteExpiration(string key, object value, TimeSpan timeSpan)
|
||||
{
|
||||
CacheItemPolicy cip = new CacheItemPolicy()
|
||||
{
|
||||
AbsoluteExpiration = DateTime.Now.Add(timeSpan),
|
||||
};
|
||||
this.memoryCache.Set(key, value, cip, null);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user