|
- using StackExchange.Redis;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
-
- namespace BPASmartClient.Compiler
- {
- public class FRedisClient
- {
- #region 单例模式
- //private static FRedisClient instance = null;
- //public static FRedisClient Instance()
- //{
- // if (instance == null) instance = new FRedisClient();
- // return instance;
- //}
- #endregion
-
- #region 变量
- /// <summary>
- /// IP地址
- /// </summary>
- public string redisconnection = "124.222.238.75:16000,password=123456";
- /// <summary>
- /// redis 连接状态
- /// </summary>
- public ConnectionMultiplexer _connection = null;
- /// <summary>
- /// 数据存储位置
- /// </summary>
- public IDatabase _database = null;
- /// <summary>
- /// 通道建立连接
- /// </summary>
- public ISubscriber subscibe = null;
- #endregion
-
- #region 外部访问
- /// <summary>
- /// 委托出去
- /// </summary>
- public Action<string,string> LogMeaage = null;
- #endregion
-
- public void Connect()
- {
- _connection = ConnectionMultiplexer.Connect(ConfigurationOptions.Parse(redisconnection));
- _database = _connection.GetDatabase(0);//默认使用db0
- subscibe = _connection.GetSubscriber();
- }
- int dbi = 0;
- public void Connect(string connection)
- {
- _connection = ConnectionMultiplexer.Connect(ConfigurationOptions.Parse(connection));
- if (connection.Contains("defaultDatabase="))
- {
- string[] str=connection.Split(',');
- string stro = str.ToList().Find(s => s.Contains("defaultDatabase="));
-
- try
- {
- dbi=int.Parse(stro.Replace("defaultDatabase=",""));
- }
- catch (Exception ex)
- {
-
- throw;
- }
- _database = _connection.GetDatabase(dbi);//默认使用db0
- }
- else
- {
- _database = _connection.GetDatabase(dbi);//默认使用db0
- }
- subscibe = _connection.GetSubscriber();
- }
-
- /// <summary>
- /// 获取设备列表
- /// </summary>
- /// <returns></returns>
- public Dictionary<string,string> GetKeys()
- {
- Dictionary<string,string> keys = new Dictionary<string,string>();
- foreach (var endPoint in _connection.GetEndPoints())
- {
- //获取指定服务器
- var server = _connection.GetServer(endPoint);
- //在指定服务器上使用 keys 或者 scan 命令来遍历key
- //foreach (var key in server.Keys(0,"设备列表:*"))
- //{
- // //获取key对于的值
- // var val = _database.StringGet(key);
- // Console.WriteLine($"key: {key}, value: {val}");
- // keys[key] = val;
- //}
- foreach (var key in server.Keys(dbi,"*"))
- {
- //获取key对于的值
- var val = _database.StringGet(key);
- Console.WriteLine($"key: {key}, value: {val}");
- keys[key] = val;
- }
- }
- return keys;
- }
-
- /// <summary>
- /// 订阅通道消息
- /// </summary>
- public void SubscribeChanne(string channelname)
- {
- if (subscibe == null) return;
- subscibe.Subscribe(channelname,(channel,message) =>
- {
- MessageLog(channel,message);
- });
- }
-
- /// <summary>
- /// 发布通道消息
- /// </summary>
- public void PublishChanne(string channelname,string value)
- {
- if (subscibe == null) return;
- subscibe.Publish(channelname,value);
- }
-
- /// <summary>
- /// 获取 key 值
- /// </summary>
- public RedisValue RedisGet(string key,string hashField = "")
- {
- if (_database == null) return new RedisValue();
- RedisValue result;
- if (string.IsNullOrEmpty(hashField))
- {
- result = _database.StringGet(key);
- }
- else
- {
- result = _database.HashGet(key,hashField);
- }
- return result;
- //MessageLog(key,result);
- }
- /// <summary>
- /// 设置 redis 的值
- /// </summary>
- public bool RedisSet(string key,string hashField,string value)
- {
- bool result;
- if (string.IsNullOrEmpty(hashField))
- {
- result = _database.StringSet(key,value);
- }
- else
- {
- result = _database.HashSet(key,hashField,value);
- }
- return result;
- }
-
- /// <summary>
- /// 消息打印
- /// </summary>
- private void MessageLog(string key,string msg)
- {
- if (LogMeaage != null)
- {
- LogMeaage.Invoke(key,msg);
- }
- }
- }
- }
|