Redis in docker

1、redis image

概念 内容
redis官网 https://redis.io/topics/persistence
什么是redis Redis is an open-source, networked, in-memory, key-value data store with optional durability. It is written in ANSI C. The development of Redis is sponsored by Redis Labs today; before that, it was sponsored by Pivotal and VMware. According to the monthly ranking by DB-Engines.com, Redis is the most popular key-value store. The name Redis means REmote DIctionary Server.
镜像源 https://hub.docker.com/_/redis?tab=description
拉取镜像 docker pull redis
端口与安全 For the ease of accessing Redis from other containers via Docker networking, the "Protected mode" is turned off by default. This means that if you expose the port outside of your host (e.g., via -p on docker run), it will be open without a password to anyone. It is highly recommended to set a password (by supplying a config file) if you plan on exposing your Redis instance to the internet. For further information, see the following links about Redis security:

2、启动redis服务

持久化数据库文件的启动
docker run --name some-redis -d redis redis-server --appendonly yes -v /docker/host/dir:/data
命令行客户端 连接 redis服务
docker run -it --network some-network --rm redis redis-cli -h some-redis
// --rm 与 -d 类似,-d是命令行历史保留, --rm是历史不保留,在stop后容器自动删除
自定义redis的配置文件
docker run -v /myredis/conf/redis.conf:/usr/local/etc/redis/redis.conf --name myredis redis redis-server /usr/local/etc/redis/redis.conf
一个例子
root@jd-bj-bjy-tianren-test01:~/myredis# docker run --name redis-server --network host -v /root/myredis/data:/data -d redis redis-server --appendonly yes
root@jd-bj-bjy-tianren-test01:~/myredis# docker run -it --network host --name redis-cli --rm redis redis-cli -h localhost
localhost:6379> 
localhost:6379> get
(error) ERR wrong number of arguments for 'get' command
localhost:6379> get 123
(nil)
localhost:6379> set 123 0090909
OK
localhost:6379> get 123
"0090909"

3、redis 的数据类型

image.png

4、redis 的常用命令

功能和验证都很简单,跑个redis-server、redis-cli,几分钟就能很好的理解

1、远程连接到数据库
redis-cli -h 127.0.0.1 -p 6379 -a "mypass"
2、string 类型的 key-value 的命令
get key
set key val
exists key
expire key 100
persist key
ttl key
rename key
incr/decr/append key
keys partten
3、hash类型的 json 的命令
适用于存储对象,json对象
{
    obkey1  : "obval1",
    obkey2  : "obval2"
}
hmset key obkey1 "obval1" obkey2 "obval2"
hgetall key
hkeys key
hvals key
hget key obkey1
hset key obkey1 "obval1"
heixsts key obkey1
4、list类型是 列表命令
列表方式存储的数据
lpush key val1 val2 val3
lpop key
llen key
lrem key 4 val3
lindex key 2
lset key 4 val4
lrange key 0 5
5、set类型的 集合命令
集合内数据元素是唯一的,不能重复
sadd key val1 val2 val3 val4
srem key val1 val2
smembers key
scard key

sunion key1 key2
sinter key1 key2
sdiff key1 key2
6、事务

redis 通过事务 可以一次执行多条命令,有如下特点:
1、顺序性, 多条命令顺序执行
2、假原子性, 多条命令作为一个整体执行(不会被其他的用户的命令在顺序中插入)
3、错不回滚, 中间某条执行失败不会导致回滚,后面的命令继续执行

实现一个事务的流程
1、开始事物 multi
2、命令入队 commad1 commad2 。。。
3、执行事物 exec

关于事物原子性:
单个 Redis 命令的执行是原子性的,但 Redis 没有在事务上增加任何维持原子性的机制,所以 Redis 事务的执行并不是原子性的。

事务可以理解为一个打包的批量执行脚本,但批量指令并非原子化的操作,中间某条指令的失败不会导致前面已做指令的回滚,也不会造成后续的指令不做。

localhost:6379> MULTI 
OK
localhost:6379> set key abc
QUEUED
localhost:6379> set key2 abc2
QUEUED
localhost:6379> exec
1) OK
2) OK


discard
watch key1 key2 ...
unwatch
7、订阅

这个功能有点坑,subscribe channel 之后无法退出(docker里ctrl+c无效)
多用户的连接也不怎么好使
多用户的发布和订阅没验证成功

5、用nodejs 客户端访问 redis-server ,存储和查询数据

redis 包含了全方位的客户端sdk ,java、python、go、c++、nodejs等接口,
可官网查询 https://redis.io/clients

这里简要说一下 nodejs 的 其中一个node_redis sdk接口: https://github.com/NodeRedis/node_redis

npm install redis

使用案例如下 
var redis = require("redis"),
    client = redis.createClient();

// if you'd like to select database 3, instead of 0 (default), call
// client.select(3, function() { /* ... */ });

client.on("error", function (err) {
    console.log("Error " + err);
});

client.set("string key", "string val", redis.print);
client.hset("hash key", "hashtest 1", "some value", redis.print);
client.hset(["hash key", "hashtest 2", "some other value"], redis.print);
client.hkeys("hash key", function (err, replies) {
    console.log(replies.length + " replies:");
    replies.forEach(function (reply, i) {
        console.log("    " + i + ": " + reply);
    });
    client.quit();
});

你可能感兴趣的:(Redis in docker)