Redis快取伺服器在Windows下的使用
一、Redis服務端
先下載Redis伺服器,點擊前往下載.msi版本,雙擊安裝Redis服務端就有了,並以服務的形式隨系統一起啟動:
安裝好Redis伺服器之後第一件事就是設定密碼,進入安裝目錄:C:Program FilesRedis - 找到設定檔:redis.windows-service.conf - 找到:# requirepass foobared - 回車換行加上:requirepass 這裡寫自己的新密碼(頂行寫,前面不要留空格) - 到服務裡重啟Redis服務,或是重啟電腦
不設定密碼的壞處,看看攜程這哥們的遭遇就知道了:記一次Redis被攻擊的事件
二、Redis客戶端(命令列與視覺化工具RDM)
命令列方式示範:啟動Redis客戶端、讀取與寫入Redis伺服器
上圖指令解釋:
:命令進入Redis安裝目錄,相當於Windows系統裡雙擊進入Redis的安裝目錄redis-cli.exe:打開redis-cli客戶端程序,相當於Windows系統裡雙擊運行一個exe程序(安裝了上面的Redis服務端程序,需要一個客戶端程式連接這個服務端。keys *:查看所有鍵值對(如果Redis伺服器設定了密碼,這條指令會報錯,需要先輸入密碼,執行此指令:auth 你的密碼)
左側樹顯示已經有一個連接了,點擊底部的Connect to Redis Server再增加一個連線:
三、C#操作Redis伺服器
以上都是命令列和視覺化工具操作Redis伺服器,C#程式操作Redis需要藉助StackExchange.Redis(https://github.com/StackExchange/StackExchange.Redis),為了統一調用,封裝了一個RedisHelper幫助類別:
using Newtonsoft.Json; using StackExchange.Redis; using System; using System.ComponentModel; using System.Configuration; using System.Reflection; using System.Text; namespace redis_Demo { /// <summary> /// Redis 帮助类 /// </summary> public static class RedisHelper { private static string _conn = ConfigurationManager.AppSettings["redis_connection_string"] ?? "127.0.0.1:6379"; private static string _pwd = ConfigurationManager.AppSettings["redis_connection_pwd"] ?? "123456"; static ConnectionMultiplexer _redis; static readonly object _locker = new object(); #region 单例模式 public static ConnectionMultiplexer Manager { get { if (_redis == null) { lock (_locker) { if (_redis != null) return _redis; _redis = GetManager(); return _redis; } } return _redis; } } private static ConnectionMultiplexer GetManager(string connectionString = null) { if (string.IsNullOrEmpty(connectionString)) { connectionString = _conn; } var options = ConfigurationOptions.Parse(connectionString); options.Password = _pwd; return ConnectionMultiplexer.Connect(options); } #endregion /// <summary> /// 添加 /// </summary> /// <param name="folder">目录</param> /// <param name="key">键</param> /// <param name="value">值</param> /// <param name="expireMinutes">过期时间,单位:分钟。默认600分钟</param> /// <param name="db">库,默认第一个。0~15共16个库</param> /// <returns></returns> public static bool StringSet(CacheFolderEnum folder, string key, string value, int expireMinutes = 600, int db = -1) { string fd = GetDescription(folder); return Manager.GetDatabase(db).StringSet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, value, TimeSpan.FromMinutes(expireMinutes)); } /// <summary> /// 获取 /// </summary> /// <param name="folder">目录</param> /// <param name="key">键</param> /// <param name="db">库,默认第一个。0~15共16个库</param> /// <returns></returns> public static string StringGet(CacheFolderEnum folder, string key, int db = -1) { try { string fd = GetDescription(folder); return Manager.GetDatabase(db).StringGet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key); } catch (Exception) { return null; } } /// <summary> /// 删除 /// </summary> /// <param name="folder">目录</param> /// <param name="key">键</param> /// <param name="db">库,默认第一个。0~15共16个库</param> /// <returns></returns> public static bool StringRemove(CacheFolderEnum folder, string key, int db = -1) { try { string fd = GetDescription(folder); return Manager.GetDatabase(db).KeyDelete(string.IsNullOrEmpty(fd) ? key : fd + ":" + key); } catch (Exception) { return false; } } /// <summary> /// 是否存在 /// </summary> /// <param name="key">键</param> /// <param name="db">库,默认第一个。0~15共16个库</param> public static bool KeyExists(CacheFolderEnum folder, string key, int db = -1) { try { string fd = GetDescription(folder); return Manager.GetDatabase(db).KeyExists(string.IsNullOrEmpty(fd) ? key : fd + ":" + key); } catch (Exception) { return false; } } /// <summary> /// 延期 /// </summary> /// <param name="folder">目录</param> /// <param name="key">键</param> /// <param name="min">延长时间,单位:分钟,默认600分钟</param> /// <param name="db">库,默认第一个。0~15共16个库</param> public static bool AddExpire(CacheFolderEnum folder, string key, int min = 600, int db = -1) { try { string fd = GetDescription(folder); return Manager.GetDatabase(db).KeyExpire(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, DateTime.Now.AddMinutes(min)); } catch (Exception) { return false; } } /// <summary> /// 添加实体 /// </summary> /// <param name="folder">目录</param> /// <param name="key">键</param> /// <param name="t">实体</param> /// <param name="ts">延长时间,单位:分钟,默认600分钟</param> /// <param name="db">库,默认第一个。0~15共16个库</param> public static bool Set<T>(CacheFolderEnum folder, string key, T t, int expireMinutes = 600, int db = -1) { string fd = GetDescription(folder); var str = JsonConvert.SerializeObject(t); return Manager.GetDatabase(db).StringSet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, str, TimeSpan.FromMinutes(expireMinutes)); } /// <summary> /// 获取实体 /// </summary> /// <param name="folder">目录</param> /// <param name="key">键</param> /// <param name="db">库,默认第一个。0~15共16个库</param> public static T Get<T>(CacheFolderEnum folder, string key, int db = -1) where T : class { string fd = GetDescription(folder); var strValue = Manager.GetDatabase(db).StringGet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key); return string.IsNullOrEmpty(strValue) ? null : JsonConvert.DeserializeObject<T>(strValue); } /// <summary> /// 获得枚举的Description /// </summary> /// <param name="value">枚举值</param> /// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用</param> /// <returns>枚举的Description</returns> private static string GetDescription(this Enum value, Boolean nameInstead = true) { Type type = value.GetType(); string name = Enum.GetName(type, value); if (name == null) { return null; } FieldInfo field = type.GetField(name); DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; if (attribute == null && nameInstead == true) { return name; } return attribute == null ? null : attribute.Description; } } }
向redis伺服器第一個庫的fd1目錄裡,添加一個鍵為name,值為wangjie的記錄:
RedisHelper.StringSet(CacheFolderEnum.Folder1, "name", "wangjie");
取得這條記錄:
string key = RedisHelper.StringGet(CacheFolderEnum.Folder1, "name"); Console.WriteLine("键为name的记录对应的值:" + key);
刪除這條記錄:
bool result = RedisHelper.StringRemove(CacheFolderEnum.Folder1, "name"); if (result) { Console.WriteLine("键为name的记录删除成功"); } else { Console.WriteLine("键为name的记录删除失败"); }
查詢這條記錄是否存在:
bool ifExist = RedisHelper.KeyExists(CacheFolderEnum.Folder1, "name"); if (ifExist) { Console.WriteLine("键为name的记录存在"); } else { Console.WriteLine("键为name的记录不存在"); }
向redis伺服器第二個函式庫的fd2目錄裡,加上一個鍵為sd1,值為一個物件的記錄:
Student student = new Student() { Id = 1, Name = "张三", Class = "三年二班" }; RedisHelper.Set<Student>(CacheFolderEnum.Folder2, "sd1", student, 10, 1);
取得這個物件:
Student sdGet = RedisHelper.Get<Student>(CacheFolderEnum.Folder2, "sd1", 1); if (sdGet != null) { Console.WriteLine("Id:" + sdGet.Id + " Name:" + sdGet.Name + " Class:" + sdGet.Class); } else { Console.WriteLine("找不到键为sd1的记录"); }
源碼: (http://files.cnblogs.com/files/oppoic/redis_Demo.zip)
環境是VS 2013,如果跑不起來自行把cs裡的程式碼拷出編譯運行
四、其他
四、其他 🎜四、其他🎜 MSOpenTech開發Redis快取伺服器自帶持久化,寫入之後重啟電腦鍵值對還存在,一般寫入鍵值對要設定過期時間,否則一直佔用記憶體不會被釋放。 Redis儲存方式不光有鍵對應字串,還有對應List,HashTable等,當然Redis更多高階的用法還是在Linux下。 🎜🎜更多Redis快取伺服器在Windows下的使用相關文章請關注PHP中文網! 🎜
熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Redis緩存方案如何實現產品排行榜列表的需求?在開發過程中,我們常常需要處理排行榜的需求,例如展示一個�...

在 Sublime 中運行代碼的方法有六種:通過熱鍵、菜單、構建系統、命令行、設置默認構建系統和自定義構建命令,並可通過右鍵單擊項目/文件運行單個文件/項目,構建系統可用性取決於 Sublime Text 的安裝情況。

Laravel 8 針對性能優化提供了以下選項:緩存配置:使用 Redis 緩存驅動、緩存門面、緩存視圖和頁面片段。數據庫優化:建立索引、使用查詢範圍、使用 Eloquent 關係。 JavaScript 和 CSS 優化:使用版本控制、合併和縮小資產、使用 CDN。代碼優化:使用 Composer 安裝包、使用 Laravel 助手函數、遵循 PSR 標準。監控和分析:使用 Laravel Scout、使用 Telescope、監控應用程序指標。

要安裝 Laravel,需依序進行以下步驟:安裝 Composer(適用於 macOS/Linux 和 Windows)安裝 Laravel 安裝器創建新項目啟動服務訪問應用程序(網址:http://127.0.0.1:8000)設置數據庫連接(如果需要)

在Laravel開發中,處理複雜的模型關係一直是個挑戰,特別是當涉及到多層級的BelongsToThrough關係時。最近,我在處理一個多級模型關係的項目中遇到了這個問題,傳統的HasManyThrough關係無法滿足需求,導致數據查詢變得複雜且低效。經過一番探索,我找到了staudenmeir/belongs-to-through這個庫,它通過Composer輕鬆安裝並解決了我的困擾。

SpringBoot中使用Redis緩存OAuth2Authorization對像在SpringBoot應用中,使用SpringSecurityOAuth2AuthorizationServer...

Redis在數據存儲和管理中扮演著關鍵角色,通過其多種數據結構和持久化機製成為現代應用的核心。 1)Redis支持字符串、列表、集合、有序集合和哈希表等數據結構,適用於緩存和復雜業務邏輯。 2)通過RDB和AOF兩種持久化方式,Redis確保數據的可靠存儲和快速恢復。
