C# Redis的操作
2018-06-17 19:59:25来源:未知 阅读 ()
Nuget添加StackExchange.Redis的引用
由于Redis封装类同时使用了Json,需要添加JSON.NET引用(Newtonsoft.Json)
Redis封装类
/// <summary>
/// Redis 操作类
/// </summary>
public class RedisHelper
{
/// <summary>
/// 连接字符串
/// </summary>
private static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["RedisConnectionString"].ConnectionString;
/// <summary>
/// 锁
/// </summary>
private readonly object _lock = new object();
/// <summary>
/// 连接对象
/// </summary>
private volatile IConnectionMultiplexer _connection;
/// <summary>
/// 数据库
/// </summary>
private IDatabase _db;
public RedisHelper()
{
_connection = ConnectionMultiplexer.Connect(ConnectionString);
_db = GetDatabase();
}
/// <summary>
/// 获取连接
/// </summary>
/// <returns></returns>
protected IConnectionMultiplexer GetConnection()
{
if (_connection != null && _connection.IsConnected)
{
return _connection;
}
lock (_lock)
{
if (_connection != null && _connection.IsConnected)
{
return _connection;
}
if (_connection != null)
{
_connection.Dispose();
}
_connection = ConnectionMultiplexer.Connect(ConnectionString);
}
return _connection;
}
/// <summary>
/// 获取数据库
/// </summary>
/// <param name="db"></param>
/// <returns></returns>
public IDatabase GetDatabase(int? db = null)
{
return GetConnection().GetDatabase(db ?? -1);
}
/// <summary>
/// 设置
/// </summary>
/// <param name="key">键</param>
/// <param name="data">值</param>
/// <param name="cacheTime">时间</param>
public virtual void Set(string key, object data, int cacheTime)
{
if (data == null)
{
return;
}
var entryBytes = Serialize(data);
var expiresIn = TimeSpan.FromMinutes(cacheTime);
_db.StringSet(key, entryBytes, expiresIn);
}
/// <summary>
/// 根据键获取值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public virtual T Get<T>(string key)
{
var rValue = _db.StringGet(key);
if (!rValue.HasValue)
{
return default(T);
}
var result = Deserialize<T>(rValue);
return result;
}
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="serializedObject"></param>
/// <returns></returns>
protected virtual T Deserialize<T>(byte[] serializedObject)
{
if (serializedObject == null)
{
return default(T);
}
var json = Encoding.UTF8.GetString(serializedObject);
return JsonConvert.DeserializeObject<T>(json);
}
/// <summary>
/// 判断是否已经设置
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public virtual bool IsSet(string key)
{
return _db.KeyExists(key);
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="data"></param>
/// <returns>byte[]</returns>
private byte[] Serialize(object data)
{
var json = JsonConvert.SerializeObject(data);
return Encoding.UTF8.GetBytes(json);
}
}
控制台测试代码
var s = "a";
new RedisHelper().Set("abc", s, 10);
Console.WriteLine("ok");
Console.ReadKey();
运行,查看数据库
获取值:
//var s = "a";
Console.WriteLine(new RedisHelper().Get<string>("abc"));
//Console.WriteLine("ok");
Console.ReadKey();
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- PythonDay08 2019-08-13
- 列表和元组 2019-08-13
- python Django基础操作 2019-07-24
- xadmin进行全局配置(修改模块名为中文以及其他自定义的操作 2019-07-24
- python连接redis 2019-07-24
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash