|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- 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);
- }
- }
- }
- }
|