Merge branch 'dev' of https://git.tuotong-tech.com/tnb/tnb.server into dev
This commit is contained in:
247
common/Tnb.Common/Redis/RedisData.cs
Normal file
247
common/Tnb.Common/Redis/RedisData.cs
Normal file
@@ -0,0 +1,247 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using CSRedis;
|
||||
using JNPF;
|
||||
using JNPF.Common.Cache;
|
||||
using JNPF.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Tnb.Common.Redis
|
||||
{
|
||||
public class RedisData : ISingleton
|
||||
{
|
||||
private static CSRedisClient _instance;
|
||||
public RedisData()
|
||||
{
|
||||
RedisOptions _RedisOptions = App.GetConfig<RedisOptions>("Redis", true);
|
||||
_instance = new CSRedis.CSRedisClient(string.Format(_RedisOptions.RedisConnectionString, _RedisOptions.ip, _RedisOptions.port, _RedisOptions.password));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于在 key 存在时删除 key.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
public long Del(params string[] key)
|
||||
{
|
||||
return _instance.Del(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于在 key 存在时删除 key.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
public Task<long> DelAsync(params string[] key)
|
||||
{
|
||||
return _instance.DelAsync(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于在 key 模板存在时删除.
|
||||
/// </summary>
|
||||
/// <param name="pattern">key模板.</param>
|
||||
public async Task<long> DelByPatternAsync(string pattern)
|
||||
{
|
||||
if (string.IsNullOrEmpty(pattern))
|
||||
return default;
|
||||
|
||||
// pattern = Regex.Replace(pattern, @"\{.*\}", "*");
|
||||
string[]? keys = await _instance.KeysAsync(pattern);
|
||||
if (keys?.Length > 0)
|
||||
{
|
||||
return await _instance.DelAsync(keys);
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查给定 key 是否存在.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
public bool Exists(string key)
|
||||
{
|
||||
return _instance.Exists(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查给定 key 是否存在.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
public Task<bool> ExistsAsync(string key)
|
||||
{
|
||||
return _instance.ExistsAsync(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定 key 的增量值.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <param name="incrBy">增量.</param>
|
||||
/// <returns></returns>
|
||||
public long Incrby(string key, long incrBy)
|
||||
{
|
||||
return _instance.IncrBy(key, incrBy);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定 key 的增量值.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <param name="incrBy">增量.</param>
|
||||
/// <returns></returns>
|
||||
public Task<long> IncrbyAsync(string key, long incrBy)
|
||||
{
|
||||
return _instance.IncrByAsync(key, incrBy);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定 key 的值.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
public string Get(string key)
|
||||
{
|
||||
return _instance.Get(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定 key 的值.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">byte[] 或其他类型.</typeparam>
|
||||
/// <param name="key">键.</param>
|
||||
public T Get<T>(string key)
|
||||
{
|
||||
return _instance.Get<T>(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定 key 的值.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <returns></returns>
|
||||
public Task<string> GetAsync(string key)
|
||||
{
|
||||
return _instance.GetAsync(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定 key 的值.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">byte[] 或其他类型.</typeparam>
|
||||
/// <param name="key">键.</param>
|
||||
public Task<T> GetAsync<T>(string key)
|
||||
{
|
||||
return _instance.GetAsync<T>(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <param name="value">值.</param>
|
||||
public bool Set(string key, object value)
|
||||
{
|
||||
return _instance.Set(key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <param name="value">值.</param>
|
||||
/// <param name="expire">有效期.</param>
|
||||
public bool Set(string key, object value, TimeSpan expire)
|
||||
{
|
||||
return _instance.Set(key, value, expire);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <param name="value">值.</param>
|
||||
public Task<bool> SetAsync(string key, object value)
|
||||
{
|
||||
return _instance.SetAsync(key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <param name="value">值.</param>
|
||||
/// <param name="expire">过期时间.</param>
|
||||
/// <returns></returns>
|
||||
public Task<bool> SetAsync(string key, object value, TimeSpan expire)
|
||||
{
|
||||
return _instance.SetAsync(key, value, expire);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 只有在 key 不存在时设置 key 的值.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <param name="value">值.</param>
|
||||
/// <param name="expire">有效期.</param>
|
||||
public bool SetNx(string key, object value, TimeSpan expire)
|
||||
{
|
||||
if (_instance.SetNx(key, value))
|
||||
{
|
||||
_instance.Set(key, value, expire);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 只有在 key 不存在时设置 key 的值.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <param name="value">值.</param>
|
||||
public bool SetNx(string key, object value)
|
||||
{
|
||||
return _instance.SetNx(key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有key.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<string> GetAllKeys()
|
||||
{
|
||||
return _instance.Keys("*").ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存过期时间.
|
||||
/// </summary>
|
||||
/// <param name="key">键值.</param>
|
||||
/// <returns></returns>
|
||||
public DateTime GetCacheOutTime(string key)
|
||||
{
|
||||
long second = _instance.PTtl(key);
|
||||
return DateTime.Now.AddMilliseconds(second);
|
||||
}
|
||||
public Task<string> GetHash(string key, string field)
|
||||
{
|
||||
return _instance.HGetAsync(key, field);
|
||||
}
|
||||
public Task<bool> HashExist(string key, string field)
|
||||
{
|
||||
return _instance.HExistsAsync(key, field);
|
||||
}
|
||||
public Task<Dictionary<string, string>> HGetAll(string key)
|
||||
{
|
||||
return _instance.HGetAllAsync(key);
|
||||
}
|
||||
public Task<bool> HSet(string key, string field, string value)
|
||||
{
|
||||
return _instance.HSetAsync(key, field, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
34
common/Tnb.Common/Redis/RedisOptions.cs
Normal file
34
common/Tnb.Common/Redis/RedisOptions.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using JNPF.Common.Cache;
|
||||
using JNPF.ConfigurableOptions;
|
||||
|
||||
namespace Tnb.Common.Redis
|
||||
{
|
||||
public class RedisOptions : IConfigurableOptions
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Redis配置.
|
||||
/// </summary>
|
||||
public string RedisConnectionString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 服务器地址.
|
||||
/// </summary>
|
||||
public string ip { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 端口.
|
||||
/// </summary>
|
||||
public int port { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 密码.
|
||||
/// </summary>
|
||||
public string password { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user