public RedisSubscription(IRedisNativeClient redisClient) { this.redisClient = redisClient; this.SubscriptionCount = 0; this.activeChannels = new List<string>(); }
/// <summary> /// 获取所有HASH数据 /// </summary> /// <param name="hashId">Hash Id</param> /// <returns>泛型</returns> public List <T> HashGetAll <T>(string hashId) where T : class { byte[][] _allKey = null; List <T> _allRedisTypeList = null; using (IRedisClient redis = PRM.GetClient()) { IRedisNativeClient _redisNative = (IRedisNativeClient)redis; _allKey = _redisNative.HKeys(hashId); } if (_allKey != null) { _allRedisTypeList = new List <T>(); } Parallel.ForEach(_allKey, (key, loopState) => { List <T> _singleRedisTypeList = HashGet <List <T> >(hashId, Encoding.UTF8.GetString(key)); lock (looker) { if (_singleRedisTypeList != null) { _allRedisTypeList.AddRange(_singleRedisTypeList); } } }); return(_allRedisTypeList); }
private static IRedisNativeClient RedisClientInstance() { if (NativeClient == null) { NativeClient = new RedisNativeClient("10.15.89.60", 6379, ""); } return NativeClient; }
private static string GetRedisValue(IRedisNativeClient client, string key) { var redisValue = client.Get(key); return(redisValue != null ? Encoding.UTF8.GetString(redisValue) : "no found"); }
private static IRedisNativeClient RedisClientInstance() { if (NativeClient == null) { NativeClient = new RedisNativeClient("10.15.89.60", 6379, ""); } return(NativeClient); }
public void pushAccToRedis(IRedisNativeClient redis) { int response = Transaction(redis); while (response == 0) { response = Transaction(redis); } }
public static void GetLeaderScore(string host, int port, string password, int count, int sleep) { RedisUtils redisUtils = new RedisUtils(host, port, password); using (IRedisNativeClient client = redisUtils.GetNativeClient()) { var counter = 1; if (count < 1) { while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape)) { counter = 1; Common.DashLine(); Console.WriteLine($"Infinite Loop (3 seconds)"); Console.WriteLine($"Leaderboard - {DateTime.Now}"); Common.DashLine(); var timer1 = System.Diagnostics.Stopwatch.StartNew(); IDictionary <string, double> dic1 = redisUtils.GetClient().GetAllWithScoresFromSortedSet("leaderboard"); // Iterate through the Dictionary and Print Key, Value Pair foreach (KeyValuePair <string, double> c in dic1.OrderByDescending(key => key.Value).Take(10)) { var val1 = Convert.ToString(c.Key); var val2 = Convert.ToString(c.Value); Console.WriteLine($" {Convert.ToString(counter).PadRight(3)} {val1.PadRight(10)} {val2}"); counter++; } Common.DashLine(); Console.WriteLine($"Elapsed Time .. {Math.Round(timer1.Elapsed.TotalSeconds, 3)} sec."); Console.WriteLine("Press ESC Key to Exit Loop\n"); Thread.Sleep(sleep); } } else { Common.DashLine(); Console.WriteLine($"One-Time Update"); Console.WriteLine($"Leaderboard - {DateTime.Now}"); Common.DashLine(); var timer1 = System.Diagnostics.Stopwatch.StartNew(); IDictionary <string, double> dic1 = redisUtils.GetClient().GetAllWithScoresFromSortedSet("leaderboard"); foreach (KeyValuePair <string, double> c in dic1.OrderByDescending(key => key.Value).Take(10)) { var val1 = Convert.ToString(c.Key); var val2 = Convert.ToString(c.Value); Console.WriteLine($" {Convert.ToString(counter).PadRight(3)} {val1.PadRight(10)} {val2}"); counter++; } Common.DashLine(); Console.WriteLine($"Elapsed Time .. {Math.Round(timer1.Elapsed.TotalSeconds, 3)} sec."); } // end If - While - Else Loop } // end using IRedisNativeClient } // end GetLeaderScore
/// <summary> /// 添加HASH类型 /// </summary> /// <param name="hashId">HASH ID</param> /// <param name="source">需要设置的数据</param> public void HashSet <T>(string hashId, IEnumerable <IGrouping <string, T> > source) where T : class { Parallel.ForEach(source, (item, loopState) => { using (IRedisClient redis = PRM.GetClient()) { IRedisNativeClient _redisNative = (IRedisNativeClient)redis; byte[] _key = Encoding.UTF8.GetBytes(item.Key.ToString()); byte[] _value = ProtoBufHelper.Serialize(item); _redisNative.HSet(hashId, _key, _value); } }); }
public static void SaveDatabaseToDiskBackground(IRedisNativeClient client) { try { using (client) { client.BgSave(); } } catch (Exception e) { var msg = e.Message; throw; } }
public int Transaction(IRedisNativeClient redis) { Random rnd = new Random(); while (redis.Exists($"person:{ID}") == 1) { ID = Nationality + rnd.Next(99999).ToString(); } string[] watchkeys = { $"person:{ID}", "First_Name", "Last_Name" }; redisClient.Watch(watchkeys); using (IRedisTransaction trans = redisClient.CreateTransaction()) { trans.QueueCommand(redisClient => redisClient.SetEntryInHash($"person:{ID}", "First_Name", first_name)); trans.QueueCommand(redisClient => redisClient.SetEntryInHash($"person:{ID}", "Last_Name", last_name)); return(trans.Commit() == false ? 0 : 1); } }
public static void SaveLogEntry(long timeStamp, string logEntryText, IRedisNativeClient client) { try { var entryTextEncodedByteArray = logEntryText.ToUtf8EncodedByteArray(); using (client) { client.ZAdd(Constants.LogEntrySetId, timeStamp, entryTextEncodedByteArray); } } catch (Exception e) { var msg = e.Message; throw; } }
/// <summary> /// 获取HASH类型数据 /// </summary> /// <param name="hashId">HASH ID</param> /// <param name="key">HASH KEY</param> /// <returns>泛型</returns> public T HashGet <T>(string hashId, string key) where T : class { using (IRedisClient redis = PRM.GetClient()) { IRedisNativeClient _redisNative = (IRedisNativeClient)redis; byte[] _findedBuffer = _redisNative.HGet(hashId, Encoding.UTF8.GetBytes(key)); if (_findedBuffer != null) { return(ProtoBufHelper.Deserialize <T>(_findedBuffer)); } else { return(null); } } }
public int Transaction(IRedisNativeClient redis) { Random rnd = new Random(); while (redis.Exists($"account:{ID}:holder") == 1) { ID = rnd.Next(99999).ToString(); } //How to update watch in MULTI down below redisClient.Watch("account:{ ID}:holder"); using (IRedisTransaction trans = redisClient.CreateTransaction()) { trans.QueueCommand(redisClient => redisClient.SetEntryInHash($"account:{ID}", "Balance", balance)); trans.QueueCommand(redisClient => redisClient.Set($"account:{ID}:holder", GetStringBytes(holder))); trans.QueueCommand(redisClient => redisClient.AddItemToList($"{holder}_accounts", ID)); return(trans.Commit() == false ? 0 : 1); } }
public static byte[][] GetLogEntries(Tuple <long, long> dateRange, IRedisNativeClient client) { var logEntries = new byte[][] {}; try { var r = new [] { dateRange.Item1, dateRange.Item2 }; var min = r.Min(); var max = r.Max(); using (client) { logEntries = client.ZRangeByScoreWithScores(Constants.LogEntrySetId, min, max, null, null); } } catch (Exception e) { var msg = e.Message; throw; } return(logEntries); }
/// <summary> /// Create a new instance /// </summary> public NativeClientTransport(IRedisNativeClient client) => _client = client ?? throw new ArgumentNullException(nameof(client));
public RapidRedis() { client = new RedisClient("162.243.79.25", 6379, "Karachi@8681", 0); }
public static Configuration UseRedisMemoryCache(this Configuration configuration, IRedisNativeClient redisClient) { configuration.SetDefault<IMemoryCache, RedisMemoryCache>(new RedisMemoryCache(redisClient)); return configuration; }
public static void SaveRecentLogEntries(List <Tuple <long, string> > logEntries, IRedisNativeClient client) { try { using (client) { logEntries.ForEach(le => { client.ZAdd(Constants.LogEntrySetId, le.Item1, le.Item2.ToUtf8EncodedByteArray()); }); } } catch (Exception e) { var msg = e.Message; throw; } }
static bool IsConnected(IRedisNativeClient client) { return client != null ; }
public async Task TryConnect() { _client = await ConnectClient(); }