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 = , // “写”链接池链接数
MaxReadPoolSize = , // “读”链接池链接数
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", );
//保存到硬盘
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的基本操作的更多相关文章
- java对redis的基本操作,ZZ
java对redis的基本操作 http://www.cnblogs.com/edisonfeng/p/3571870.html
- python下redis的基本操作:
1. 基本操作: >>> import redis >>> print redis.__file__ /usr/local/lib/python2.7/dist-p ...
- Redis 的基本操作、Key的操作及命名规范
Redis基本操作 查看数据的状态 pong redis 给我们返回 PONG,表示 redis 服务 运行正常 redis 默认用 使用 16 个 库 • Redis 默认使用 16 个库,从 0 ...
- java对redis的基本操作
一.server端安装 1.下载 https://github.com/MSOpenTech/redis 可看到当前可下载版本:redis2.6
- java对redis的基本操作(转)
本文转自:http://www.cnblogs.com/edisonfeng/p/3571870.html 2.主要类 1)功能类 package com.redis; import java.uti ...
- Redis缓存 ava-Jedis操作Redis,基本操作以及 实现对象保存
源代码下载: http://download.csdn.net/detail/jiangtao_st/7623113 1.Maven配置 <dependency> <groupId& ...
- redis - java 基本操作
import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; im ...
- Redis缓存系统(一)Java-Jedis操作Redis,基本操作以及 实现对象保存
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/jiangtao_st/article/details/37699473 源码下载: http://d ...
- java 连接redis 以及基本操作
一.首先下载安装redis 二.项目搭建 1.搭建一个maven 工程 2. 在pom.xml文件的dependencies节点下增加如下内容: <!-- resis --> <de ...
- java对redis的基本操作(初识)
一.server端安装 1.下载 https://github.com/MSOpenTech/redis 可看到当前可下载版本:redis2.6
随机推荐
- 花了好几个小时的奇葩Mat为0问题
问题 1. Mat mserMat = adaptive_image_from_points(contour, rect); CCharacter character; character.setCh ...
- [C++] CONST 2
The C++ 'const' Declaration: Why & How The 'const' system is one of the really messy features of ...
- [GO]空接口
package main import "fmt" //空接口的实际意义就在于在使用函数时,空接口可以接收任意类型的值,类似于python中的*args, **kwargs fun ...
- NBA常识 位置的划分 足球:越位等于抢跑
篮球:1号位——组织后卫(控球,组织)2号位——得分后卫(中远投篮,突破)3号位-----小前锋(突破,中远投篮)4号位——大前锋(二中锋,篮板,背身单打,禁区防守)5号位——中锋(篮板.背身单打,禁 ...
- 使用word 2007 发布csdn博客
目前大部分的博客作者在写博客这件事情上都会遇到以下3个痛点:1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.2.发布到博客或公众号平台 ...
- Promise实现简易AMD加载器
在最新的Chrome和FF中已经 实现了Promise.有了Promise我们用数行代码即可实现一个简易AMD模式的加载器 var registry = { promises: { }, resolv ...
- C# Timer 用法
System.Timers.Timer,通过.NET Thread Pool实现的,轻量,计时精确,对应用程序.消息没有特别的要求. using Timer = System.Timers.Tim ...
- IntelliJ OpenCV 开发环境搭建
Windows下的IntelliJ + OpenCV开发环境搭建 基于IntelliJ IDEA 15 和 OpenCV 3.1.0 1. 在OpenCV官网下载OpenCV安装程序,双击解压到目标目 ...
- hadoop1.2.1+hbase0.94.11+nutch2.2.1+elasticsearch0.90.5安装配置攻略
一.背景 最近由于项目和论文的需要,需要搭建一个垂直搜索的环境,查阅了很多资料,决定使用Apache的一套解决方案hadoop+hbase+nutch+es.这几样神器的作用就不多作介绍了,自行参考各 ...
- uwsgi启动报错WARNING: Can't find section "uwsgi" in INI configuration file autotestsite_uwsgi.ini
提示配置文件头部找不到 [uwsgi] 解决: 在最上方加上[uwsgi],有时候明明有标识,但是还是提示,那就再加一个