123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- using Microsoft.AspNetCore.Mvc.Formatters;
- using StackExchange.Redis;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Reflection;
- using System.Text;
- namespace MS.WebCore.Core
- {
- public class RedisExample
- {
- private static ConnectionMultiplexer redisConnection;
- private static IDatabase redisDb;
-
-
- public RedisExample(int dbIndex = 0)
- {
-
-
- redisConnection = ConnectionMultiplexer.Connect("localhost");
- redisDb = redisConnection.GetDatabase();
- }
- public void SetData(string key, string value)
- {
- redisDb.StringSet(key, value);
- }
- public string GetData(string key)
- {
- return redisDb.StringGet(key);
- }
- public void DeleteData(string key, string field)
- {
- redisDb.HashDelete(key, field);
-
- }
- public void DeleteData(string key)
- {
- redisDb.KeyDelete(key);
-
- }
- public bool HashKeyExists(string hashKey, string fieldKey)
- {
- return redisDb.HashExists(hashKey, fieldKey);
- }
- public void SetDataWithTime(string key, string value, DateTime time)
- {
- redisDb.SortedSetAdd("mySortedSet", value, time.Ticks);
- }
- public string GetValueByTime(DateTime time)
- {
- var result = redisDb.SortedSetRangeByScore("mySortedSet", time.Ticks, time.Ticks);
- return result.FirstOrDefault();
- }
- public void AddToHash(string key, string field, string value)
- {
- RedisType keyType = redisDb.KeyType(key);
- if (keyType != RedisType.None && keyType != RedisType.Hash)
- {
-
- }
- redisDb.HashSet(key, field, value);
- }
-
-
-
-
-
-
-
-
-
-
- public List<string> GetValuesByUUID(string uuid)
- {
- var values = redisDb.ListRange("myList");
- List<string> result = new List<string>();
- foreach (var value in values)
- {
- var parts = value.ToString().Split(new[] { ' ' }, 2);
- var storedUUID = parts[0];
- if (storedUUID == uuid)
- {
- result.Add(value.ToString());
- }
- }
- return result;
- }
- public List<KeyValuePair<string, string>> GetListValues()
- {
- var values = redisDb.HashGetAll("myList");
- List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
- foreach (var value in values)
- {
- result.Add(new KeyValuePair<string, string>(value.Name, value.Value));
- }
- return result;
- }
- public List<KeyValuePair<string, string>> GetValuesByKey(string key)
- {
- var values = redisDb.HashGetAll("myList");
- List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
- foreach (var value in values)
- {
- if (value.Name == key)
- {
- result.Add(new KeyValuePair<string, string>(value.Name, value.Value));
- }
- }
- return result;
- }
- public List<string> GetListFromRedis(string listKey)
- {
- var redisValues = redisDb.ListRange(listKey);
- return redisValues.Select(value => value.ToString()).ToList();
- }
- public void AddToList(string key, string value)
- {
- RedisType keyType = redisDb.KeyType(key);
- if (keyType != RedisType.None && keyType != RedisType.Hash)
- {
-
-
- }
- redisDb.HashSet(key, value, value);
- }
- public List<KeyValuePair<string, string>> GetListValues(string key)
- {
- RedisType keyType = redisDb.KeyType(key);
- if (keyType != RedisType.Hash)
- {
-
- }
- var values = redisDb.HashGetAll(key);
- List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
- foreach (var value in values)
- {
- result.Add(new KeyValuePair<string, string>(value.Name, value.Value));
- }
- return result;
- }
- public List<string> GetHashKeysByPattern(string pattern)
- {
- List<string> keys = new List<string>();
-
- EndPoint[] endPoints = redisDb.Multiplexer.GetEndPoints();
- foreach (EndPoint endPoint in endPoints)
- {
- IServer server = redisDb.Multiplexer.GetServer(endPoint);
- keys.AddRange(server.Keys(pattern: pattern, database: redisDb.Database).Select(key => (string)key));
- }
- return keys;
- }
- public bool SetHashField(string hashKey, string fieldKey, string value)
- {
-
-
-
- return redisDb.HashSet(hashKey, fieldKey, value);
- }
- }
- }
|