1、StackExchange.Redis基础操作帮助类
/**
*┌──────────────────────────────────────────────────────────────┐
*│ 描 述:Redis相关的工具类(Redis基础操作帮助类)
*│ 作 者:执笔小白
*│ 版 本:2.0
*│ 创建时间:2022-5-06 15:40:56
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│ 命名空间: Util.NoSQLHelpers
*│ 类 名:RedisHelper
*└──────────────────────────────────────────────────────────────┘
*/
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Windows.Forms;
using Newtonsoft.Json;
using StackExchange.Redis;
namespace Util.NoSQLHelpers
{
/// <summary>
/// Redis基础操作帮助类
/// </summary>
public class RedisHelper
{
#region 变量
/// <summary>
/// 初始化链接
/// RedisConStr:127.0.0.1:6379,password=ZhibiXiaobai123.
/// </summary>
private static ConnectionMultiplexer _con; // = ConnectionMultiplexer.Connect("127.0.0.1:6379,password=ZhibiXiaobai123.");//此处写redis的账号和密码;
// private string _host= ConfigurationSettings.AppSettings["RedisConStr"].Trim();
#endregion 变量
#region 初始化
/// <summary>
/// 初始化数据库链接
/// </summary>
/// <param name="conStr">数据库连接字符串</param>
public static void Initial(string conStr)
{
_con = ConnectionMultiplexer.Connect(conStr); // 此处写redis的账号和密码
}
#endregion 初始化
#region String类型
/// <summary>
/// 查
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">Key名</param>
public static string GetStringValue_Redis(int DataOrder, string Key)
{
IDatabase db = _con.GetDatabase(DataOrder); // GetDatabase()括号中如果不填写数字就默认是0
return db.StringGet(Key); // 得到缓存
}
/// <summary>
/// 增
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">Key名</param>
/// <param name="Value">Key值</param>
/// <param name="expiry">过期时间</param>
/// <returns></returns>
public static bool AddStringValue_Redis(int DataOrder, string Key, string Value, TimeSpan? expiry = null)
{
IDatabase db = _con.GetDatabase(DataOrder); // GetDatabase()括号中如果不填写数字就默认是0
if (expiry != null)
{
return db.StringSet(Key, Value, expiry); // string类型存入缓存
}
else
{
return db.StringSet(Key, Value); // string类型存入缓存
}
}
/// <summary>
/// 增加多个值
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">Key名</param>
/// <param name="Values">多个Key值</param>
/// <returns></returns>
public static bool AddStringValues_Redis(int DataOrder, string Key, List<string> Values)
{
IDatabase db = _con.GetDatabase(DataOrder);
return db.StringSet(new KeyValuePair<RedisKey, RedisValue>[] { new KeyValuePair<RedisKey, RedisValue>(Key, JsonConvert.SerializeObject(Values)) }); // 写入缓存
}
// 修改(值)
/// <summary>
/// 删
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">Key名</param>
/// <returns></returns>
public static bool DelStringValue_Redis(int DataOrder, string Key)
{
IDatabase db = _con.GetDatabase(DataOrder); // GetDatabase()括号中如果不填写数字就默认是0
return db.KeyDelete(Key); // 删除缓存key的缓存
}
#endregion String类型
#region hash 哈希
/// <summary>
/// 查
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">Key</param>
/// <param name="FieId">HashKey</param>
/// <returns></returns>
public static string GetHashValue_Redis(int DataOrder, string Key, string FieId)
{
IDatabase database = _con.GetDatabase(DataOrder);
return database.HashGet(Key, FieId); // 得到缓存
}
/// <summary>
/// 获取Hash所有的值
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">Key</param>
/// <returns></returns>
public static List<string> GetAllHashValue_Redis(int DataOrder, string Key)
{
IDatabase database = _con.GetDatabase(DataOrder);
HashEntry[] hashEntries = database.HashGetAll(Key); // 得到缓存
return hashEntries.Select(x => x.ToString()).ToList();
}
/// <summary>
/// 增
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">Key</param>
/// <param name="FieId">HashKey</param>
/// <param name="Value">HashValue</param>
/// <returns></returns>
public static bool AddHashValue_Redis(int DataOrder, string Key, string FieId, string Value)
{
IDatabase database = _con.GetDatabase(DataOrder);
return database.HashSet(Key, FieId, Value); // 写入缓存
}
// 增加多个值
public static void AddHashValues_Redis(int DataOrder, string Key, Dictionary<string, string> FieIds)
{
IDatabase database = _con.GetDatabase(DataOrder);
HashEntry[] hashEntries = FieIds.Select(i => new HashEntry(i.Key, JsonConvert.SerializeObject(i.Value))).ToArray();
database.HashSet(Key, hashEntries);
//foreach (var bb in Listbb)
//{
// database.HashDelete("hashList", bb.Name);//删除缓存key下的缓存
//}
}
// 修改(值)
/// <summary>
/// 删除hashkey元素
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">key</param>
/// <param name="HashKey">HashKey</param>
/// <returns></returns>
public static bool DelHashKeyValue_Redis(int DataOrder, string Key, string HashKey)
{
IDatabase database = _con.GetDatabase(DataOrder);
return database.HashDelete(Key, HashKey);//删除(缓存key和键对应的缓存数据)
}
/// <summary>
/// 删除
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">key</param>
/// <param name="Value">Value</param>
/// <returns></returns>
public static bool DelHashValue_Redis(int DataOrder, string Key)
{
IDatabase database = _con.GetDatabase(DataOrder);
return database.KeyDelete(Key);//删除(缓存key和键对应的缓存数据)
}
#endregion hash 哈希
#region set 集合
/// <summary>
/// 查所有
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">Key名</param>
public static List<string> GetSetValue_Redis(int DataOrder, string Key)
{
IDatabase db = _con.GetDatabase(DataOrder); // GetDatabase()括号中如果不填写数字就默认是0
var rrs = db.SetMembers(Key);
return rrs.Select(x => x.ToString()).ToList();
}
/// <summary>
/// 增
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">Key</param>
/// <param name="Value">Value</param>
/// <returns></returns>
public static bool AddSetValue_Redis(int DataOrder, string Key, string Value)
{
IDatabase db = _con.GetDatabase(DataOrder); // GetDatabase()括号中如果不填写数字就默认是0
return db.SetAdd(Key, Value); // set类型存入缓存
}
/// <summary>
/// 增加多个
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">Key</param>
/// <param name="Values">Values</param>
/// <returns></returns>
public static long AddSetValues_Redis(int DataOrder, string Key, List<string> Values)
{
IDatabase db = _con.GetDatabase(DataOrder); // GetDatabase()括号中如果不填写数字就默认是0
return db.SetAdd(Key, Values.Select(i => new StackExchange.Redis.RedisValue(i)).ToArray());
}
// 修改(值)
/// <summary>
/// 删除SetKey
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">Key</param>
/// <param name="FeiId">SetKey</param>
/// <returns></returns>
public static bool DelSetKeyValue_Redis(int DataOrder, string Key, string Value)
{
IDatabase db = _con.GetDatabase(DataOrder); // GetDatabase()括号中如果不填写数字就默认是0
return db.SetRemove(Key, Value); // 删除(key和value对应的缓存数据)
}
/// <summary>
/// 删
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">Key</param>
/// <returns></returns>
public static bool DelSetValue_Redis(int DataOrder, string Key)
{
IDatabase db = _con.GetDatabase(DataOrder); // GetDatabase()括号中如果不填写数字就默认是0
return db.KeyDelete(Key); // 删除key对应的缓存数据
}
#endregion set 集合
#region zset 有序集合
/// <summary>
/// 查
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">Key</param>
public static List<ZsetValue> GetZSetValue_Redis(int DataOrder, string Key)
{
IDatabase db = _con.GetDatabase(DataOrder); // GetDatabase()括号中如果不填写数字就默认是0
var result = db.SortedSetScan(Key);
return result.Select(item => new ZsetValue() { Value = item.Element, Order = item.Score.ToString() }).ToList<ZsetValue>();
}
/// <summary>
/// 增
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">Key</param>
/// <param name="Value">Value</param>
/// <param name="Order">优先级</param>
/// <returns></returns>
public static bool AddZSetValue_Redis(int DataOrder, string Key, string Value, int Order)
{
IDatabase db = _con.GetDatabase(DataOrder); // GetDatabase()括号中如果不填写数字就默认是0
return db.SortedSetAdd(Key, Value, Order); //添加
}
// 修改(值)
/// <summary>
/// 删ZSetKey
/// </summary>
/// <param name="DataOrder"></param>
/// <param name="Key"></param>
/// <returns></returns>
public static bool DelZSetKeyValue_Redis(int DataOrder, string Key, string Value)
{
IDatabase db = _con.GetDatabase(DataOrder); // GetDatabase()括号中如果不填写数字就默认是0
return db.SortedSetRemove(Key, Value);//删除
}
// 删
public static bool DelZSetValue_Redis(int DataOrder, string Key)
{
IDatabase db = _con.GetDatabase(DataOrder); // GetDatabase()括号中如果不填写数字就默认是0
return db.KeyDelete(Key);//删除key对应的缓存数据
}
#endregion zset 有序集合
#region list 列表
/// <summary>
/// 查
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">Key名</param>
public static List<string> GetListValue_Redis(int DataOrder, string Key)
{
IDatabase db = _con.GetDatabase(DataOrder); // GetDatabase()括号中如果不填写数字就默认是0
var ss = db.ListRange(Key); // 得到缓存
return ss.Select(item => item.ToString()).ToList<string>();
}
/// <summary>
/// 增
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">Key</param>
/// <param name="Value">Value</param>
/// <param name="IsTop">是否从顶部</param>
/// <returns></returns>
public static long AddListValue_Redis(int DataOrder, string Key, string Value, bool IsTop = false)
{
IDatabase db = _con.GetDatabase(DataOrder); // GetDatabase()括号中如果不填写数字就默认是0
if (IsTop)
{
return db.ListLeftPush(Key, Value); // 底部插入
}
else
{
return db.ListRightPush(Key, Value); // 底部插入
}
}
// 修改(值)
/// <summary>
/// 从顶部或者底部删除
/// </summary>
/// <param name="DataOrder">db的序号</param>
/// <param name="Key">Key</param>
/// <param name="IsTop">是否从顶部</param>
/// <returns></returns>
public static string DelListKeyValue_Redis(int DataOrder, string Key, bool IsTop = false)
{
IDatabase db = _con.GetDatabase(DataOrder); // GetDatabase()括号中如果不填写数字就默认是0
if (IsTop)
{
return db.ListLeftPop(Key); // 底部插入
}
else
{
return db.ListRightPop(Key); // 底部插入
}
}
// 删
public static bool DelListValue_Redis(int DataOrder, string Key)
{
IDatabase db = _con.GetDatabase(DataOrder); // GetDatabase()括号中如果不填写数字就默认是0
return db.KeyDelete(Key);//删除key对应的缓存数据
}
#endregion list 列表
#region stream 不常用-略
#endregion stream
}
/// <summary>
/// Zset的查询类
/// </summary>
public class ZsetValue
{
public string Value { set; get; }
public string Order { set; get; }
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
- 233.
- 234.
- 235.
- 236.
- 237.
- 238.
- 239.
- 240.
- 241.
- 242.
- 243.
- 244.
- 245.
- 246.
- 247.
- 248.
- 249.
- 250.
- 251.
- 252.
- 253.
- 254.
- 255.
- 256.
- 257.
- 258.
- 259.
- 260.
- 261.
- 262.
- 263.
- 264.
- 265.
- 266.
- 267.
- 268.
- 269.
- 270.
- 271.
- 272.
- 273.
- 274.
- 275.
- 276.
- 277.
- 278.
- 279.
- 280.
- 281.
- 282.
- 283.
- 284.
- 285.
- 286.
- 287.
- 288.
- 289.
- 290.
- 291.
- 292.
- 293.
- 294.
- 295.
- 296.
- 297.
- 298.
- 299.
- 300.
- 301.
- 302.
- 303.
- 304.
- 305.
- 306.
- 307.
- 308.
- 309.
- 310.
- 311.
- 312.
- 313.
- 314.
- 315.
- 316.
- 317.
- 318.
- 319.
- 320.
- 321.
- 322.
- 323.
- 324.
- 325.
- 326.
- 327.
- 328.
- 329.
- 330.
- 331.
- 332.
- 333.
- 334.
- 335.
- 336.
- 337.
- 338.
- 339.
- 340.
- 341.
- 342.
- 343.
- 344.
- 345.
- 346.
- 347.
- 348.
- 349.
- 350.
- 351.
- 352.
- 353.
- 354.
- 355.
- 356.
- 357.
- 358.
- 359.
- 360.
- 361.
- 362.
- 363.
- 364.
- 365.
- 366.
- 367.
- 368.
- 369.
- 370.
- 371.
- 372.
- 373.
- 374.
- 375.
- 376.
- 377.
- 378.
- 379.
- 380.
- 381.
- 382.
- 383.
- 384.
- 385.
- 386.
- 387.
2、链接字符串:
var conn = ConnectionMultiplexer.Connect("localhost:6379,password=...");
配置选项
ConfigurationOptions 包含大量的配置选项,一些常用的配置如下:
abortConnect : 当为true时,当没有可用的服务器时则不会创建一个连接
allowAdmin : 当为true时 ,可以使用一些被认为危险的命令
channelPrefix:所有pub/sub渠道的前缀
connectRetry :重试连接的次数
connectTimeout:超时时间
configChannel: Broadcast channel name for communicating configuration changes
defaultDatabase : 默认0到-1
keepAlive : 保存x秒的活动连接
name:ClientName
password:password
proxy:代理 比如 twemproxy
resolveDns : 指定dns解析
serviceName : Not currently implemented (intended for use with sentinel)
ssl={bool} : 使用sll加密
sslHost={string} : 强制服务器使用特定的ssl标识
syncTimeout={int} : 异步超时时间
tiebreaker={string}:Key to use for selecting a server in an ambiguous master scenario
version={string} : Redis version level (useful when the server does not make this available)
writeBuffer={int} : 输出缓存区的大小
各配置项用逗号分割
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
3、测试Demo
https://gitee.com/qq28069933146_admin/csharp_adonet_demo
作者:꧁执笔小白꧂