init commit

This commit is contained in:
路 范
2022-03-30 17:54:33 +08:00
parent df01841625
commit 904bdd16cd
500 changed files with 217251 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
using Furion.ConfigurableOptions;
namespace Ewide.Core
{
/// <summary>
/// 缓存配置
/// </summary>
public class CacheOptions : IConfigurableOptions
{
/// <summary>
/// 缓存类型
/// </summary>
public CacheType CacheType { get; set; }
/// <summary>
/// Redis配置
/// </summary>
public string RedisConnectionString { get; set; }
}
public enum CacheType
{
/// <summary>
/// 内存缓存
/// </summary>
MemoryCache,
/// <summary>
/// Redis缓存
/// </summary>
RedisCache
}
}

View File

@@ -0,0 +1,107 @@
using System;
using System.Threading.Tasks;
namespace Ewide.Core
{
/// <summary>
/// 缓存接口
/// </summary>
public interface ICache
{
/// <summary>
/// 用于在 key 存在时删除 key
/// </summary>
/// <param name="key">键</param>
long Del(params string[] key);
/// <summary>
/// 用于在 key 存在时删除 key
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
Task<long> DelAsync(params string[] key);
/// <summary>
/// 用于在 key 模板存在时删除
/// </summary>
/// <param name="pattern">key模板</param>
/// <returns></returns>
Task<long> DelByPatternAsync(string pattern);
/// <summary>
/// 检查给定 key 是否存在
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
bool Exists(string key);
/// <summary>
/// 检查给定 key 是否存在
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
Task<bool> ExistsAsync(string key);
/// <summary>
/// 获取指定 key 的值
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
string Get(string key);
/// <summary>
/// 获取指定 key 的值
/// </summary>
/// <typeparam name="T">byte[] 或其他类型</typeparam>
/// <param name="key">键</param>
/// <returns></returns>
T Get<T>(string key);
/// <summary>
/// 获取指定 key 的值
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
Task<string> GetAsync(string key);
/// <summary>
/// 获取指定 key 的值
/// </summary>
/// <typeparam name="T">byte[] 或其他类型</typeparam>
/// <param name="key">键</param>
/// <returns></returns>
Task<T> GetAsync<T>(string key);
/// <summary>
/// 设置指定 key 的值所有写入参数object都支持string | byte[] | 数值 | 对象
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
bool Set(string key, object value);
/// <summary>
/// 设置指定 key 的值所有写入参数object都支持string | byte[] | 数值 | 对象
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="expire">有效期</param>
bool Set(string key, object value, TimeSpan expire);
/// <summary>
/// 设置指定 key 的值所有写入参数object都支持string | byte[] | 数值 | 对象
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <returns></returns>
Task<bool> SetAsync(string key, object value);
/// <summary>
/// 设置指定 key 的值所有写入参数object都支持string | byte[] | 数值 | 对象
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="expire">有效期</param>
/// <returns></returns>
Task<bool> SetAsync(string key, object value, TimeSpan expire);
}
}

View File

@@ -0,0 +1,125 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Furion.DependencyInjection;
using Microsoft.Extensions.Caching.Memory;
namespace Ewide.Core
{
/// <summary>
/// 内存缓存
/// </summary>
public class MemoryCache : ICache, ISingleton
{
private readonly IMemoryCache _memoryCache;
public MemoryCache(IMemoryCache memoryCache)
{
_memoryCache = memoryCache;
}
public long Del(params string[] key)
{
foreach (var k in key)
{
_memoryCache.Remove(k);
}
return key.Length;
}
public Task<long> DelAsync(params string[] key)
{
foreach (var k in key)
{
_memoryCache.Remove(k);
}
return Task.FromResult((long)key.Length);
}
public async Task<long> DelByPatternAsync(string pattern)
{
if (string.IsNullOrEmpty(pattern))
return default;
//pattern = Regex.Replace(pattern, @"\{*.\}", "(.*)");
var keys = GetAllKeys().Where(k => k.StartsWith(pattern));
if (keys != null && keys.Any())
return await DelAsync(keys.ToArray());
return default;
}
public bool Exists(string key)
{
return _memoryCache.TryGetValue(key, out _);
}
public Task<bool> ExistsAsync(string key)
{
return Task.FromResult(_memoryCache.TryGetValue(key, out _));
}
public string Get(string key)
{
return _memoryCache.Get(key)?.ToString();
}
public T Get<T>(string key)
{
return _memoryCache.Get<T>(key);
}
public Task<string> GetAsync(string key)
{
return Task.FromResult(Get(key));
}
public Task<T> GetAsync<T>(string key)
{
return Task.FromResult(Get<T>(key));
}
public bool Set(string key, object value)
{
_memoryCache.Set(key, value);
return true;
}
public bool Set(string key, object value, TimeSpan expire)
{
_memoryCache.Set(key, value, expire);
return true;
}
public Task<bool> SetAsync(string key, object value)
{
Set(key, value);
return Task.FromResult(true);
}
public Task<bool> SetAsync(string key, object value, TimeSpan expire)
{
Set(key, value, expire);
return Task.FromResult(true);
}
private List<string> GetAllKeys()
{
const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
var entries = _memoryCache.GetType().GetField("_entries", flags).GetValue(_memoryCache);
var cacheItems = entries.GetType().GetProperty("Keys").GetValue(entries) as ICollection<object>; //entries as IDictionary;
var keys = new List<string>();
if (cacheItems == null) return keys;
return cacheItems.Select(u => u.ToString()).ToList();
//foreach (DictionaryEntry cacheItem in cacheItems)
//{
// keys.Add(cacheItem.Key.ToString());
//}
//return keys;
}
}
}

View File

@@ -0,0 +1,95 @@
using Furion.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Ewide.Core
{
/// <summary>
/// Redis缓存
/// </summary>
public class RedisCache : ICache, ISingleton
{
public RedisCache(IOptions<CacheOptions> cacheOptions)
{
var csredis = new CSRedis.CSRedisClient(cacheOptions.Value.RedisConnectionString);
RedisHelper.Initialization(csredis);
}
public long Del(params string[] key)
{
return RedisHelper.Del(key);
}
public Task<long> DelAsync(params string[] key)
{
return RedisHelper.DelAsync(key);
}
public async Task<long> DelByPatternAsync(string pattern)
{
if (string.IsNullOrEmpty(pattern))
return default;
//pattern = Regex.Replace(pattern, @"\{.*\}", "*");
var keys = (await RedisHelper.KeysAsync(pattern));
if (keys != null && keys.Length > 0)
{
return await RedisHelper.DelAsync(keys);
}
return default;
}
public bool Exists(string key)
{
return RedisHelper.Exists(key);
}
public Task<bool> ExistsAsync(string key)
{
return RedisHelper.ExistsAsync(key);
}
public string Get(string key)
{
return RedisHelper.Get(key);
}
public T Get<T>(string key)
{
return RedisHelper.Get<T>(key);
}
public Task<string> GetAsync(string key)
{
return RedisHelper.GetAsync(key);
}
public Task<T> GetAsync<T>(string key)
{
return RedisHelper.GetAsync<T>(key);
}
public bool Set(string key, object value)
{
return RedisHelper.Set(key, value);
}
public bool Set(string key, object value, TimeSpan expire)
{
return RedisHelper.Set(key, value, expire);
}
public Task<bool> SetAsync(string key, object value)
{
return RedisHelper.SetAsync(key, value);
}
public Task<bool> SetAsync(string key, object value, TimeSpan expire)
{
return RedisHelper.SetAsync(key, value, expire);
}
}
}