C# Redis 帮助类
标签:param str stat exe options returns text int alt
总结下,后期会陆续更新
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
{
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;
///
/// 连接redis
///
///
public static ConnectionMultiplexer getRedisConn()
{
if (redisConn == null)
{
lock (Locker)
{
if (redisConn == null || !redisConn.IsConnected)
{
redisConn = ConnectionMultiplexer.Connect(configurationOptions);
}
}
}
return redisConn;
}
#region Redis 字符串操作
///
/// 设置一个key的值
///
///
///
public void InsertStr(string strKey, string strValue)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase();
//set get
bool setResult = db.StringSet(strKey, strValue);
}
///
/// 设置一个key的值并设定过期时间
///
/// key
/// values
/// 过期时间
///
public void InsertStr(string strKey, string strValue, DateTime expirytime, int databasenum)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase(databasenum);
bool setResult = db.StringSet(strKey, strValue);
db.KeyExpire(strKey, expirytime.ToUniversalTime());
}
///
/// 读取一个key值
///
///
///
public string ReadStr(string strKey)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase();
return db.StringGet(strKey);
}
///
/// 删除一个key值
///
///
public void DeleteStr(string strKey)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase();
//set get
bool setResult = db.KeyDelete(strKey);
}
///
/// 删除一个key值
///
///
///
///
public bool DeleteStr(string strKey, int databasenum)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase(databasenum);
return db.KeyDelete(strKey);
}
///
/// 判断key是否存在
///
///
///
public bool Exist(string strKey)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase();
return db.KeyExists(strKey);
}
#endregion
#region Redis哈希操作
///
/// 添加一个key值
///
///
///
///
public void InsertHash(string tablename, string strKey, string strValue)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase();
//set get
bool setResult = db.HashSet(tablename, strKey, strValue);
}
///
/// 读取一个key值
///
///
///
///
public string ReadHash(string tablename, string strKey)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase();
return db.HashGet(tablename, strKey);
}
///
/// 判断一个key值是否存在
///
///
///
///
public bool ExistHash(string tablename, string strKey)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase();
return db.HashExists(tablename, strKey);
}
///
///
///
///
///
public void DeleteHash(string tablename, string strKey)
{
redisConn = getRedisConn();
var db = redisConn.GetDatabase();
//set get
bool setResult = db.HashDelete(tablename,strKey);
}
#endregion
}
}
C# Redis 帮助类
标签:param str stat exe options returns text int alt
原文地址:https://www.cnblogs.com/for917157ever/p/14288307.html
评论