C#使用Redis的基本操作
作者:请叫我小冯哥哥 时间:2023-12-03 11:03:51
一,引入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();
}
}
}
}
完活,下面是运行后的结果
以上所述是小编给大家介绍的C#使用Redis的基本操作网站的支持!
来源:http://www.cnblogs.com/f-z-h/archive/2017/06/26/7080404.html
标签:c#,Redis
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
WPF实现文字粒子闪烁动画效果
2022-01-12 23:56:45
![](https://img.aspxhome.com/file/2023/4/102154_0s.gif)
C#操作ini文件的帮助类
2022-12-26 17:11:09
简单了解Spring中常用工具类
2021-09-24 10:44:28
一篇文章带你初步认识Maven
2023-08-31 01:56:14
![](https://img.aspxhome.com/file/2023/7/68007_0s.png)
C#中缓存System.Web.Caching用法总结
2021-09-05 04:41:11
Android实现圆角Button按钮
2022-02-04 10:05:45
![](https://img.aspxhome.com/file/2023/2/113282_0s.jpg)
实例详解C#正则表达式
2023-06-23 00:03:03
Java中Stream流中map和forEach的区别详解
2022-07-09 12:24:43
![](https://img.aspxhome.com/file/2023/8/108428_0s.jpg)
java 多态性详解及简单实例
2021-10-06 23:29:50
Cors实现java后端完全跨域实例
2022-01-04 15:03:33
Spring Boot项目维护全局json数据代码实例
2023-04-17 23:24:10
c#使用Unity粒子实现炮塔发射系统
2023-11-04 19:05:19
![](https://img.aspxhome.com/file/2023/2/92922_0s.png)
详解C# FileStream类
2022-10-19 13:44:27
JAVA WSIMPORT生成WEBSERVICE客户端401认证过程图解
2023-11-14 00:27:55
![](https://img.aspxhome.com/file/2023/5/58985_0s.png)
Java Socket 编程详解
2022-06-13 12:06:57
android客户端从服务器端获取json数据并解析的实现代码
2023-09-30 07:52:20
Kotlin协程概念原理与使用万字梳理
2023-03-28 21:29:32
![](https://img.aspxhome.com/file/2023/7/94287_0s.png)
C++ 实现求最大公约数和最小公倍数
2023-10-30 10:49:23
一文彻底搞懂Kotlin中的协程
2021-09-19 19:16:01
![](https://img.aspxhome.com/file/2023/3/123333_0s.png)
Java 中泛型 T 和 ? 的区别详解
2022-07-08 00:40:28
![](https://img.aspxhome.com/file/2023/8/64748_0s.png)