首页 技术 正文
技术 2022年11月18日
0 收藏 466 点赞 3,858 浏览 1803 个字

这篇文章我将介绍如果用最简洁的方式配置Redis Server,

以及如何使用C#和它交互编程

一. 背景介绍

Redis是最快的key-value分布式缓存之一

缺点: 没有本地数据缓冲, 目前还没有完整的数据聚集化支持

优点: 配置简单, 使用方便, 高性能,支持不同的数据类型(hashes, lists, sets, sorted sets)

ASP.NET WebUI for viewing content of the cache

二. 安装Redis

1) 从github下载最新的32/64位安装

https://github.com/dmajkic/redis/downloads

解压到你自己的目录

eg: d:\RedisServer

2) 从github下载Redis服务程序

dll手工版

https://github.com/kcherenkov/redis-windows-service/downloads

安装版

https://github.com/rgl/redis/downloads

拷贝到RedisServer的安装目录

eg: d:\RedisServer

3) 安装redis服务

进入你的应用程序目录,运行下面的命令

sc create %name% binpath= “\”%binpath%\” %configpath%” start= “auto” DisplayName= “Redis”

%name% — name of service instance, ex. redis-instance;

%binpath% — path to this project exe file, ex. C:\Program Files\Redis\RedisService_1.1.exe;

%configpath% — path to redis configuration file, ex. C:\Program Files\Redis\redis.conf;

sc create my-redis binpath= “\”D:\RedisServer\RedisService_1.1.exe\”  D:\RedisServer\redis.conf” start= “auto” DisplayName= “MyRedis”

C# Redis Server分布式缓存编程(一)

C# Redis Server分布式缓存编程(一)

C# Redis Server分布式缓存编程(一)

4) 基本配置

redis.conf

# requirepass foobared

去掉注释,重启服务

这样实例化一个Redis服务的时候,就需要密码

RedisClient client = new RedisClient(serverHost, port, redisPassword);

Redis server replication (master – slave配置)

# slaveof <masterip> <masterport>

eg:

slaveof 192.168.1.1 6379

三. 客户端编程

1) 安装Redis包

C# Redis Server分布式缓存编程(一)

2) 简单例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;
using System.Threading;namespace Zeus.Cache.Redis.Demo
{
public class SimpleRedisDemo
{
public void SimpleDemo()
{
string host = "localhost";
string elementKey = "testKeyRedis"; using (RedisClient redisClient = new RedisClient(host))
{
if (redisClient.Get<string>(elementKey) == null)
{
// adding delay to see the difference
Thread.Sleep();
// save value in cache
redisClient.Set(elementKey, "default value");
} //change the value
redisClient.Set(elementKey, "fuck you value"); // get value from the cache by key
string message = "Item value is: " + redisClient.Get<string>(elementKey); Console.WriteLine(message);
}
}
}
}

运行结果:

C# Redis Server分布式缓存编程(一)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919