C#使用Redis的基本操作
一,引入dll
1.ServiceStack.Common.dll
2.ServiceStack.Interfaces.dll
3.ServiceStack.Redis.dll
4.ServiceStack.Text.dll
二,修改配置文件
在你的配置文件中加入如下的代码:
<appSettings> <add key="RedisPath" value="127.0.0.1:6379"/> todo:这里配置自己redis的ip地址和端口号 </appSettings>
二,用到的工具类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ServiceStack.Redis; namespace RedisDemo { /// <summary> /// RedisManager类主要是创建链接池管理对象的 /// </summary> public class RedisManager { /// <summary> /// redis配置文件信息 /// </summary> private static string RedisPath = System.Configuration.ConfigurationSettings.AppSettings["RedisPath"]; private static PooledRedisClientManager _prcm; /// <summary> /// 静态构造方法,初始化链接池管理对象 /// </summary> static RedisManager() { CreateManager(); } /// <summary> /// 创建链接池管理对象 /// </summary> private static void CreateManager() { _prcm = CreateManager(new string[] { RedisPath }, new string[] { RedisPath }); } private static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts) { //WriteServerList:可写的Redis链接地址。 //ReadServerList:可读的Redis链接地址。 //MaxWritePoolSize:最大写链接数。 //MaxReadPoolSize:最大读链接数。 //AutoStart:自动重启。 //LocalCacheTime:本地缓存到期时间,单位:秒。 //RecordeLog:是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项。 //RedisConfigInfo类是记录redis连接信息,此信息和配置文件中的RedisConfig相呼应 // 支持读写分离,均衡负载 return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig { MaxWritePoolSize = 5, // “写”链接池链接数 MaxReadPoolSize = 5, // “读”链接池链接数 AutoStart = true, }); } private static IEnumerable<string> SplitString(string strSource, string split) { return strSource.Split(split.ToArray()); } /// <summary> /// 客户端缓存操作对象 /// </summary> public static IRedisClient GetClient() { if (_prcm == null) { CreateManager(); } return _prcm.GetClient(); } } }
三,main方法执行存储操作与读取操作
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ServiceStack.Redis; using ServiceStack.Redis.Support; namespace RedisDemo { class Program { static void Main(string[] args) { try { //获取Redis操作接口 IRedisClient Redis = RedisManager.GetClient(); //放入内存 Redis.Set<string>("my_name", "小张"); Redis.Set<int>("my_age", 12); //保存到硬盘 Redis.Save(); //释放内存 Redis.Dispose(); //取出数据 Console.WriteLine("取出刚才存进去的数据 \r\n 我的Name:{0}; 我的Age:{1}.", Redis.Get<string>("my_name"), Redis.Get<int>("my_age")); Console.ReadKey(); } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); Console.ReadKey(); } } } }
完活,下面是运行后的结果
分类:
01-c#基础
标签:
Cache/Redis缓存
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App