Redis在.net中的应用学习
在Redis的官网(http://redis.io/clients#c)上可以看到支持Redis C#的客户端。
redis的网络连接方式和传统的rdbms相似,一种是长连接,一种是连接池,此处使用长连接进行连接。
目前redis官方版本不支持.net直接进行连接,需要使用一些开源类库。目前最流行的就是ServiceStack.redis,可以通过https://github.com/ServiceStack/ServiceStack.Redis下载最新版本。
下载完成解压,在\ServiceStack.Redis-master\build\release\MonoDevelop目录下看到ServiceStack.Redis.zip文件,这个就是需要引入到.net项目中的4个dll文件。
测试时发现四个文件版本比较旧,可以通过编译redis源码,生成最新的dll文件。

打开VS2013,创建一个控制台应用程序,写了一些简单的Redis操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; using ServiceStack.Redis;
using ServiceStack.Redis.Support; namespace RedisDemo
{
class Program
{
private static RedisClient redis = new RedisClient("192.168.32.216", , "anny");
static void Main(string[] args)
{
//单个字符串写入
redis.SetValue("age", "");
//读取指定key的字符串
redis.GetValue("age"); //存储数字
redis.Set<int>("int_age", );
int age = redis.Get<int>("int_age");
Console.WriteLine("int_age={0}", age); //将字符串列表写入Redis List
List<string> colourList = new List<string>{"red","pink","green","blue","black","white"};
colourList.ForEach(item => redis.AddItemToList("colourList", item)); //读取Redis List内容
List<string> colourList1 = redis.GetAllItemsFromList("colourList");
colourList1.ForEach(item => Console.Write(item + " ")); //存储实体对象,在Redis中以json格式存储
UserInfo user = new UserInfo(){Id=, Name="Mark", Age=, City="ShangHai" };
redis.Set<UserInfo>("user_1", user);
UserInfo user1 = redis.Get<UserInfo>("user_1");
Console.WriteLine("id={0},name={1},age={2},city={3}", user1.Id, user1.Name, user1.Age, user1.City); //object序列化方式
var ser = new ObjectSerializer();
redis.Set<byte[]>("user1", ser.Serialize(user));
UserInfo user11 = ser.Deserialize(redis.Get<byte[]>("user1")) as UserInfo;
Console.WriteLine("id={0},name={1},age={2},city={3}", user1.Id, user1.Name, user1.Age, user1.City); //存储对象列表到redis中
List<UserInfo> userList = new List<UserInfo>{
new UserInfo{Id=, Name="Jack", Age=, City="beijing" },
new UserInfo{Id=, Name="Tom", Age=, City="XiaMen" }
}; redis.Set<byte[]>("userlist", ser.Serialize(userList));
List<UserInfo> userList1 = ser.Deserialize(redis.Get<byte[]>("userlist")) as List<UserInfo>;
userList1.ForEach(i =>
{
Console.WriteLine("id={0},name={1},age={2},city={3}", i.Id, i.Name, i.Age, i.City);
}); Console.Read();
} [Serializable]
class UserInfo
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; } public string City { get; set; }
}
}
}

