mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-08-23 22:11:35 +08:00
加入MemoryCache相关代码
This commit is contained in:
parent
ece6a57d6e
commit
981b595bcc
48
Infrastructure/Cache/ICache.cs
Normal file
48
Infrastructure/Cache/ICache.cs
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
93
Infrastructure/Cache/RuntimeMemoryCache.cs
Normal file
93
Infrastructure/Cache/RuntimeMemoryCache.cs
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -58,6 +58,7 @@
|
|||||||
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
|
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
|
||||||
<Private>True</Private>
|
<Private>True</Private>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="System.Runtime.Caching" />
|
||||||
<Reference Include="System.Web" />
|
<Reference Include="System.Web" />
|
||||||
<Reference Include="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
<Reference Include="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||||
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll</HintPath>
|
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll</HintPath>
|
||||||
@ -77,6 +78,8 @@
|
|||||||
<Compile Include="AutoMapperExt.cs" />
|
<Compile Include="AutoMapperExt.cs" />
|
||||||
<Compile Include="Cache\CacheContext.cs" />
|
<Compile Include="Cache\CacheContext.cs" />
|
||||||
<Compile Include="Cache\EnyimMemcachedContext.cs" />
|
<Compile Include="Cache\EnyimMemcachedContext.cs" />
|
||||||
|
<Compile Include="Cache\ICache.cs" />
|
||||||
|
<Compile Include="Cache\RuntimeMemoryCache.cs" />
|
||||||
<Compile Include="Cache\SessionContext.cs" />
|
<Compile Include="Cache\SessionContext.cs" />
|
||||||
<Compile Include="CookieHelper.cs" />
|
<Compile Include="CookieHelper.cs" />
|
||||||
<Compile Include="DynamicLinq.cs" />
|
<Compile Include="DynamicLinq.cs" />
|
||||||
@ -98,7 +101,9 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup />
|
<ItemGroup>
|
||||||
|
<Folder Include="MVC\" />
|
||||||
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
<!-- <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
|
<!-- <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
|
||||||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||||
|
Loading…
Reference in New Issue
Block a user