mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-18 09:44:28 +08:00
优化SSO登录
This commit is contained in:
44
Infrastructure/Cache/CacheContext.cs
Normal file
44
Infrastructure/Cache/CacheContext.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : Helper
|
||||
// Author : yubaolee
|
||||
// Created : 12-16-2016
|
||||
//
|
||||
// Last Modified By : yubaolee
|
||||
// Last Modified On : 12-21-2016
|
||||
// 使用微软默认带超时的Cache
|
||||
// File: CacheContext.cs
|
||||
// ***********************************************************************
|
||||
|
||||
using System;
|
||||
using System.Web;
|
||||
|
||||
namespace Infrastructure.Cache
|
||||
{
|
||||
public class CacheContext : ICacheContext
|
||||
{
|
||||
private readonly System.Web.Caching.Cache _objCache = HttpRuntime.Cache;
|
||||
public override T Get<T>(string key)
|
||||
{
|
||||
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
|
||||
return (T) objCache[key];
|
||||
}
|
||||
|
||||
public override bool Set<T>(string key, T t, DateTime expire)
|
||||
{
|
||||
var obj = Get<T>(key);
|
||||
if (obj != null)
|
||||
{
|
||||
Remove(key);
|
||||
}
|
||||
|
||||
_objCache.Insert(key, t, null, expire, System.Web.Caching.Cache.NoSlidingExpiration);
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool Remove(string key)
|
||||
{
|
||||
_objCache.Remove(key);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,16 +0,0 @@
|
||||
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; }
|
||||
}
|
||||
}
|
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Helper.Cache
|
||||
namespace Infrastructure.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存工厂
|
||||
|
@@ -10,40 +10,29 @@
|
||||
// ***********************************************************************
|
||||
|
||||
|
||||
using System;
|
||||
using Enyim.Caching;
|
||||
using Enyim.Caching.Memcached;
|
||||
|
||||
namespace Helper.Cache
|
||||
namespace Infrastructure.Cache
|
||||
{
|
||||
public sealed class EnyimMemcachedContext : ICacheContext
|
||||
{
|
||||
private readonly MemcachedClient _memcachedClient = new MemcachedClient("memcached");
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
}
|
||||
private static readonly MemcachedClient _memcachedClient = new MemcachedClient();
|
||||
|
||||
public override T Get<T>(string key)
|
||||
{
|
||||
return _memcachedClient.Get<T>(key);
|
||||
}
|
||||
|
||||
public override bool Set<T>(string key, T t)
|
||||
public override bool Set<T>(string key, T t, DateTime expire)
|
||||
{
|
||||
return _memcachedClient.Store(StoreMode.Set, key, t);
|
||||
return _memcachedClient.Store(StoreMode.Set, key, t, expire);
|
||||
}
|
||||
|
||||
public override bool Remove(string key)
|
||||
{
|
||||
return _memcachedClient.Remove(key);
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
if (_memcachedClient != null)
|
||||
{
|
||||
_memcachedClient.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,65 +0,0 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : Helper
|
||||
// Author : Administrator
|
||||
// Created : 09-21-2016
|
||||
//
|
||||
// Last Modified By : Administrator
|
||||
// Last Modified On : 11-09-2016
|
||||
// Contact :
|
||||
// File: HttpApplicationContext.cs
|
||||
// ***********************************************************************
|
||||
|
||||
|
||||
using System;
|
||||
using System.Web;
|
||||
|
||||
namespace Helper.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 基于HttpApplication的存储
|
||||
/// <para>李玉宝新增于2016-11-09 9:30:51</para>
|
||||
/// </summary>
|
||||
public sealed class HttpApplicationContext : ICacheContext
|
||||
{
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
}
|
||||
|
||||
public override T Get<T>(string key)
|
||||
{
|
||||
return (T) HttpContext.Current.Application[key];
|
||||
}
|
||||
|
||||
public override bool Set<T>(string key, T t)
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpContext.Current.Application[key] = t;
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool Remove(string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpContext.Current.Application[key] = null;
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,24 +1,19 @@
|
||||
using System;
|
||||
|
||||
namespace Helper.Cache
|
||||
namespace Infrastructure.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存接口
|
||||
/// </summary>
|
||||
public abstract class ICacheContext : IDisposable
|
||||
public abstract class ICacheContext
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化缓存组件
|
||||
/// </summary>
|
||||
public abstract void Init();
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存项
|
||||
/// </summary>
|
||||
/// <typeparam name="T">缓存对象类型</typeparam>
|
||||
/// <param name="key">键</param>
|
||||
/// <returns>缓存对象</returns>
|
||||
public abstract T Get<T>(string key) where T : class;
|
||||
public abstract T Get<T>(string key) ;
|
||||
|
||||
/// <summary>
|
||||
/// 设置缓存项
|
||||
@@ -27,7 +22,7 @@ namespace Helper.Cache
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="t">缓存对象</param>
|
||||
/// <returns>true成功,false失败</returns>
|
||||
public abstract bool Set<T>(string key, T t) where T : class;
|
||||
public abstract bool Set<T>(string key, T t, DateTime expire);
|
||||
|
||||
/// <summary>
|
||||
/// 移除一个缓存项
|
||||
@@ -36,9 +31,5 @@ namespace Helper.Cache
|
||||
/// <returns>true成功,false失败</returns>
|
||||
public abstract bool Remove(string key);
|
||||
|
||||
/// <summary>
|
||||
/// 释放缓存组件
|
||||
/// </summary>
|
||||
public abstract void Dispose();
|
||||
}
|
||||
}
|
@@ -1,39 +1,34 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : OpenAuth.WebApi
|
||||
// Author : yubaolee
|
||||
// Created : 07-11-2016
|
||||
// Assembly : Helper
|
||||
// Author : Administrator
|
||||
// Created : 12-21-2016
|
||||
//
|
||||
// Last Modified By : yubaolee
|
||||
// Last Modified On : 07-11-2016
|
||||
// Last Modified By : Administrator
|
||||
// Last Modified On : 12-22-2016
|
||||
// Contact :
|
||||
// File: CacheObjService.cs
|
||||
// File: ObjCacheProvider.cs
|
||||
// ***********************************************************************
|
||||
|
||||
|
||||
using System;
|
||||
|
||||
namespace Helper.Cache
|
||||
namespace Infrastructure.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 带超时结构的缓存
|
||||
/// 缓存工厂实现
|
||||
/// 这样做是方便换其他的缓存时(如memcachedContext)只换这一个地方即可
|
||||
/// </summary>
|
||||
public class ObjCacheProvider<T> : CacheProvider
|
||||
{
|
||||
public ObjCacheProvider()
|
||||
{
|
||||
SetCacheInstance(new HttpApplicationContext());
|
||||
SetCacheInstance(new CacheContext());
|
||||
}
|
||||
|
||||
public bool Create(string key, T val)
|
||||
public bool Create(string key, T val, DateTime expire)
|
||||
{
|
||||
var cacheobj = new CacheObj<T>
|
||||
{
|
||||
key = key,
|
||||
InvalidTime = DateTime.Now.AddMinutes(5),
|
||||
CreateTime = DateTime.Now,
|
||||
Obj = val
|
||||
};
|
||||
//设置缓存
|
||||
return CacheContext.Set(key, cacheobj);
|
||||
return CacheContext.Set<T>(key, val, expire);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -43,18 +38,7 @@ namespace Helper.Cache
|
||||
/// <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);
|
||||
return CacheContext.Get<T>(key);
|
||||
}
|
||||
|
||||
public void Remove(string key)
|
||||
|
@@ -80,10 +80,9 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AutoMapperExt.cs" />
|
||||
<Compile Include="Cache\CacheObj.cs" />
|
||||
<Compile Include="Cache\CacheContext.cs" />
|
||||
<Compile Include="Cache\CacheProvider.cs" />
|
||||
<Compile Include="Cache\EnyimMemcachedContext.cs" />
|
||||
<Compile Include="Cache\HttpApplicationContext.cs" />
|
||||
<Compile Include="Cache\ICacheContext.cs" />
|
||||
<Compile Include="Cache\ObjCacheProvider.cs" />
|
||||
<Compile Include="CookieHelper.cs" />
|
||||
|
Reference in New Issue
Block a user