C# Redis Server分布式缓存编程 --网络转载
这篇文章我将介绍如果用最简洁的方式配置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"
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包
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(2000);
// 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);
}
}
}
}
运行结果:
最新文章
- SQL Server 内存和Paging
- JAVA设计模式之合成模式
- ExtJS组件的xtype属性列表
- Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
- [MODx] 2. Install some useful packages into ur MODx
- OpenJudge/Poj 1163 The Triangle
- Decode Ways -- LeetCode
- springMVC如何访问静态文件
- vue.js使用props在父子组件之间传参
- 无限大地图:lightmap拆分
- ES2018正则表达式更新
- activiti 基础
- DevExpress GridControl使用方法总结2
- vpnbook.py
- 调用oracle 各种报错总结---待续
- 【PL/SQL编程】变量和常量
- nest 排序
- Mobile IP
- Visual Studio for Mac 安装时无法连接到网络等问题
- log4j日志的配置--Debug
热门文章
- 如何使用.NET开发全版本支持的Outlook插件产品(二)&mdash;&mdash;完善插件
- JavaScript toFixed 用法
- spring boot 跨域访问处理
- Python开发入门与实战13-基于模板的界面
- MVC 实体字段自定义验证
- jpa遇到的 org.hibernate.PersistentObjectException: detached entity passed to persist异常
- 【转】一名大学生的PHP进阶之路
- 黑马程序员——【Java高新技术】——类加载器
- NSOperation实现线程间通信
- PHP中FOREACH()用法