Linux 下 redis 安装
参考:http://www.runoob.com/redis/redis-install.html
官网最新版本列表: http://download.redis.io/releases/
wget http://download.redis.io/releases/redis-4.0.11.tar.gz
tar xzf redis-4.0.11.tar.gz
cd redis-4.0.11
make
cd src
./redis-server
判断 redis 是否安装成功:
$ cd src
$ ./redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
Windows 下 redis 安装
https://github.com/MSOpenTech/redis/releases
安装完成之后, cmd 进入安装目录, 输入:
redis-server.exe redis.windows.conf
redis-cli.exe -h 127.0.0.1 -p 6379
set myKey abc
get myKey
.net core 控制台程序
先在 “程序包管理器控制台” 上安装包:
install-package ServiceStack.Redis
可以看一下相关的项目:https://github.com/ServiceStack/ServiceStack.Redis
代码:
using ServiceStack.Redis;
using ServiceStack.Redis.Generic;
using ServiceStack.Text;
using System;
namespace ConsoleApp3
{
class UserInfo
{
public long UserId { get; set; }
/// <summary>
/// Id列必须设置。因为GetById方法只识别名称为 id 的属性( id 的大小写不敏感 )。
/// </summary>
public long Id { get { return this.UserId; } }
public string UserName { get; set; }
}
class Program
{
static void Main(string[] args)
{
var redisManger = new RedisManagerPool("127.0.0.1:6379");
var redis = redisManger.GetClient(); //获取一个Redis Client
IRedisTypedClient<UserInfo> redisTodos = redis.As<UserInfo>();
var newTodo = new UserInfo //实例化一个Todo类
{
UserId = 1,
UserName = "小明"
};
redisTodos.Store(newTodo); //把newTodo实例保存到数据库中 增
UserInfo saveTodo = redisTodos.GetById(newTodo.Id); //根据Id查询 查
"Saved Todo: {0}".Print(saveTodo.Dump());
saveTodo.UserName = "小华"; //改
redisTodos.Store(saveTodo);
var updateTodo = redisTodos.GetById(newTodo.Id); //查
"Updated Todo: {0}".Print(updateTodo.Dump());
redisTodos.DeleteById(newTodo.Id); //删除
var remainingTodos = redisTodos.GetAll();
"No more Todos:".Print(remainingTodos.Dump());
Console.Read();
}
}
}