前言:lz自打工作以來第一次遇到電腦問題需要重裝系統,全盤格式化。打擊是沉痛的。特別傷。 然后需要重新在本地部署 redis。這是寫這篇博客的原因。希望對大家有所幫助,安裝資源和引用DLL可以引用 只用於學習 ( windows環境安裝 )
一:安裝Redis
首先我們需要在本地或是服務器安裝Redis。安裝包可以去redis官網下載,去Gethub上down 我這里直接給我的百度雲
鏈接:https://pan.baidu.com/s/1WJ48XMaHOmgb1J0KaQkdEw 密碼:xjf4 下載后解壓
本地的話 你可以直接打開 redis-server.exe這個應用程序。也可以打開cmd命令 然后把路徑換成你redis文件的路徑 我的是放在 c:\redis 可以跟我的來,方便搬運代碼 切換文件路徑 cd
然后我們啟動redis服務,運行 redis-server.exe redis.windows.conf
出現這個界面說明OK了,這個窗口別關 如果關閉了 重新來一遍
繼續,再打開一個cmd窗口,切換到redis目錄下面
這里解釋一下這個 127.0.0.1 是本地訪問,相當於localhost 6379是redis默認的端口
繼續,redis是存儲鍵值對的所以我們測試一下
set一個 key value
get 一個key
機制就是把數據set 到 redis的數據庫里,就是我們說的緩存 用的時候在get取值
接下來我們下載一個redis可視化工具,作用是相當於我們平時使用的數據庫 可以去官網上下載,可以我直接上資源,方便 鏈接:https://pan.baidu.com/s/1K7QtxSVV_15kxP8zkEvIng 密碼:k4f8
解壓文件,安裝成功 打開
字面意思,很簡單吧
name 測試階段隨便寫 HOST呢 就是主機IP地址 鏈接上之后 打開我們的DB
crtest666 wuchen 這個鍵值隊是成功添加了的 (關於redis key是名字無所謂 value可以是列表,集合,自由使用,今天先不講這個)
二:C# 中使用redis(查找,增加,刪除的使用)
首先我們需要添加redis的引用。nuget包 搜索redis第一個就是。--StackExchange.Redis
根據運行的環境不同。還需要以下dll
1.ServiceStack.Common.dll
2.ServiceStack.Interfaces.dll
3.ServiceStack.Text.dll
可以去Gethub上去down, https://github.com/ServiceStack/ServiceStack.Redis 真的慢
博主百度雲資源: 鏈接:https://pan.baidu.com/s/1gQLJlIcHhZtPikIgFHnHxA 密碼:scqt
Lg:本博一直用win10,按理說只需要這些DLL的 現在裝成win7,發現不行了 后來我在Nuget包中添加 RedisHelper,(現在好像直接叫做Redis了,看下備注說明確認一下)
這里舉個很簡單的使用redis的例子
添加一個控制台應用程序
然后在主程序類 program.cs 代碼
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace crtest { class Program { /// <summary> /// 基無塵 18-8-31 /// </summary> /// <param name="args"></param> static void Main(string[] args) { Console.WriteLine("Redis寫入緩存:wuchengtest"); //添加 RedisCacheHelper.Add("wuchen", "jiwuchen", DateTime.Now.AddDays(1)); Console.WriteLine("Redis獲取緩存:crtest666");//查找 string str3 = RedisCacheHelper.Get<string>("crtest666"); Console.WriteLine(str3); RedisCacheHelper.Remove("hellow");//刪除 string str = RedisCacheHelper.Get<string>("hellow");//查看是否刪除成功 Console.WriteLine(str == null ? "未找到" : str); Console.ReadKey(); } } }
然后添加一個幫助類 作用類似於我們常用的DbHelper
using ServiceStack.Redis; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace crtest { public class RedisCacheHelper { private static readonly PooledRedisClientManager pool = null; private static readonly string[] redisHosts = null; public static int RedisMaxReadPool = 3; public static int RedisMaxWritePool = 1; static RedisCacheHelper() { var redisHostStr = "127.0.0.1:6379"; if (!string.IsNullOrEmpty(redisHostStr)) { redisHosts = redisHostStr.Split(','); if (redisHosts.Length > 0) { pool = new PooledRedisClientManager(redisHosts, redisHosts, new RedisClientManagerConfig() { MaxWritePoolSize = RedisMaxWritePool, MaxReadPoolSize = RedisMaxReadPool, AutoStart = true }); } } } #region Add public static void Add<T>(string key, T value, DateTime expiry) { if (value == null) { return; } if (expiry <= DateTime.Now) { Remove(key); return; } try { if (pool != null) { using (var r = pool.GetClient()) { if (r != null) { r.SendTimeout = 1000; r.Set(key, value, expiry - DateTime.Now); } } } } catch (Exception ex) { string msg = string.Format("{0}:{1}發生異常!{2}", "cache", "存儲", key); } } public static void Add<T>(string key, T value, TimeSpan slidingExpiration) { if (value == null) { return; } if (slidingExpiration.TotalSeconds <= 0) { Remove(key); return; } try { if (pool != null) { using (var r = pool.GetClient()) { if (r != null) { r.SendTimeout = 1000; r.Set(key, value, slidingExpiration); } } } } catch (Exception ex) { string msg = string.Format("{0}:{1}發生異常!{2}", "cache", "存儲", key); } } public static T Get<T>(string key) { if (string.IsNullOrEmpty(key)) { return default(T); } T obj = default(T); try { if (pool != null) { using (var r = pool.GetClient()) { if (r != null) { r.SendTimeout = 1000; obj = r.Get<T>(key); } } } } catch (Exception ex) { string msg = string.Format("{0}:{1}發生異常!{2}", "cache", "獲取", key); } return obj; } #endregion public static void Remove(string key) { try { if (pool != null) { using (var r = pool.GetClient()) { if (r != null) { r.SendTimeout = 1000; r.Remove(key); } } } } catch (Exception ex) { string msg = string.Format("{0}:{1}發生異常!{2}", "cache", "刪除", key); } } public static bool Exists(string key) { try { if (pool != null) { using (var r = pool.GetClient()) { if (r != null) { r.SendTimeout = 1000; return r.ContainsKey(key); } } } } catch (Exception ex) { string msg = string.Format("{0}:{1}發生異常!{2}", "cache", "是否存在", key); } return false; } } }
OK,生成一下 然后運行看看效果
OK,我們去redis-server上看看效果,也可以用上面的控制台來get 看看效果(cmd命令窗口自始至終都是開着的)
可以看到是成功添加的。好啦
通過簡單的兩句代碼,慢慢發現編程的樂趣