首页

.Net中如何使用StackExchange.Redis组建

StackExchange.Redis是由Stack Overflow开发的c#语言Redis客户端,是当下最流行的Redis客户端组件,完全免费和开源,使用方便,性能优越,版本更新也及时,能适配Redis最新的功能。代码行如何很长,可能部分超出边界,可以将内容复制出来浏览。

前置任务
- 安装StackExchange.Redis
- 安装Newtonsoft.Json

1. 写一个核心的单例连接类,这个是内部的,不给外面调用
using StackExchange.Redis;
using System;
using System.Configuration;

namespace Util
{
internal class RedisClient
{
private static Lazy<ConfigurationOptions> ConfigOptions = new Lazy<ConfigurationOptions>(() =>
{
var connection = ConfigurationManager.AppSettings["RedisConnection"];
return ConfigurationOptions.Parse(connection);
});

private static Lazy<ConnectionMultiplexer> Connection = new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(ConfigOptions.Value));

private static ConnectionMultiplexer SafeConn
{
get
{
return Connection.Value;
}
}

public static IDatabase Database
{
get
{
return SafeConn.GetDatabase();
}
}
}
}

2. 再做一个Redis静态辅助类,外部调用这个
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Util
{
public class RedisHelpers
{
public static long StringIncrement(string incrKey)
{
return RedisClient.Database.StringIncrement(incrKey);
}

public static IEnumerable<string> SetMembers(string key)
{
IEnumerable<string> list = RedisClient.Database.SetMembers(key).Select(p => p.ToString());
return list;
}

public static bool HashExists(string key, string field)
{
bool result = RedisClient.Database.HashExists(key, field);
return result;
}

public static Dictionary<string, string> HashGetAll(string key)
{
Dictionary<string, string> dict = RedisClient.Database.HashGetAll(key).ToDictionary(k => k.Name.ToString(), v => v.Value.ToString());
return dict;
}

public static int HashLength(string key)
{
int result = (int)RedisClient.Database.HashLength(key);
return result;
}

public static long HashIncrement(string key, string field, long money)
{
return RedisClient.Database.HashIncrement(key, field, money);
}

public static long StringIncrement(string key, TimeSpan expires, long value = 1)
{
var result = RedisClient.Database.StringIncrement(key, value);
var exp = RedisClient.Database.KeyExpire(key, expires);
return result;
}

public static bool ListLeftPush<T>(string key, T data) where T : class
{
if (data == null)
return false;

string value = JsonConvert.SerializeObject(data);
return RedisClient.Database.ListLeftPush(key, value) > 0;
}

public static T ListRightPop<T>(string key) where T : class
{
string data = RedisClient.Database.ListRightPop(key);
if (string.IsNullOrWhiteSpace(data))
return null;

T item = JsonConvert.DeserializeObject<T>(data);
return item;
}

public static bool HashSet(string key, string field, string value)
{
return RedisClient.Database.HashSet(key, field, value);
}

public static bool HashSet(string key, string field, string value, TimeSpan expires)
{
var set = RedisClient.Database.HashSet(key, field, value);
var exp = RedisClient.Database.KeyExpire(key, expires);
return set && exp;
}

public static string HashGet(string key, string field)
{
return RedisClient.Database.HashGet(key, field);
}

public static bool HashDelete(string key, string field)
{
return RedisClient.Database.HashDelete(key, field);
}

public static bool StringSet<T>(string key, T data)
{
if (data == null)
return false;

string value = JsonConvert.SerializeObject(data);
return RedisClient.Database.StringSet(key, value);
}

public static bool StringSet<T>(string key, T data, TimeSpan expires)
{
if (data == null)
return false;

string value = JsonConvert.SerializeObject(data);
return RedisClient.Database.StringSet(key, value, expires);
}

public static T StringGet<T>(string key)
{
string value = RedisClient.Database.StringGet(key);
if (string.IsNullOrWhiteSpace(value))
{
object obj = null;

switch (typeof(T).FullName)
{
case "System.Int32":
obj = 0;
break;
default:
obj = null;
break;
}

return (T)obj;
}

T item = JsonConvert.DeserializeObject<T>(value);
return item;
}

public static bool KeyDelete(string key)
{
return RedisClient.Database.KeyDelete(key);
}
}
}

3. 配置文件中添加
<add key="RedisConnection" value="172.16.86.66:6379,allowAdmin=true,connectTimeout=30000,syncTimeout=5000" />

注意:不能每次连接都new一个连接,这样性能会很差,所以这里用Lazy方式做了一个单例类。这些代码是直接从生产环境中拿取的代码,经过几年的压力考验,代码没有注释(本人不大喜欢代码中太多注释,强迫症),但是应该很容易看懂,每个方法代表每个操作。
from 爱施园
Posted by 森林 on 2018/09/21
Copyright ©2018 爱施园 粤ICP备14091834号