您好,登錄后才能下訂單哦!
這篇文章主要講解了“redis數(shù)據(jù)庫(kù)的簡(jiǎn)單用法”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“redis數(shù)據(jù)庫(kù)的簡(jiǎn)單用法”吧!
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>
/// 連接對(duì)象
/// </summary>
private volatile IConnectionMultiplexer _connection;
/// <summary>
/// 數(shù)據(jù)庫(kù)
/// </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>
/// 獲取數(shù)據(jù)庫(kù)
/// </summary>
/// <param name="db"></param>
/// <returns></returns>
public IDatabase GetDatabase(int? db = null)
{
//return _connection.GetDatabase(db ?? -1);
return GetConnection().GetDatabase(db ?? -1);
}
/// <summary>
/// 設(shè)置
/// </summary>
/// <param name="key">鍵</param>
/// <param name="data">值</param>
/// <param name="cacheTime">時(shí)間</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>
/// 根據(jù)鍵獲取值
/// </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>
/// 判斷是否已經(jīng)設(shè)置
/// </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數(shù)據(jù)庫(kù)的簡(jiǎn)單用法”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)redis數(shù)據(jù)庫(kù)的簡(jiǎn)單用法這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。