自己动手搭建 Redis 环境,并建立一个 .NET HelloWorld 程序测试
关于 Redis ,下面来自百度百科:redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hashs(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Python,Ruby,Erlang,PHP客户端,使用很方便。
上次测试了 Memcached,今天决定试试这个 Redis。
由于 Redis 部署在 Linux 环境下性能优于 Windows,于是就打算在虚拟机 ubuntu 环境下部署,然后在 Windows 8 下测试。
1. 去官网下载 Redis 解压和安装,我下载的 2.8.0-rc2。
$ wget http://redis.googlecode.com/files/redis-2.6.15.tar.gz
$ tar xzf redis-2.6.15.tar.gz
$ cd redis-2.6.15
$ make
2. 编译后的可执行文件在src目录中,可以使用下面的命令运行Redis:
$ src/redis-server
3. 你可以使用内置的客户端连接Redis:
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
4. 在 Windows 下新建 .NET 程序测试。
如果连不上
http://images.cnitblog.com/blog/148372/201308/28180312-fe3c9d2810d447b790c6fee8ff850aba.png
成功的情况下
http://images.cnitblog.com/blog/148372/201308/28180343-c84dc53faac54c5c987bde98ec346be8.png
测试代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;
using System.Threading;
namespace RedisTutorial
{
class Program
{
static void Main(string[] args)
{
using (var redisClient = RedisManager.GetClient())
{
using (var cars = redisClient.GetTypedClient())
{
if (cars.GetAll().Count > 0)
cars.DeleteAll();
var dansFord = new Car { Id = cars.GetNextSequence(), Title = "张三的汽车", Make = new Make { Name = "宝马" }, Model = new Model { Name = "奔驰" }};
var beccisFord = new Car{Id = cars.GetNextSequence(),Title = "李四的汽车",Make = new Make { Name = "本田" },Model = new Model { Name = "福特" }};
var vauxhallAstra = new Car{Id = cars.GetNextSequence(),Title = "王五的汽车",Make = new Make { Name = "比亚迪" },Model = new Model { Name = "通用" }};
var vauxhallNova = new Car{Id = cars.GetNextSequence(),Title = "赵六的汽车",Make = new Make { Name = "大众" },Model = new Model { Name = "奥迪" }};
var carsToStore = new List { dansFord, beccisFord, vauxhallAstra, vauxhallNova };
cars.StoreAll(carsToStore);
Console.WriteLine("Redis 有-> " + cars.GetAll().Count + " 辆汽车。");
cars.ExpireAt(vauxhallAstra.Id, DateTime.Now.AddSeconds(5));
Thread.Sleep(6000);
Console.WriteLine("Redis 有-> " + cars.GetAll().Count + " 辆汽车。");
var carsFromRedis = cars.GetAll().Where(car => car.Make.Name == "比亚迪");
foreach (var car in carsFromRedis)
{
Console.WriteLine("Redis 有 ->" + car.Title);
}
}
}
Console.ReadLine();
}
}
public class Car
{
public long Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public Make Make { get; set; }
public Model Model { get; set; }
}
public class Make
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Model
{
public int Id { get; set; }
public Make Make { get; set; }
public string Name { get; set; }
}
}
配置文件:
测试代码下载:点我下载
参考:ServiceStack.Redis 使用教程
其它资源:
redis在.net架构中的应用(1)--利用servicestack连接redis
谢谢浏览!
页:
[1]