mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-12-27 06:35:39 +08:00
转移.net core 3.1,为.NET 5做准备
This commit is contained in:
@@ -10,17 +10,22 @@
|
||||
// ***********************************************************************
|
||||
|
||||
using System;
|
||||
using System.Web;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace Infrastructure.Cache
|
||||
{
|
||||
public class CacheContext : ICacheContext
|
||||
{
|
||||
private readonly System.Web.Caching.Cache _objCache = HttpRuntime.Cache;
|
||||
private IMemoryCache _objCache;
|
||||
|
||||
public CacheContext(IMemoryCache objCache)
|
||||
{
|
||||
_objCache = objCache;
|
||||
}
|
||||
|
||||
public override T Get<T>(string key)
|
||||
{
|
||||
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
|
||||
return (T) objCache[key];
|
||||
return _objCache.Get<T>(key);
|
||||
}
|
||||
|
||||
public override bool Set<T>(string key, T t, DateTime expire)
|
||||
@@ -30,8 +35,10 @@ namespace Infrastructure.Cache
|
||||
{
|
||||
Remove(key);
|
||||
}
|
||||
|
||||
_objCache.Insert(key, t, null, expire, System.Web.Caching.Cache.NoSlidingExpiration);
|
||||
|
||||
_objCache.Set(key, t, new MemoryCacheEntryOptions()
|
||||
.SetAbsoluteExpiration(expire)); //绝对过期时间
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Infrastructure.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()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,13 @@
|
||||
//
|
||||
// Last Modified By : yubaolee
|
||||
// Last Modified On : 06-21-2016
|
||||
// Contact :
|
||||
// Contact : Add services.AddEnyimMemcached(...)
|
||||
// and app.UseEnyimMemcached() in Startup.
|
||||
// File: EnyimMemcachedContext.cs
|
||||
// ***********************************************************************
|
||||
|
||||
|
||||
|
||||
using System;
|
||||
using Enyim.Caching;
|
||||
using Enyim.Caching.Memcached;
|
||||
@@ -18,7 +20,12 @@ namespace Infrastructure.Cache
|
||||
{
|
||||
public sealed class EnyimMemcachedContext : ICacheContext
|
||||
{
|
||||
private static readonly MemcachedClient _memcachedClient = new MemcachedClient();
|
||||
private IMemcachedClient _memcachedClient;
|
||||
|
||||
public EnyimMemcachedContext(IMemcachedClient client)
|
||||
{
|
||||
_memcachedClient = client;
|
||||
}
|
||||
|
||||
public override T Get<T>(string key)
|
||||
{
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : Helper
|
||||
// Author : Administrator
|
||||
// Created : 12-21-2016
|
||||
//
|
||||
// Last Modified By : Administrator
|
||||
// Last Modified On : 12-22-2016
|
||||
// Contact :
|
||||
// File: ObjCacheProvider.cs
|
||||
// ***********************************************************************
|
||||
|
||||
|
||||
using System;
|
||||
|
||||
namespace Infrastructure.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存工厂实现
|
||||
/// 这样做是方便换其他的缓存时(如memcachedContext)只换这一个地方即可
|
||||
/// </summary>
|
||||
public class ObjCacheProvider<T> : CacheProvider
|
||||
{
|
||||
public ObjCacheProvider()
|
||||
{
|
||||
SetCacheInstance(new CacheContext());
|
||||
}
|
||||
|
||||
public bool Create(string key, T val, DateTime expire)
|
||||
{
|
||||
//设置缓存
|
||||
return CacheContext.Set<T>(key, val, expire);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据失效时间获取缓存
|
||||
/// <para>李玉宝于2016-11-08 16:54:04</para>
|
||||
/// </summary>
|
||||
/// <param name="key">The key.</param>
|
||||
public T GetCache(string key)
|
||||
{
|
||||
return CacheContext.Get<T>(key);
|
||||
}
|
||||
|
||||
public void Remove(string key)
|
||||
{
|
||||
CacheContext.Remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user