This commit is contained in:
anjoy8 2022-04-20 18:14:27 +08:00
parent f798b0c6f4
commit 2bc4fbf297
4 changed files with 83 additions and 8 deletions

View File

@ -14,13 +14,8 @@ using Blog.Core.Model.Models;
using Blog.Core.Model.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace Blog.Core.Controllers
{
@ -106,6 +101,13 @@ namespace Blog.Core.Controllers
};
}
[HttpGet]
[AllowAnonymous]
public async Task<BlogArticle> TestSqlsugarWithCache()
{
return await _blogArticleServices.QueryById("1", true);
}
/// <summary>
/// Get方法
/// </summary>
@ -425,12 +427,12 @@ namespace Blog.Core.Controllers
public async Task<string> HttpPollyGet()
{
return await _httpPollyHelper.GetAsync(HttpEnum.LocalHost, "/api/ElasticDemo/GetDetailInfo?esid=3130&esindex=chinacodex");
}
}
#endregion
[HttpPost]
[AllowAnonymous]
public string TestEnum(EnumDemoDto dto)=>dto.Type.ToString();
public string TestEnum(EnumDemoDto dto) => dto.Type.ToString();
}
public class ClaimDto
{

View File

@ -0,0 +1,67 @@
using Microsoft.Extensions.Caching.Memory;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
namespace Blog.Core.Extensions
{
/// <summary>
/// 实现SqlSugar的ICacheService接口
/// </summary>
public class SqlSugarMemoryCacheService : ICacheService
{
protected IMemoryCache _memoryCache;
public SqlSugarMemoryCacheService(IMemoryCache memoryCache)
{
_memoryCache = memoryCache;
}
public void Add<V>(string key, V value)
{
_memoryCache.Set(key, value);
}
public void Add<V>(string key, V value, int cacheDurationInSeconds)
{
_memoryCache.Set(key, value, DateTimeOffset.Now.AddSeconds(cacheDurationInSeconds));
}
public bool ContainsKey<V>(string key)
{
return _memoryCache.TryGetValue(key, out _);
}
public V Get<V>(string key)
{
return _memoryCache.Get<V>(key);
}
public IEnumerable<string> GetAllKey<V>()
{
const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
var entries = _memoryCache.GetType().GetField("_entries", flags).GetValue(_memoryCache);
var cacheItems = entries as IDictionary;
var keys = new List<string>();
if (cacheItems == null) return keys;
foreach (DictionaryEntry cacheItem in cacheItems)
{
keys.Add(cacheItem.Key.ToString());
}
return keys;
}
public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
{
if (!_memoryCache.TryGetValue<V>(cacheKey, out V value))
{
value = create();
_memoryCache.Set(cacheKey, value, DateTime.Now.AddSeconds(cacheDurationInSeconds));
}
return value;
}
public void Remove<V>(string key)
{
_memoryCache.Remove(key);
}
}
}

View File

@ -2,6 +2,7 @@
using Blog.Core.Common.DB;
using Blog.Core.Common.Helper;
using Blog.Core.Common.LogHelper;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using StackExchange.Profiling;
@ -16,6 +17,8 @@ namespace Blog.Core.Extensions
/// </summary>
public static class SqlsugarSetup
{
private static readonly MemoryCache Cache = new MemoryCache(new MemoryCacheOptions());
public static void AddSqlsugarSetup(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
@ -26,6 +29,8 @@ namespace Blog.Core.Extensions
// 把多个连接对象注入服务这里必须采用Scope因为有事务操作
services.AddScoped<ISqlSugarClient>(o =>
{
var memoryCache = o.GetRequiredService<IMemoryCache>();
// 连接字符串
var listConfig = new List<ConnectionConfig>();
// 从库
@ -81,6 +86,7 @@ namespace Blog.Core.Extensions
// 自定义特性
ConfigureExternalServices = new ConfigureExternalServices()
{
DataInfoCacheService = new SqlSugarMemoryCacheService(memoryCache),
EntityService = (property, column) =>
{
if (column.IsPrimarykey && property.PropertyType == typeof(int))

View File

@ -67,7 +67,7 @@ namespace Blog.Core.Repository.Base
public async Task<TEntity> QueryById(object objId, bool blnUseCache = false)
{
//return await Task.Run(() => _db.Queryable<TEntity>().WithCacheIF(blnUseCache).InSingle(objId));
return await _db.Queryable<TEntity>().WithCacheIF(blnUseCache).In(objId).SingleAsync();
return await _db.Queryable<TEntity>().WithCacheIF(blnUseCache, 10).In(objId).SingleAsync();
}
/// <summary>