需要在NuGet里面引用ServiceStack.Redis
NuGet 地址:https://www.nuget.org/packages/StackExchange.Redis/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StackExchange.Redis;
using System.Threading;
using System.Data;
using System.Configuration;
namespace WebCommon
{
/// <summary>
///
/// </summary>
public class RedisHelper
{
private static string connectStr = ConfigurationManager.AppSettings["RedisServer"];
private static ConfigurationOptions configurationOptions = ConfigurationOptions.Parse(connectStr);
//the lock for singleton
private static readonly object Locker = new object();
//singleton
private static ConnectionMultiplexer redisConn;
//singleton
public static ConnectionMultiplexer getRedisConn()
{
if (redisConn == null)
{
lock (Locker)
{
if (redisConn == null || !redisConn.IsConnected)
{
redisConn = ConnectionMultiplexer.Connect(configurationOptions);
}
}
}
return redisConn;
}
#region String类型操作
/// <summary>
/// 设置指定key 的值(默认第0个库)
/// </summary>
/// <param name="strKey">指定key的值</param>
/// <param name="strValue"></param>
/// <returns></returns>
public static bool InsertStr(string strKey, string strValue)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase();
return db.StringSet(strKey, strValue);
}
/// <summary>
/// 设置指定key 的值(指定库)
/// </summary>
/// <param name="strKey"></param>
/// <param name="strValue"></param>
/// <param name="database"></param>
/// <returns></returns>
public static bool InsertStr(string strKey, string strValue, int database)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase(database);
//set get
return db.StringSet(strKey, strValue);
}
/// <summary>
/// 设置指定key 的值(指定第一个库,指定过期时间)
/// </summary>
/// <param name="strKey">指定key的值</param>
/// <param name="strValue"></param>
/// <param name="expire"></param>
public void InsertStrExpire(string strKey, string strValue, DateTime expire)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase(1);
db.StringSet(strKey, strValue);
db.KeyExpire(strKey, expire);
}
/// <summary>
/// 设置指定key 的值(指定第一个库,指定过期分钟数)
/// </summary>
/// <param name="strKey">指定key的值</param>
/// <param name="strValue"></param>
/// <param name="timespanmin"></param>
public void InsertStrExpire(string strKey, string strValue, int timespanmin)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase(1);
db.StringSet(strKey, strValue, TimeSpan.FromMinutes(timespanmin));
}
/// <summary>
/// 设置指定key 的值(指定第一个库,指定过期分钟数)
/// </summary>
/// <param name="strKey">指定key的值</param>
/// <param name="strValue"></param>
/// <param name="timespanmin"></param>
public void InsertStrExpireByDatabaseOne(string strKey, string strValue, int timespanmin)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase(1);
db.StringSet(strKey, strValue, TimeSpan.FromMinutes(timespanmin));
}
/// <summary>
/// 获取指定 key 的值(默认第0个库)
/// </summary>
/// <param name="strKey">指定key的值</param>
/// <returns></returns>
public string ReadStr(string strKey)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase();
return db.StringGet(strKey);
}
/// <summary>
/// 获取指定 key 的值(指定第一个库)
/// </summary>
/// <param name="strKey">指定key的值</param>
/// <returns></returns>
public string ReadStrByDatabaseOne(string strKey)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase(1);
return db.StringGet(strKey);
}
/// <summary>
/// 获取指定 key 的值(指定第一个库)
/// </summary>
/// <param name="strKey">指定key的值</param>
/// <param name="database"></param>
/// <returns></returns>
public string ReadStrByDatabase(string strKey, int database)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase(database);
return db.StringGet(strKey);
}
/// <summary>
/// 删除指定key的值(默认第0个库)
/// </summary>
/// <param name="strKey">指定key的值</param>
public static bool DeleteStr(string strKey)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase();
return db.KeyDelete(strKey);
}
/// <summary>
/// 删除指定key的值(默认第0个库)
/// </summary>
/// <param name="strKey">指定key的值</param>
/// <param name="database">指定库的值</param>
public static bool DeleteStrByDatabase(string strKey, int database)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase(database);
return db.KeyDelete(strKey);
}
public bool Exist(string strKey)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase();
return db.KeyExists(strKey);
}
#endregion
#region Hash类型操作
public static void InsertHash(string tablename, string strKey, string strValue)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase();
bool setResult = db.HashSet(tablename, strKey, strValue);
}
public static string ReadHash(string tablename, string strKey)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase();
return db.HashGet(tablename, strKey);
}
public static bool ExistHash(string tablename, string strKey)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase();
return db.HashExists(tablename, strKey);
}
public static void DeleteHash(string tablename, string strKey)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase();
//set get
bool setResult = db.HashDelete(tablename, strKey);
}
#endregion
}
}
后期继续完善下