支持缓冲
This commit is contained in:
		
							parent
							
								
									fd59272e23
								
							
						
					
					
						commit
						e8b8b6000d
					
				@ -0,0 +1,52 @@
 | 
			
		||||
using SqlSugar;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Runtime.Caching;
 | 
			
		||||
 | 
			
		||||
namespace Falcon.SugarApi.DatabaseDefinitions.Cache
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// HttpRuntimeCache 缓存
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public class HttpRuntimeCache : ICacheService
 | 
			
		||||
    {
 | 
			
		||||
        public void Add<V>(string key, V value) {
 | 
			
		||||
            ObjectCache cache = MemoryCache.Default;
 | 
			
		||||
            cache[key] = value;
 | 
			
		||||
        }
 | 
			
		||||
        public void Add<V>(string key, V value, int cacheDurationInSeconds) {
 | 
			
		||||
            MemoryCache.Default.Add(key, value, new CacheItemPolicy() {
 | 
			
		||||
                AbsoluteExpiration = DateTime.Now.AddSeconds(cacheDurationInSeconds)
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
        public bool ContainsKey<V>(string key) {
 | 
			
		||||
            return MemoryCache.Default.Contains(key);
 | 
			
		||||
        }
 | 
			
		||||
        public V Get<V>(string key) {
 | 
			
		||||
            return (V)MemoryCache.Default.Get(key);
 | 
			
		||||
        }
 | 
			
		||||
        public IEnumerable<string> GetAllKey<V>() {
 | 
			
		||||
            var keys = new List<string>();
 | 
			
		||||
            foreach (var cacheItem in MemoryCache.Default) {
 | 
			
		||||
                keys.Add(cacheItem.Key);
 | 
			
		||||
            }
 | 
			
		||||
            return keys;
 | 
			
		||||
        }
 | 
			
		||||
        public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue) {
 | 
			
		||||
            var cacheManager = MemoryCache.Default;
 | 
			
		||||
            if (cacheManager.Contains(cacheKey)) {
 | 
			
		||||
                return (V)cacheManager[cacheKey];
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                var result = create();
 | 
			
		||||
                cacheManager.Add(cacheKey, result, new CacheItemPolicy() {
 | 
			
		||||
                    AbsoluteExpiration = DateTime.Now.AddSeconds(cacheDurationInSeconds)
 | 
			
		||||
                });
 | 
			
		||||
                return result;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        public void Remove<V>(string key) {
 | 
			
		||||
            MemoryCache.Default.Remove(key);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,83 @@
 | 
			
		||||
using SqlSugar;
 | 
			
		||||
using SugarRedis;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
 | 
			
		||||
namespace Falcon.SugarApi.DatabaseDefinitions.Cache
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// SqlSugar使用Redis缓冲。必须以单例实现
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public class SqlSugarRedisCache : ICacheService
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        //NUGET安装 SugarRedis  (也可以自个实现)   
 | 
			
		||||
        //注意:SugarRedis 不要扔到构造函数里面, 一定要单例模式  
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 数据缓冲服务
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public static SugarRedisClient Service { get; private set; } = null;
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 通过配置实现Redis缓冲,必须单例实现.
 | 
			
		||||
        /// <para>默认:127.0.0.1:6379,password=,connectTimeout=3000,connectRetry=1,syncTimeout=10000,DefaultDatabase=0</para>
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public SqlSugarRedisCache() {
 | 
			
		||||
            Service ??= new SugarRedisClient();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 通过配置实现Redis缓冲,必须单例实现
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="redisConnectionString">Redis链接字符串</param>
 | 
			
		||||
        public SqlSugarRedisCache(string redisConnectionString) {
 | 
			
		||||
            Service ??= redisConnectionString.IsNullOrEmpty() ? new SugarRedisClient()
 | 
			
		||||
                : new SugarRedisClient(redisConnectionString);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Add<V>(string key, V value) {
 | 
			
		||||
            Service.Set(key, value);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Add<V>(string key, V value, int cacheDurationInSeconds) {
 | 
			
		||||
            Service.Set(key, value, cacheDurationInSeconds);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public bool ContainsKey<V>(string key) {
 | 
			
		||||
            return Service.Exists(key);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public V Get<V>(string key) {
 | 
			
		||||
            return Service.Get<V>(key);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public IEnumerable<string> GetAllKey<V>() {
 | 
			
		||||
 | 
			
		||||
            return Service.SearchCacheRegex("SqlSugarDataCache.*");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue) {
 | 
			
		||||
            if (ContainsKey<V>(cacheKey)) {
 | 
			
		||||
                var result = Get<V>(cacheKey);
 | 
			
		||||
                if (result == null) {
 | 
			
		||||
                    return create();
 | 
			
		||||
                }
 | 
			
		||||
                else {
 | 
			
		||||
                    return result;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                var result = create();
 | 
			
		||||
                Add(cacheKey, result, cacheDurationInSeconds);
 | 
			
		||||
                return result;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Remove<V>(string key) {
 | 
			
		||||
            Service.Remove(key);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -1,4 +1,5 @@
 | 
			
		||||
using SqlSugar;
 | 
			
		||||
using Falcon.SugarApi.DatabaseDefinitions.Cache;
 | 
			
		||||
using SqlSugar;
 | 
			
		||||
using System;
 | 
			
		||||
using System.ComponentModel.DataAnnotations;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
@ -15,6 +16,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions
 | 
			
		||||
        /// 是否使用log
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public bool Log { get; set; }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 实例化SugarDb链接配置
 | 
			
		||||
        /// </summary>
 | 
			
		||||
@ -47,5 +49,36 @@ namespace Falcon.SugarApi.DatabaseDefinitions
 | 
			
		||||
                //}
 | 
			
		||||
            };
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private static ICacheService? CacheService = null;
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 通过配置实现Redis缓冲,必须单例实现.
 | 
			
		||||
        /// <para>默认:127.0.0.1:6379,password=,connectTimeout=3000,connectRetry=1,syncTimeout=10000,DefaultDatabase=0</para>
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public void AddRedisCache() {
 | 
			
		||||
            this.ConfigureExternalServices ??= new ConfigureExternalServices { };
 | 
			
		||||
            this.ConfigureExternalServices.DataInfoCacheService = new SqlSugarRedisCache();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 通过配置实现Redis缓冲,必须单例实现.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public void AddRedisCache(string connectionString) {
 | 
			
		||||
            if (connectionString.IsNullOrEmpty()) {
 | 
			
		||||
                AddRedisCache();
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            this.ConfigureExternalServices ??= new ConfigureExternalServices { };
 | 
			
		||||
            this.ConfigureExternalServices.DataInfoCacheService = new SqlSugarRedisCache(connectionString);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// HttpRuntimeCache 缓存
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public void AddHttpRuntimeCache() {
 | 
			
		||||
            this.ConfigureExternalServices ??= new ConfigureExternalServices { };
 | 
			
		||||
            this.ConfigureExternalServices.DataInfoCacheService = new HttpRuntimeCache();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -13,7 +13,9 @@
 | 
			
		||||
	<ItemGroup>
 | 
			
		||||
		<!--<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />-->
 | 
			
		||||
		<PackageReference Include="SqlSugarCore" Version="5.0.6.1" />
 | 
			
		||||
		<PackageReference Include="SugarRedis" Version="1.3.0" />
 | 
			
		||||
		<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.6.3" />
 | 
			
		||||
		<PackageReference Include="System.Runtime.Caching" Version="6.0.0" />
 | 
			
		||||
	</ItemGroup>
 | 
			
		||||
 | 
			
		||||
	<ItemGroup Condition=" '$(TargetFramework)' == 'net5' ">
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user