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
随机推荐
- linux系统文件的链接
一. 硬链接(实际链接) (以linux系统为例) 1. 文件的索引节点inode 假设我们在硬盘当前目录下建立了一个名为mytext文本文件,其内容只有一行:This is my file. (1) ...
- C++细节理解
1.为什么static类外初始化不需要static关键字 答:因为类外static变量或函数表示限定在此源文件中才能使用,而类中的static变量或函数表示由本类及其所有对象共享,如果在类外初始化或定 ...
- Linux发送邮件
以下是自己收集的实用Linux下简单配置外部邮箱发送邮件的方法: 1.配置/etc/mail.rc,使用mail命令 # vim /etc/mail.rc ###调用外部邮箱 set from=t ...
- 谈谈Vue.js——vue-resource全攻略
本篇文章主要介绍了谈谈Vue.js——vue-resource全攻略,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 概述 上一篇我们介绍了如何将$.ajax和Vue. ...
- 优秀前端工程师必备: (总结) 清除原生ios按钮样式
写移动端的web开发时, 需要清除IOS本身的各种样式: 1.消除ios按钮原生样式, 给按钮加自定义样式: input[type="button"], input[type=&q ...
- 在iOS中使用百度地图
就如同在百度地图的文档中所说的一样,这么来.但是,有一个小疏忽. 到添加完所需要的framework之后,一定要记得把你的(Class-Prefix)AppDelegate的后缀改成mm. 估计百度的 ...
- npm使用【转】
NPM是一个Node包管理和分发工具,已经成为了非官方的发布Node模块(包)的标准.有了NPM,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包.在安装nodeJS 安装包的时候 ...
- Windows装python
pycharm常用快捷键ctr+alt+shift+l可以快速格式化python安装下载地址https://www.python.org/downloads/release/python-365/ 一 ...
- ES 遇到的一个坑too_many_clauses: maxClauseCount
异常: Caused by: org.elasticsearch.common.io.stream.NotSerializableExceptionWrapper: too_many_clauses: ...
- CodeForces 620E New Year Tree(线段树的骚操作第二弹)
The New Year holidays are over, but Resha doesn't want to throw away the New Year tree. He invited h ...