数据库缓存支持分布式缓存

This commit is contained in:
falcon 2022-10-11 10:24:08 +08:00
parent 511bfab781
commit 14cb6627ff
2 changed files with 93 additions and 1 deletions

View File

@ -0,0 +1,81 @@
using Falcon.SugarApi.JsonSerialize;
using Microsoft.Extensions.Caching.Distributed;
using SqlSugar;
using System;
using System.Collections.Generic;
namespace Falcon.SugarApi.DatabaseDefinitions.Cache
{
/// <summary>
/// 使用分布式缓存存储器
/// </summary>
public class DistributedCache : ICacheService
{
/// <summary>
/// 分布式缓存存储器
/// </summary>
public IDistributedCache Cache { get; set; }
/// <summary>
/// 序列化方法
/// </summary>
public IJsonSerialize Serialize { get; set; }
/// <summary>
/// 提供分布式缓存存储器
/// </summary>
/// <param name="cache">分布式缓存存储器</param>
/// <param name="serialize">实现序列化的接口s</param>
public DistributedCache(IDistributedCache cache, IJsonSerialize serialize) {
this.Cache = cache;
this.Serialize = serialize;
}
/// <inheritdoc/>
public void Add<V>(string key, V value) {
var valStr = this.Serialize.Serialize(value);
this.Cache.SetString(key, valStr);
}
/// <inheritdoc/>
public void Add<V>(string key, V value, int cacheDurationInSeconds) {
var valStr = this.Serialize.Serialize(value);
DistributedCacheEntryOptions op = new() {
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(cacheDurationInSeconds),
};
this.Cache.SetString(key, valStr, op);
}
/// <inheritdoc/>
public bool ContainsKey<V>(string key) {
return this.Cache.Get(key) != null;
}
/// <inheritdoc/>
public V Get<V>(string key) {
var val = this.Cache.GetString(key);
if (val == null) return default(V);
return (V)this.Serialize.Deserialize(val, typeof(V));
}
/// <inheritdoc/>
public IEnumerable<string> GetAllKey<V>() {
throw new NotImplementedException();
}
/// <inheritdoc/>
public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue) {
if (ContainsKey<V>(cacheKey)) {
return Get<V>(cacheKey);
}
V val = create();
Add(cacheKey, val, cacheDurationInSeconds);
return val;
}
/// <inheritdoc/>
public void Remove<V>(string key) {
this.Cache.Remove(key);
}
}
}

View File

@ -1,8 +1,9 @@
using Falcon.SugarApi.DatabaseDefinitions.Cache; using Falcon.SugarApi.DatabaseDefinitions.Cache;
using Falcon.SugarApi.JsonSerialize;
using Microsoft.Extensions.Caching.Distributed;
using SqlSugar; using SqlSugar;
using System; using System;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection; using System.Reflection;
namespace Falcon.SugarApi.DatabaseDefinitions namespace Falcon.SugarApi.DatabaseDefinitions
@ -93,5 +94,15 @@ namespace Falcon.SugarApi.DatabaseDefinitions
this.ConfigureExternalServices ??= new ConfigureExternalServices { }; this.ConfigureExternalServices ??= new ConfigureExternalServices { };
this.ConfigureExternalServices.DataInfoCacheService = new HttpRuntimeCache(); this.ConfigureExternalServices.DataInfoCacheService = new HttpRuntimeCache();
} }
/// <summary>
/// 分布式缓存
/// </summary>
/// <param name="cache">缓存提供程序</param>
/// <param name="serialize">序列化实现</param>
public void AddDistributedCache(IDistributedCache cache, IJsonSerialize serialize) {
this.ConfigureExternalServices ??= new ConfigureExternalServices { };
this.ConfigureExternalServices.DataInfoCacheService = new DistributedCache(cache,serialize);
}
} }
} }