diff --git a/Src/Asp.Net/ExtensionsDemo/ExtensionsDemo.csproj b/Src/Asp.Net/ExtensionsDemo/ExtensionsDemo.csproj index 2465ef3bb..ef370983f 100644 --- a/Src/Asp.Net/ExtensionsDemo/ExtensionsDemo.csproj +++ b/Src/Asp.Net/ExtensionsDemo/ExtensionsDemo.csproj @@ -53,10 +53,6 @@ - - {cdb72abe-0336-4730-a195-abf2611deeaa} - SqlSugar.Extensions.Cache - {489bb790-226c-4fad-8d1e-51d72a7ff8e5} SqlSugar diff --git a/Src/Asp.Net/SqlSugar.Extensions.DataCache/HttpRuntimeCache.cs b/Src/Asp.Net/SqlSugar.Extensions.DataCache/HttpRuntimeCache.cs new file mode 100644 index 000000000..b0a1c6064 --- /dev/null +++ b/Src/Asp.Net/SqlSugar.Extensions.DataCache/HttpRuntimeCache.cs @@ -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(string key, V value) + { + HttpRuntimeCacheHelper.GetInstance().Add(key, value); + } + + public void Add(string key, V value, int cacheDurationInSeconds) + { + HttpRuntimeCacheHelper.GetInstance().Add(key, value, cacheDurationInSeconds); + } + + public bool ContainsKey(string key) + { + return HttpRuntimeCacheHelper.GetInstance().ContainsKey(key); + } + + public V Get(string key) + { + return HttpRuntimeCacheHelper.GetInstance().Get(key); + } + + public IEnumerable GetAllKey() + { + return HttpRuntimeCacheHelper.GetInstance().GetAllKey(); + } + + public V GetOrCreate(string cacheKey, Func create, int cacheDurationInSeconds = int.MaxValue) + { + var cacheManager = HttpRuntimeCacheHelper.GetInstance(); + if (cacheManager.ContainsKey(cacheKey)) + { + return cacheManager[cacheKey]; + } + else + { + var result = create(); + cacheManager.Add(cacheKey, result, cacheDurationInSeconds); + return result; + } + } + + public void Remove(string key) + { + HttpRuntimeCacheHelper.GetInstance().Remove(key); + } + } + + internal class HttpRuntimeCacheHelper + { + + #region 全局变量 + private static HttpRuntimeCacheHelper _instance = null; + private static readonly object _instanceLock = new object(); + #endregion + + #region 构造函数 + + private HttpRuntimeCacheHelper() { } + #endregion + + #region 属性 + /// + ///根据key获取value + /// + /// + public V this[string key] + { + get { return (V)HttpRuntime.Cache[CreateKey(key)]; } + } + #endregion + + #region 公共函数 + + /// + /// key是否存在 + /// + /// key + /// /// 存在true 不存在false. /// /// + public bool ContainsKey(string key) + { + return HttpRuntime.Cache[CreateKey(key)] != null; + } + + /// + /// 获取缓存值 + /// + /// key + /// + public V Get(string key) + { + return (V)HttpRuntime.Cache.Get(CreateKey(key)); + } + + /// + /// 获取实例 (单例模式) + /// + /// + public static HttpRuntimeCacheHelper GetInstance() + { + if (_instance == null) + lock (_instanceLock) + if (_instance == null) + _instance = new HttpRuntimeCacheHelper(); + return _instance; + } + + /// + /// 插入缓存(默认20分钟) + /// + /// key + /// value + public void Add(string key, V value) + { + Add(key, value, 60 * 20); + } + + /// + /// 插入缓存 + /// + /// key + /// value + /// 过期时间单位秒 + public void Add(string key, V value, int cacheDurationInSeconds) + { + Add(key, value, cacheDurationInSeconds, CacheItemPriority.Default); + } + + /// + /// 插入缓存. + /// + /// key + /// value + /// 过期时间单位秒 + /// 缓存项属性 + 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); + } + + /// + /// 插入缓存. + /// + /// key + /// value + /// 过期时间单位秒 + /// 缓存项属性 + 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); + } + + /// + /// 删除缓存 + /// + /// key + public void Remove(string key) + { + HttpRuntime.Cache.Remove(CreateKey(key)); + } + + /// + /// 清除所有缓存 + /// + 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); + } + } + + /// + /// 清除所有包含关键字的缓存 + /// + /// 关键字 + public void RemoveAll(Func 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); ; + } + } + + /// + /// 获取所有缓存key + /// + /// + public IEnumerable GetAllKey() + { + IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator(); + while (CacheEnum.MoveNext()) + { + yield return CacheEnum.Key.ToString(); + } + } + #endregion + + #region 私有函数 + + /// + ///创建KEY + /// + /// Key + /// + private string CreateKey(string key) + { + return key; + } + #endregion + } +} diff --git a/Src/Asp.Net/SqlSugar.Extensions.DataCache/Properties/AssemblyInfo.cs b/Src/Asp.Net/SqlSugar.Extensions.DataCache/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..61c919340 --- /dev/null +++ b/Src/Asp.Net/SqlSugar.Extensions.DataCache/Properties/AssemblyInfo.cs @@ -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")] diff --git a/Src/Asp.Net/SqlSugar.Extensions.DataCache/RedisCache.cs b/Src/Asp.Net/SqlSugar.Extensions.DataCache/RedisCache.cs new file mode 100644 index 000000000..fa6a29e50 --- /dev/null +++ b/Src/Asp.Net/SqlSugar.Extensions.DataCache/RedisCache.cs @@ -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 + { + } +} diff --git a/Src/Asp.Net/SqlSugar.Extensions.DataCache/SqlSugar.Extensions.DataCache.csproj b/Src/Asp.Net/SqlSugar.Extensions.DataCache/SqlSugar.Extensions.DataCache.csproj new file mode 100644 index 000000000..bdef2d035 --- /dev/null +++ b/Src/Asp.Net/SqlSugar.Extensions.DataCache/SqlSugar.Extensions.DataCache.csproj @@ -0,0 +1,62 @@ + + + + + Debug + AnyCPU + {CDB72ABE-0336-4730-A195-ABF2611DEEAA} + Library + Properties + SqlSugar.Extensions.Cache + SqlSugar.Extensions.Cache + v4.5.2 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + {489bb790-226c-4fad-8d1e-51d72a7ff8e5} + SqlSugar + + + + + \ No newline at end of file diff --git a/Src/Asp.Net/SqlSugar.sln b/Src/Asp.Net/SqlSugar.sln index f4c677ea1..622623872 100644 --- a/Src/Asp.Net/SqlSugar.sln +++ b/Src/Asp.Net/SqlSugar.sln @@ -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