Redis 使用.net实际教程
2015-09-28 10:07
549 查看
1:打开Redis服务(已安装在windows服务下)
2:在应用中添加Redis引用
3:代码编辑
namespace Test
{
class Program
{
static void Main(string[] args)
{
var redisClient = new RedisClient("localhost");
using (var cars = redisClient.GetTypedClient<Car>())
{
if (cars.GetAll().Count > 0)
cars.DeleteAll();
var dansFord = new Car
{
Id = cars.GetNextSequence(),
Title = "Dan's Ford",
Make = new Make { Name = "Ford" },
Model = new Model { Name = "Fiesta" }
};
var beccisFord = new Car
{
Id = cars.GetNextSequence(),
Title = "Becci's Ford",
Make = new Make { Name = "Ford" },
Model = new Model { Name = "Focus" }
};
var vauxhallAstra = new Car
{
Id = cars.GetNextSequence(),
Title = "Dans Vauxhall Astra",
Make = new Make { Name = "Vauxhall" },
Model = new Model { Name = "Asta" }
};
var vauxhallNova = new Car
{
Id = cars.GetNextSequence(),
Title = "Dans Vauxhall Nova",
Make = new Make { Name = "Vauxhall" },
Model = new Model { Name = "Nova" }
};
redisClient.FlushAll();
var carsToStore = new List<Car> { dansFord, beccisFord, vauxhallAstra, vauxhallNova };
cars.StoreAll(carsToStore);
Console.WriteLine("Redis Has-> " + cars.GetAll().Count + " cars");
string Idlist = vauxhallAstra.Id + "," + vauxhallNova.Id;
var result = cars.ExpireAt(vauxhallAstra.Id, DateTime.Now.AddSeconds(5)); //Expire Vauxhall Astra in 5 seconds
Thread.Sleep(6000); //Wait 6 seconds to prove we can expire our old Astra
Console.WriteLine("Redis Has-> " + cars.GetAll().Count + " cars");
//Get Cars out of Redis
var carsFromRedis = cars.GetAll().Where(car => car.Make.Name == "Ford");
foreach (var car in carsFromRedis)
{
Console.WriteLine("Redis Has a ->" + 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; }
}
}
2:在应用中添加Redis引用
3:代码编辑
namespace Test
{
class Program
{
static void Main(string[] args)
{
var redisClient = new RedisClient("localhost");
using (var cars = redisClient.GetTypedClient<Car>())
{
if (cars.GetAll().Count > 0)
cars.DeleteAll();
var dansFord = new Car
{
Id = cars.GetNextSequence(),
Title = "Dan's Ford",
Make = new Make { Name = "Ford" },
Model = new Model { Name = "Fiesta" }
};
var beccisFord = new Car
{
Id = cars.GetNextSequence(),
Title = "Becci's Ford",
Make = new Make { Name = "Ford" },
Model = new Model { Name = "Focus" }
};
var vauxhallAstra = new Car
{
Id = cars.GetNextSequence(),
Title = "Dans Vauxhall Astra",
Make = new Make { Name = "Vauxhall" },
Model = new Model { Name = "Asta" }
};
var vauxhallNova = new Car
{
Id = cars.GetNextSequence(),
Title = "Dans Vauxhall Nova",
Make = new Make { Name = "Vauxhall" },
Model = new Model { Name = "Nova" }
};
redisClient.FlushAll();
var carsToStore = new List<Car> { dansFord, beccisFord, vauxhallAstra, vauxhallNova };
cars.StoreAll(carsToStore);
Console.WriteLine("Redis Has-> " + cars.GetAll().Count + " cars");
string Idlist = vauxhallAstra.Id + "," + vauxhallNova.Id;
var result = cars.ExpireAt(vauxhallAstra.Id, DateTime.Now.AddSeconds(5)); //Expire Vauxhall Astra in 5 seconds
Thread.Sleep(6000); //Wait 6 seconds to prove we can expire our old Astra
Console.WriteLine("Redis Has-> " + cars.GetAll().Count + " cars");
//Get Cars out of Redis
var carsFromRedis = cars.GetAll().Where(car => car.Make.Name == "Ford");
foreach (var car in carsFromRedis)
{
Console.WriteLine("Redis Has a ->" + 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; }
}
}
相关文章推荐
- Redis 宕机之后 启动失败 启动不了 原因之一 aof 文件出错 以及持久化介绍
- Java下利用Jedis开发Redis
- 【推荐】redis的主从复制配置
- redis在.net架构中的应用
- Redis 在线测试
- redis.conf配置详细解析
- Redis 作为多个Windows服务运行配置方法
- Cygwin编译redis
- redis有string,hash,list,sets.zsets几种数据类型
- redis key,string,hash,list,set,zset指令汇总
- 用Python组合Celery Redis RabbitMQ进行分布式数据抓取
- Redis安装、配置、操作、持久化、主从、phpredis扩展安装使用详解之操作使用
- 通过yum安装redis
- redis
- 使用elk+redis搭建nginx日志分析平台
- linux redis安装源码包安装
- Redis 命令 - Pub/Sub
- Redis 命令 - Server
- Redis数据类型和抽象
- redis-cli 使用