C#使用Redis的基本操作
作者:请叫我小冯哥哥 发布时间:2023-12-03 11:03:51
标签: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();
}
}
}
}
完活,下面是运行后的结果
以上所述是小编给大家介绍的C#使用Redis的基本操作网站的支持!
来源:http://www.cnblogs.com/f-z-h/archive/2017/06/26/7080404.html


猜你喜欢
- 本文实例为大家分享了java仿windows记事本小程序的具体代码,供大家参考,具体内容如下import java.awt.Checkbox
- 使用Inten的putExtra传递第一个Activity中//创建意图对象 Intent intent = new Intent(this
- 前言日志模块是每个项目中必须的,用来记录程序运行中的相关信息。一般在开发环境下使用DEBUG级别的日志输出,为了方便查看问题,而在线上一般都
- 请求网络数据是在安卓开发中使用最频繁的一个功能,网络请求的体验决定了用户对整个APP的感觉,因此合理地使用缓存对网络请求的数据进行处理极为重
- 什么是AppWidget?AppWidget就是我们平常在桌面上见到的那种一个个的小窗口,利用这个小窗口可以给用户提供一些方便快捷的操作。
- 具体代码如下所示;<!-- 第一种打包方式 (maven-jar-plugin), 将依赖包和配置文件放到jar包外 -->&l
- 目录概述语法索引器(Indexer)的用途重载索引器(Indexer)概述索引器(Indexer) 允许一个对象可以像数组一样使用下标的方式
- 本文实例讲述了C#采用递归实现阶乘的方法,供大家参考之用。通常来说,如果想实现一个阶乘,比如6 * 5 * 4 * 3 * 2 * 1,首先
- 前言对于信息的加密方式多种多样,之前为大家介绍了一种自己设计的加密方式,有兴趣的朋友可以欣赏一下,欢迎给予指点。今天为大家介绍一下对称加密方
- import java.io.BufferedInputStream;import java.io.BufferedOutputStream
- 从SD卡中获取图片资源,或者拍一张新的图片。 先贴代码 获取图片: 注释:拍照获取的话,可以指定图片的保存地址,在此不说明。 CharSeq
- 基本操作import org.apache.hadoop.conf.Configuration;import org.apache.hado
- 前言通过上一章的学习, 我们了解了Server启动的大致流程, 有很多组件与模块并没有细讲, 从这个章开始, 我们开始详细剖析netty的各
- Android MediaPlayer实现音乐播放器1、布局文件<?xml version="1.0" encod
- BroadcastReceiver不仅可以接收系统广播,也可接收自定义的广播 1.定义一个广播 * public class My
- 1. selectKey标签查询DDLCREATE TABLE `luck_reward_info` ( `id` int NO
- 每日一笑下班和实习生一起回家,公交站等车,一乞丐把碗推向实习生乞讨。这时,实习生不慌不忙的说了句:“我不要你的钱,你这钱
- 概念Java中的集合就是一种容器,可以容纳不同种类的数据,这些容纳是建立在未知的基础上。优点1.可以动态保存任意多个对象,使用比较方便。2.
- 快速排序------------------------------------------------------------------
- 1、 WIFI网卡的状态WIFI网卡的状态信息都以整型变量的形式存放在 android.net.wifi.WifiManager 类中,有以