Redis作为NoSQL数据库,在很多项目场景中使用到。本文主要讲解如何在dotNet Core的环境下使用Redis进行项目开发。
///
/// Redis 接口
///
public interface IRedisService
{
///
/// Redis get命令
///
/// 键
///
dynamic GetRedisValue(string key);
///
/// Redis set 命令
///
/// 键
/// 值
///
bool SetRedisValue(dynamic key, dynamic value);
///
/// Redis set 命令 设置过期时间 ttl
///
///
///
///
///
bool SetRedisValue(dynamic key, dynamic value, TimeSpan time);
///
/// Redis 获取字符串类型的key
///
/// 字符串类型的key
///
dynamic GetRedisStringValue(string key);
///
/// Redis 设置字符串类型的过期时间
///
///
///
///
///
bool SetRedisStringValue(string key, string value, TimeSpan time);
///
/// 删除key
///
///
///
bool RemoveRedisValue(string key);
///
/// 存储字符串key-value到指定的redis数据库中
///
///
///
///
///
bool SetRedisStringValueToSelectDb(string key, string value, int dbNumber = -1);
///
/// 存储字符串key-value到指定的redis数据库中
///
/// 键
/// 值
/// 数据库编号
/// 过期时间
///
bool SetRedisStringValueTimeSpanToSelectDb(string key, string value, TimeSpan time, int dbNumber = -1);
///
/// 指定数据库中获取指定key的value
///
/// 字符串类型键
/// 数据库编码 默认-1
///
dynamic GetRedisValueSelectDb(string key,int dbNumber = -1);
///
/// 指定数据库中删除指定key的value
///
/// 键
/// 数据库编码 默认-1
///
bool DeleteRedisValueSelectDb(string key, int dbNumber = -1);
///
/// 指定Redis数据库修改key的value
///
///
///
///
bool EditRedisStringValueSelectDb(string key, string value, int dbNumber = -1);
}
public class RedisServiceHelp : IRedisService
{
private string _redisConnection {get;set;}
public RedisServiceHelp()
{
_redisConnection = "192.168.2.**:6379,password=,allowadmin=true,ConnectTimeout=65535,syncTimeout=65535";
}
public dynamic GetRedisStringValue(string key)
{
dynamic ret = null;
try
{
using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(_redisConnection))
{
IDatabase db = redis.GetDatabase();
var newobj = db.StringGet(key, CommandFlags.None);
ret = Newtonsoft.Json.JsonConvert.DeserializeObject(newobj);
}
}
catch (Exception ex)
{
ret = null;
}
return ret;
}
public dynamic GetRedisValue(string key)
{
dynamic ret = null;
try
{
using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(_redisConnection))
{
IDatabase db = redis.GetDatabase();
var newobj = db.StringGet(key, CommandFlags.None);
ret = (dynamic)(newobj);
}
}
catch (Exception ex)
{
ret = null;
}
return ret;
}
public bool RemoveRedisValue(string key)
{
var ret = false;
try
{
using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(_redisConnection))
{
IDatabase db = redis.GetDatabase();
ret = db.KeyDelete(key);
}
}
catch (Exception ex)
{
}
return ret;
}
public bool SetRedisStringValue(string key, string value, TimeSpan time)
{
var ret = false;
try
{
using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(_redisConnection))
{
IDatabase db = redis.GetDatabase();
ret = db.StringSet(key, value, time);
}
}
catch (Exception ex)
{
}
return ret;
}
public bool SetRedisValue(dynamic key, dynamic value)
{
var ret = false;
try
{
using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(_redisConnection))
{
IDatabase db = redis.GetDatabase();
ret = db.StringSet(key, Newtonsoft.Json.JsonConvert.SerializeObject(value));
}
}
catch (Exception ex)
{
}
return ret;
}
public bool SetRedisValue(dynamic key, dynamic value, TimeSpan time)
{
var ret = false;
try
{
using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(_redisConnection))
{
IDatabase db = redis.GetDatabase();
ret = db.StringSet(key, Newtonsoft.Json.JsonConvert.SerializeObject(value), time);
}
}
catch (Exception ex)
{
}
return ret;
}
public bool SetRedisStringValueToSelectDb(string key, string value, int dbNumber = -1)
{
var ret = false;
try
{
using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(_redisConnection))
{
IDatabase db = redis.GetDatabase(dbNumber);
ret = db.StringSet(key, Newtonsoft.Json.JsonConvert.SerializeObject(value));
}
}
catch (Exception)
{
throw;
}
return ret;
}
public bool SetRedisStringValueTimeSpanToSelectDb(string key, string value, TimeSpan time, int dbNumber = -1)
{
var ret = false;
try
{
using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(_redisConnection))
{
IDatabase db = redis.GetDatabase(dbNumber);
ret = db.StringSet(key, Newtonsoft.Json.JsonConvert.SerializeObject(value), time);
}
}
catch (Exception)
{
throw;
}
return ret;
}
public dynamic GetRedisValueSelectDb(string key, int dbNumber = -1)
{
dynamic ret = null;
try
{
using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(_redisConnection))
{
IDatabase db = redis.GetDatabase(dbNumber);
var newobj = db.StringGet(key, CommandFlags.None);
ret = Newtonsoft.Json.JsonConvert.DeserializeObject(newobj);
}
}
catch (Exception)
{
ret = null;
}
return ret;
}
public bool DeleteRedisValueSelectDb(string key, int dbNumber = -1)
{
var ret = false;
try
{
using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(_redisConnection))
{
IDatabase db = redis.GetDatabase(dbNumber);
ret = db.KeyDelete(key);
}
}
catch (Exception)
{
ret = false;
}
return ret;
}
public bool EditRedisStringValueSelectDb(string key,string value, int dbNumber = -1)
{
var ret = false;
try
{
using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(_redisConnection))
{
IDatabase db = redis.GetDatabase(dbNumber);
ret = db.StringSet(key, Newtonsoft.Json.JsonConvert.SerializeObject(value));
}
}
catch (Exception)
{
throw;
}
return ret;
}
}