Redis linux环境下查看:
root@ubuntu:/usr/local/redis/bin# ./redis-cli -a anny
127.0.0.1:6379> keys *
1) "age"
2) "colourList"
3) "int_age"
4) "user1"
5) "userlist"
6) "user_1"
127.0.0.1:6379> type colourList
list
127.0.0.1:6379> lrange colourList 0 -1
1) "red"
2) "pink"
3) "green"
4) "blue"
5) "black"
6) "white"
127.0.0.1:6379> type userlist
string
127.0.0.1:6379> get userlist
"\x00\x01\x00\x00\x00\xff\xff\xff\xff\x01\x00\x00\x00\x00\x00\x00\x00\x0c\x02\x00\x00\x00@RedisDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\x04\x01\x00\x00\x00\x81\x01System.Collections.Generic.List`1[[RedisDemo.Program+UserInfo, RedisDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]\x03\x00\x00\x00\x06_items\x05_size\b_version\x04\x00\x00\x1cRedisDemo.Program+UserInfo[]\x02\x00\x00\x00\b\b\t\x03\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\a\x03\x00\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00\x04\x1aRedisDemo.Program+UserInfo\x02\x00\x00\x00\t\x04\x00\x00\x00\t\x05\x00\x00\x00\r\x02\x05\x04\x00\x00\x00\x1aRedisDemo.Program+UserInfo\x04\x00\x00\x00\x13<Id>k__BackingField\x15<Name>k__BackingField\x14<Age>k__BackingField\x15<City>k__BackingField\x00\x01\x00\x01\b\b\x02\x00\x00\x00\x02\x00\x00\x00\x06\x06\x00\x00\x00\x04Jack\x1b\x00\x00\x00\x06\a\x00\x00\x00\abeijing\x01\x05\x00\x00\x00\x04\x00\x00\x00\x03\x00\x00\x00\x06\b\x00\x00\x00\x03Tom\x19\x00\x00\x00\x06\t\x00\x00\x00\x06XiaMen\x0b"
127.0.0.1:6379> type user_1
string
127.0.0.1:6379> get user_1
"{\"Id\":1,\"Name\":\"Mark\",\"Age\":32,\"City\":\"ShangHai\"}"
Redis在.net中的应用学习的更多相关文章
- REDIS源码中一些值得学习的技术细节02
Redis中散列函数的实现: Redis针对整数key和字符串key,采用了不同的散列函数 对于整数key,redis使用了 Thomas Wang的 32 bit Mix Function,实现了d ...
- REDIS源码中一些值得学习的技术细节01
redis.c/exitFromChild函数: void exitFromChild(int retcode) { #ifdef COVERAGE_TEST exit(retcode); #else ...
- Flink读写Redis(二)-flink-redis-connector代码学习
源码结构 RedisSink package org.apache.flink.streaming.connectors.redis; import org.apache.flink.configur ...
- Redis设置认证密码 Redis使用认证密码登录 在Redis集群中使用认证密码
Redis默认配置是不需要密码认证的,也就是说只要连接的Redis服务器的host和port正确,就可以连接使用.这在安全性上会有一定的问题,所以需要启用Redis的认证密码,增加Redis服务器的安 ...
- PHP中的Libevent学习
wangbin@2012,1,3 目录 Libevent在php中的应用学习 1. Libevent介绍 2. 为什么要学习libevent 3. Php libeven ...
- Redis 在新浪微博中的应用
Redis 在新浪微博中的应用 Redis简介 1. 支持5种数据结构 支持strings, hashes, lists, sets, sorted setsstring是很好的存储方式,用来做计数存 ...
- Redis在PHP中的基本使用案例
下载http://www.oschina.net/p/redis 解压后里面有:lib 源文件 .examples 例子.test测试 将lib目录拷贝到你的项目中,就可以开始你的predis操作了. ...
- JS中childNodes深入学习
原文:JS中childNodes深入学习 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <ti ...
- CNCC2017中的深度学习与跨媒体智能
CNCC2017中的深度学习与跨媒体智能 转载请注明作者:梦里茶 目录 机器学习与跨媒体智能 传统方法与深度学习 图像分割 小数据集下的深度学习 语音前沿技术 生成模型 基于贝叶斯的视觉信息编解码 珠 ...
随机推荐
- mybatis 关于传long 类型问题
@Datapublic class PrealertPackageStatusDTO { private Integer nowStatus; /** * packageStatusEnum */ p ...
- hdu 6020 MG loves apple 恶心模拟
题目链接:点击传送 MG loves apple Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Ja ...
- Codeforces Beta Round #16 div 2 C.Monitor最大公约数
C. Monitor time limit per test 0.5 second memory limit per test 64 megabytes input standard input ou ...
- python 文件分割
import sys,os def split(fromfile,todir,chunksize): partnum = inputfile = open(fromfile,'rb')#open th ...
- c++ 多继承 public
以下代码会报错 #include <iostream> using namespace std; class Sofa { public: Sofa(); ~Sofa(); void si ...
- Couldn't find an AF_INET address for
vim ~/.bashrc #输入内容 export ROS_HOSTNAME=你的hostname export ROS_MASTER_URI=http://(目标主机的IP):11311 expo ...
- Linux更改主机名
1.临时 # hostname newhostname 2.修改/etc/hostname文件,需重启 # vim /etc/hostname 3.查看 # hostname Ubuntu18 # h ...
- MongoDB(课时22 唯一索引)
3.6.1 唯一索引 唯一索引的主要目的是用在某一个字段上,使该字段的内容不重复. 范例:创建唯一索引 db.students.ensureIndex({"name" : 1}, ...
- Socket入门之前的知识点
Socket难点 数据粘包 心跳维持 数据丢包 性能问题 7层网络模型-OSI 基础层:物理层(Physical).数据链路层(Datalink).网络层(Network) 传输层(Transport ...
- 【Golang 接口自动化07】struct转map的三种方式
背景 我们在前面介绍过怎么使用net/http发送json或者map数据,那么它能不能直接发送结构体数据呢?我们今天一起来学习结构体struct转map的三种方法,为后续做铺垫. struct转map ...