RedisHelper.cs
using StackExchange.Redis; namespace ConsoleApplication2 { //学习使用Nuget配置NewtonJson.dll //https://blog.csdn.net/leftfist/article/details/38687745 //https://blog.csdn.net/ligaofeng/article/details/17371769 // PM> install-package newtonsoft.json // PM> Install-Package StackExchange.Redis public class RedisHelper { private static readonly ConfigurationOptions ConfigurationOptions = ConfigurationOptions.Parse("127.0.0.1" + ":" + "6379"); private static readonly object Locker = new object(); private static ConnectionMultiplexer _redisConn; /// <summary> /// 单例获取 /// </summary> public static ConnectionMultiplexer RedisConn { get { if (_redisConn == null) { lock (Locker) { if (_redisConn == null || !_redisConn.IsConnected) { _redisConn = ConnectionMultiplexer.Connect(ConfigurationOptions); } } } return _redisConn; } } } }
用法:
static void Main(string[] args) { //http://www.cnblogs.com/weixiao520/p/5765358.html var db = RedisHelper.RedisConn.GetDatabase(); var key = "keyTest1"; //SET命令 db.StringSet(key, "1093939393939393939"); //GET命令 var value = db.StringGet(key); Console.WriteLine(value); //HMSET key = "hashTest"; db.HashSet(key, new[] { new HashEntry("b", "2"), new HashEntry("c", "3") }); //HMGET var values = db.HashGetAll(key); Console.WriteLine(values[0].Name + "///" + values[0].Value); Console.ReadLine(); }