您好,登录后才能下订单哦!
这篇文章主要讲解了“redis数据库的简单用法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“redis数据库的简单用法”吧!
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp
{
/// <summary>
/// Redis 操作类
/// </summary>
public class RedisHelper
{
/// <summary>
/// 连接字符串
/// </summary>
private static readonly string ConnectionString = ConfigurationManager.AppSettings["RedisConnectionString"];
/// <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 _connection.GetDatabase(db ?? -1);
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);
}
}
}
<appSettings>
<add key="RedisConnectionString" value="127.0.0.1:6379"></add>
</appSettings>
感谢各位的阅读,以上就是“redis数据库的简单用法”的内容了,经过本文的学习后,相信大家对redis数据库的简单用法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。