1砌梆、redis image
概念 | 內(nèi)容 |
---|---|
redis官網(wǎng) | 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服務(wù)
持久化數(shù)據(jù)庫文件的啟動
docker run --name some-redis -d redis redis-server --appendonly yes -v /docker/host/dir:/data
命令行客戶端 連接 redis服務(wù)
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 的數(shù)據(jù)類型
4、redis 的常用命令
功能和驗證都很簡單遭笋,跑個redis-server坝冕、redis-cli,幾分鐘就能很好的理解
1瓦呼、遠(yuǎn)程連接到數(shù)據(jù)庫
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類型是 列表命令
列表方式存儲的數(shù)據(jù)
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類型的 集合命令
集合內(nèi)數(shù)據(jù)元素是唯一的,不能重復(fù)
sadd key val1 val2 val3 val4
srem key val1 val2
smembers key
scard key
sunion key1 key2
sinter key1 key2
sdiff key1 key2
6稳摄、事務(wù)
redis 通過事務(wù) 可以一次執(zhí)行多條命令稚字,有如下特點:
1、順序性厦酬, 多條命令順序執(zhí)行
2胆描、假原子性, 多條命令作為一個整體執(zhí)行(不會被其他的用戶的命令在順序中插入)
3弃锐、錯不回滾袄友, 中間某條執(zhí)行失敗不會導(dǎo)致回滾,后面的命令繼續(xù)執(zhí)行
實現(xiàn)一個事務(wù)的流程
1霹菊、開始事物 multi
2剧蚣、命令入隊 commad1 commad2 。旋廷。鸠按。
3、執(zhí)行事物 exec
關(guān)于事物原子性:
單個 Redis 命令的執(zhí)行是原子性的饶碘,但 Redis 沒有在事務(wù)上增加任何維持原子性的機(jī)制目尖,所以 Redis 事務(wù)的執(zhí)行并不是原子性的。
事務(wù)可以理解為一個打包的批量執(zhí)行腳本扎运,但批量指令并非原子化的操作瑟曲,中間某條指令的失敗不會導(dǎo)致前面已做指令的回滾,也不會造成后續(xù)的指令不做豪治。
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無效)
多用戶的連接也不怎么好使
多用戶的發(fā)布和訂閱沒驗證成功
5负拟、用nodejs 客戶端訪問 redis-server 烦衣,存儲和查詢數(shù)據(jù)
redis 包含了全方位的客戶端sdk ,java掩浙、python花吟、go、c++厨姚、nodejs等接口衅澈,
可官網(wǎng)查詢 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();
});