1. 程式人生 > >C# Redis的操作

C# Redis的操作

ret exchange readonly aac new dispose null amp byte

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); } }
View Code

控制臺測試代碼

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();

技術分享

C# Redis的操作