Skip to content

Commit

Permalink
✨增加SqlSugar缓存开关
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonNoCry committed Dec 3, 2024
1 parent d0b029f commit 593505a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 51 deletions.
3 changes: 2 additions & 1 deletion Blog.Core.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"SeedDBDataEnabled": true, //生成表,并初始化数据
"Author": "Blog.Core",
"SvcName": "", // /svc/blog
"UseLoadTest": false
"UseLoadTest": false,
"CacheDbEnabled": false
},

//优化DB配置、不会再区分单库多库
Expand Down
142 changes: 92 additions & 50 deletions Blog.Core.Common/Caches/SqlSugarCacheService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Blog.Core.Common.Caches.Interface;
using Blog.Core.Common.Option;
using SqlSugar;

namespace Blog.Core.Common.Caches;
Expand All @@ -15,54 +16,95 @@ namespace Blog.Core.Common.Caches;
/// </summary>
public class SqlSugarCacheService : ICacheService
{
private readonly Lazy<ICaching> _caching = new(() => App.GetService<ICaching>(false));
private ICaching Caching => _caching.Value;

public void Add<V>(string key, V value)
{
Caching.Set(key, value);
}

public void Add<V>(string key, V value, int cacheDurationInSeconds)
{
Caching.Set(key, value, TimeSpan.FromSeconds(cacheDurationInSeconds));
}

public bool ContainsKey<V>(string key)
{
return Caching.Exists(key);
}

public V Get<V>(string key)
{
return Caching.Get<V>(key);
}

public IEnumerable<string> GetAllKey<V>()
{
return Caching.GetAllCacheKeys();
}

public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
{
if (!ContainsKey<V>(cacheKey))
{
var value = create();
Caching.Set(cacheKey, value, TimeSpan.FromSeconds(cacheDurationInSeconds));
return value;
}

return Caching.Get<V>(cacheKey);
}

public void Remove<V>(string key)
{
Caching.Remove(key);
}

public bool RemoveAll()
{
Caching.RemoveAll();
return true;
}
private readonly Lazy<ICaching> _caching = new(() => App.GetService<ICaching>(false));
private ICaching Caching => _caching.Value;
private readonly AppSettingsOption _options = App.GetOptions<AppSettingsOption>();

public void Add<V>(string key, V value)
{
if (!_options.CacheDbEnabled)
{
return;
}

Caching.Set(key, value);
}

public void Add<V>(string key, V value, int cacheDurationInSeconds)
{
if (!_options.CacheDbEnabled)
{
return;
}

Caching.Set(key, value, TimeSpan.FromSeconds(cacheDurationInSeconds));
}

public bool ContainsKey<V>(string key)
{
if (!_options.CacheDbEnabled)
{
return default;
}

return Caching.Exists(key);
}

public V Get<V>(string key)
{
if (!_options.CacheDbEnabled)
{
return default;
}

return Caching.Get<V>(key);
}

public IEnumerable<string> GetAllKey<V>()
{
if (!_options.CacheDbEnabled)
{
return default;
}

return Caching.GetAllCacheKeys();
}

public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
{
if (!_options.CacheDbEnabled)
{
return create();
}

if (!ContainsKey<V>(cacheKey))
{
var value = create();
Caching.Set(cacheKey, value, TimeSpan.FromSeconds(cacheDurationInSeconds));
return value;
}

return Caching.Get<V>(cacheKey);
}

public void Remove<V>(string key)
{
if (!_options.CacheDbEnabled)
{
return;
}

Caching.Remove(key);
}

public bool RemoveAll()
{
if (!_options.CacheDbEnabled)
{
return true;
}

Caching.RemoveAll();
return true;
}
}

0 comments on commit 593505a

Please sign in to comment.