mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 13:06:50 +08:00
-
This commit is contained in:
parent
eb87fbb500
commit
7aadc05977
@ -53,10 +53,6 @@
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SqlSugar.Extensions.Cache\SqlSugar.Extensions.Cache.csproj">
|
||||
<Project>{cdb72abe-0336-4730-a195-abf2611deeaa}</Project>
|
||||
<Name>SqlSugar.Extensions.Cache</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\SqlSugar\SqlSugar.csproj">
|
||||
<Project>{489bb790-226c-4fad-8d1e-51d72a7ff8e5}</Project>
|
||||
<Name>SqlSugar</Name>
|
||||
|
238
Src/Asp.Net/SqlSugar.Extensions.DataCache/HttpRuntimeCache.cs
Normal file
238
Src/Asp.Net/SqlSugar.Extensions.DataCache/HttpRuntimeCache.cs
Normal file
@ -0,0 +1,238 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using System.Web.Caching;
|
||||
|
||||
namespace SqlSugar.Extensions
|
||||
{
|
||||
public class HttpRuntimeCache : ICacheService
|
||||
{
|
||||
public void Add<V>(string key, V value)
|
||||
{
|
||||
HttpRuntimeCacheHelper<V>.GetInstance().Add(key, value);
|
||||
}
|
||||
|
||||
public void Add<V>(string key, V value, int cacheDurationInSeconds)
|
||||
{
|
||||
HttpRuntimeCacheHelper<V>.GetInstance().Add(key, value, cacheDurationInSeconds);
|
||||
}
|
||||
|
||||
public bool ContainsKey<V>(string key)
|
||||
{
|
||||
return HttpRuntimeCacheHelper<V>.GetInstance().ContainsKey(key);
|
||||
}
|
||||
|
||||
public V Get<V>(string key)
|
||||
{
|
||||
return HttpRuntimeCacheHelper<V>.GetInstance().Get(key);
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetAllKey<V>()
|
||||
{
|
||||
return HttpRuntimeCacheHelper<V>.GetInstance().GetAllKey();
|
||||
}
|
||||
|
||||
public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
|
||||
{
|
||||
var cacheManager = HttpRuntimeCacheHelper<V>.GetInstance();
|
||||
if (cacheManager.ContainsKey(cacheKey))
|
||||
{
|
||||
return cacheManager[cacheKey];
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = create();
|
||||
cacheManager.Add(cacheKey, result, cacheDurationInSeconds);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public void Remove<V>(string key)
|
||||
{
|
||||
HttpRuntimeCacheHelper<V>.GetInstance().Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
internal class HttpRuntimeCacheHelper<V>
|
||||
{
|
||||
|
||||
#region 全局变量
|
||||
private static HttpRuntimeCacheHelper<V> _instance = null;
|
||||
private static readonly object _instanceLock = new object();
|
||||
#endregion
|
||||
|
||||
#region 构造函数
|
||||
|
||||
private HttpRuntimeCacheHelper() { }
|
||||
#endregion
|
||||
|
||||
#region 属性
|
||||
/// <summary>
|
||||
///根据key获取value
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public V this[string key]
|
||||
{
|
||||
get { return (V)HttpRuntime.Cache[CreateKey(key)]; }
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 公共函数
|
||||
|
||||
/// <summary>
|
||||
/// key是否存在
|
||||
/// </summary>
|
||||
/// <param name="key">key</param>
|
||||
/// <returns> /// 存在<c>true</c> 不存在<c>false</c>. /// /// </returns>
|
||||
public bool ContainsKey(string key)
|
||||
{
|
||||
return HttpRuntime.Cache[CreateKey(key)] != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存值
|
||||
/// </summary>
|
||||
/// <param name="key">key</param>
|
||||
/// <returns></returns>
|
||||
public V Get(string key)
|
||||
{
|
||||
return (V)HttpRuntime.Cache.Get(CreateKey(key));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取实例 (单例模式)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static HttpRuntimeCacheHelper<V> GetInstance()
|
||||
{
|
||||
if (_instance == null)
|
||||
lock (_instanceLock)
|
||||
if (_instance == null)
|
||||
_instance = new HttpRuntimeCacheHelper<V>();
|
||||
return _instance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插入缓存(默认20分钟)
|
||||
/// </summary>
|
||||
/// <param name="key"> key</param>
|
||||
/// <param name="value">value</param>
|
||||
public void Add(string key, V value)
|
||||
{
|
||||
Add(key, value, 60 * 20);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插入缓存
|
||||
/// </summary>
|
||||
/// <param name="key"> key</param>
|
||||
/// <param name="value">value</param>
|
||||
/// <param name="cacheDurationInSeconds">过期时间单位秒</param>
|
||||
public void Add(string key, V value, int cacheDurationInSeconds)
|
||||
{
|
||||
Add(key, value, cacheDurationInSeconds, CacheItemPriority.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插入缓存.
|
||||
/// </summary>
|
||||
/// <param name="key">key</param>
|
||||
/// <param name="value">value</param>
|
||||
/// <param name="cacheDurationInSeconds">过期时间单位秒</param>
|
||||
/// <param name="priority">缓存项属性</param>
|
||||
public void Add(string key, V value, int cacheDurationInSeconds, CacheItemPriority priority)
|
||||
{
|
||||
string keyString = CreateKey(key);
|
||||
HttpRuntime.Cache.Insert(keyString, value, null,
|
||||
DateTime.Now.AddSeconds(cacheDurationInSeconds), Cache.NoSlidingExpiration, priority, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插入缓存.
|
||||
/// </summary>
|
||||
/// <param name="key">key</param>
|
||||
/// <param name="value">value</param>
|
||||
/// <param name="cacheDurationInSeconds">过期时间单位秒</param>
|
||||
/// <param name="priority">缓存项属性</param>
|
||||
public void Add(string key, V value, int
|
||||
cacheDurationInSeconds, CacheDependency dependency, CacheItemPriority priority)
|
||||
{
|
||||
string keyString = CreateKey(key);
|
||||
HttpRuntime.Cache.Insert(keyString, value,
|
||||
dependency, DateTime.Now.AddSeconds(cacheDurationInSeconds), Cache.NoSlidingExpiration, priority, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除缓存
|
||||
/// </summary>
|
||||
/// <param name="key">key</param>
|
||||
public void Remove(string key)
|
||||
{
|
||||
HttpRuntime.Cache.Remove(CreateKey(key));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除所有缓存
|
||||
/// </summary>
|
||||
public void RemoveAll()
|
||||
{
|
||||
System.Web.Caching.Cache cache = HttpRuntime.Cache;
|
||||
IDictionaryEnumerator CacheEnum = cache.GetEnumerator();
|
||||
ArrayList al = new ArrayList();
|
||||
while (CacheEnum.MoveNext())
|
||||
{
|
||||
al.Add(CacheEnum.Key);
|
||||
}
|
||||
foreach (string key in al)
|
||||
{
|
||||
cache.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除所有包含关键字的缓存
|
||||
/// </summary>
|
||||
/// <param name="removeKey">关键字</param>
|
||||
public void RemoveAll(Func<string, bool> removeExpression)
|
||||
{
|
||||
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
|
||||
var allKeyList = GetAllKey();
|
||||
var delKeyList = allKeyList.Where(removeExpression).ToList();
|
||||
foreach (var key in delKeyList)
|
||||
{
|
||||
HttpRuntime.Cache.Remove(key); ;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有缓存key
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<string> GetAllKey()
|
||||
{
|
||||
IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();
|
||||
while (CacheEnum.MoveNext())
|
||||
{
|
||||
yield return CacheEnum.Key.ToString();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 私有函数
|
||||
|
||||
/// <summary>
|
||||
///创建KEY
|
||||
/// </summary>
|
||||
/// <param name="key">Key</param>
|
||||
/// <returns></returns>
|
||||
private string CreateKey(string key)
|
||||
{
|
||||
return key;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// 有关程序集的一般信息由以下
|
||||
// 控制。更改这些特性值可修改
|
||||
// 与程序集关联的信息。
|
||||
[assembly: AssemblyTitle("SqlSugar.Extensions.Cache")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("SqlSugar.Extensions.Cache")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2017")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
//将 ComVisible 设置为 false 将使此程序集中的类型
|
||||
//对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
|
||||
//请将此类型的 ComVisible 特性设置为 true。
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||
[assembly: Guid("cdb72abe-0336-4730-a195-abf2611deeaa")]
|
||||
|
||||
// 程序集的版本信息由下列四个值组成:
|
||||
//
|
||||
// 主版本
|
||||
// 次版本
|
||||
// 生成号
|
||||
// 修订号
|
||||
//
|
||||
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
|
||||
// 方法是按如下所示使用“*”: :
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
12
Src/Asp.Net/SqlSugar.Extensions.DataCache/RedisCache.cs
Normal file
12
Src/Asp.Net/SqlSugar.Extensions.DataCache/RedisCache.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar.Extensions
|
||||
{
|
||||
public class RedisCache
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{CDB72ABE-0336-4730-A195-ABF2611DEEAA}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>SqlSugar.Extensions.Cache</RootNamespace>
|
||||
<AssemblyName>SqlSugar.Extensions.Cache</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Web" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="HttpRuntimeCache.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="RedisCache.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SqlSugar\SqlSugar.csproj">
|
||||
<Project>{489bb790-226c-4fad-8d1e-51d72a7ff8e5}</Project>
|
||||
<Name>SqlSugar</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
@ -17,10 +17,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PerformanceTest", "Performa
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SqlSugar.Extensions", "SqlSugar.Extensions", "{B762D1DA-DFCD-4597-A14D-4F20DA0EE70E}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlSugar.Extensions.Cache", "SqlSugar.Extensions.Cache\SqlSugar.Extensions.Cache.csproj", "{CDB72ABE-0336-4730-A195-ABF2611DEEAA}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtensionsDemo", "ExtensionsDemo\ExtensionsDemo.csproj", "{EBBA686A-C1F0-4823-85EB-32EB306ADD7F}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlSugar.Extensions.DataCache", "SqlSugar.Extensions.DataCache\SqlSugar.Extensions.DataCache.csproj", "{CDB72ABE-0336-4730-A195-ABF2611DEEAA}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -51,20 +51,20 @@ Global
|
||||
{60D6AA62-93ED-4D02-80E4-6BEB81766D3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{60D6AA62-93ED-4D02-80E4-6BEB81766D3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{60D6AA62-93ED-4D02-80E4-6BEB81766D3E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{CDB72ABE-0336-4730-A195-ABF2611DEEAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{CDB72ABE-0336-4730-A195-ABF2611DEEAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{CDB72ABE-0336-4730-A195-ABF2611DEEAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CDB72ABE-0336-4730-A195-ABF2611DEEAA}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{EBBA686A-C1F0-4823-85EB-32EB306ADD7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{EBBA686A-C1F0-4823-85EB-32EB306ADD7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{EBBA686A-C1F0-4823-85EB-32EB306ADD7F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{EBBA686A-C1F0-4823-85EB-32EB306ADD7F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{CDB72ABE-0336-4730-A195-ABF2611DEEAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{CDB72ABE-0336-4730-A195-ABF2611DEEAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{CDB72ABE-0336-4730-A195-ABF2611DEEAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CDB72ABE-0336-4730-A195-ABF2611DEEAA}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{CDB72ABE-0336-4730-A195-ABF2611DEEAA} = {B762D1DA-DFCD-4597-A14D-4F20DA0EE70E}
|
||||
{EBBA686A-C1F0-4823-85EB-32EB306ADD7F} = {B762D1DA-DFCD-4597-A14D-4F20DA0EE70E}
|
||||
{CDB72ABE-0336-4730-A195-ABF2611DEEAA} = {B762D1DA-DFCD-4597-A14D-4F20DA0EE70E}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
Loading…
Reference in New Issue
Block a user