新增设备的redis
This commit is contained in:
@@ -11,5 +11,11 @@
|
||||
"port": 6379,
|
||||
"password": "05jWEoJa8v",
|
||||
"RedisConnectionString": "{0}:{1},password={2}, poolsize=500,ssl=false,defaultDatabase=0"
|
||||
},
|
||||
"Redis2": {
|
||||
"ip": "127.0.0.1",
|
||||
"port": 6379,
|
||||
"password": "05jWEoJa8v",
|
||||
"RedisConnectionString": "{0}:{1},password={2}, poolsize=500,ssl=false,defaultDatabase=1"
|
||||
}
|
||||
}
|
||||
284
common/Tnb.Common/Redis/RedisData2.cs
Normal file
284
common/Tnb.Common/Redis/RedisData2.cs
Normal file
@@ -0,0 +1,284 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Channels;
|
||||
using System.Threading.Tasks;
|
||||
using Aspose.Cells.Drawing;
|
||||
using CSRedis;
|
||||
using JNPF;
|
||||
using JNPF.Common.Cache;
|
||||
using JNPF.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json;
|
||||
using static CSRedis.CSRedisClient;
|
||||
|
||||
namespace Tnb.Common.Redis
|
||||
{
|
||||
public class RedisData2 : ISingleton
|
||||
{
|
||||
private CSRedisClient _instance;
|
||||
public delegate void rcvMsgHandler(string Channel, string Body);
|
||||
public event rcvMsgHandler rcvMsg;
|
||||
public RedisData2()
|
||||
{
|
||||
RedisOptions _RedisOptions = App.GetConfig<RedisOptions>("Redis2", true);
|
||||
_instance = new CSRedis.CSRedisClient(string.Format(_RedisOptions.RedisConnectionString, _RedisOptions.ip, _RedisOptions.port, _RedisOptions.password));
|
||||
|
||||
//_instance.Subscribe(("devdata_change", msg =>
|
||||
//{
|
||||
// if (rcvMsg != null)
|
||||
// {
|
||||
// rcvMsg(msg.Channel, msg.Body);
|
||||
// }
|
||||
//}
|
||||
//));
|
||||
}
|
||||
|
||||
/// <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);
|
||||
}
|
||||
|
||||
public async Task<T> TryGetValueByKeyField<T>(string key, string field)
|
||||
{
|
||||
if (await _instance.HExistsAsync(key, field))
|
||||
{
|
||||
string data = await GetHash(key, field);
|
||||
Dictionary<String, String> json = JsonConvert.DeserializeObject<Dictionary<String, String>>(data);
|
||||
Type type = typeof(T);
|
||||
if ((!type.IsValueType && type.GetGenericArguments().Length > 0) || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)))
|
||||
{
|
||||
return (T)Convert.ChangeType(json["Value"],type.GetGenericArguments()[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (T)Convert.ChangeType(json["Value"],type);
|
||||
}
|
||||
// return ;
|
||||
}
|
||||
return default(T);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user