#拉取redis > docker pull redis:latest latest: Pulling from library/redis 8d691f585fa8: Pull complete 8ccd02d17190: Pull complete 4719eb1815f2: Pull complete 200531706a7d: Pull complete eed7c26916cf: Pull complete e1285fcc6a46: Pull complete Digest: sha256:fe80393a67c7058590ca6b6903f64e35b50fa411b0496f604a85c526fb5bd2d2 Status: Downloaded newer image for redis:latest docker.io/library/redis:latest #查看images > docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis latest de25a81a5a0b 6 days ago 98.2MB
#運行
docker run -ti redis:latest /bin/bash #啓動redis:後臺啓動redis 並將redis6379端口映射至本地6380端口 容器名設爲some-redis1 > docker run --name some-redis1 -p 6380:6379 -d redis:latest --requirepass "123456" a996cb04b0024be3fd4f6d08e3b567ad9389d9c59b1185f601400ab48e17cefd #查看容器 > docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a996cb04b002 redis:latest "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:6380->6379/tcp some-redis1 #進入容器&&鏈接redis客戶端 > docker exec -ti a996cb04b002 redis-cli 127.0.0.1:6379> #運行redis命令 127.0.0.1:6379> auth 123456 OK 127.0.0.1:6379> set name panpan OK 127.0.0.1:6379> get name "panpan" 127.0.0.1:6379> quit
#進入容器 > docker exec -ti a996cb04b002 /bin/bash #查看環境變量 root@a996cb04b002:/data# env HOSTNAME=a996cb04b002 REDIS_DOWNLOAD_SHA=6624841267e142c5d5d5be292d705f8fb6070677687c5aad1645421a936d22b3 PWD=/data HOME=/root REDIS_VERSION=5.0.6 GOSU_VERSION=1.11 TERM=xterm REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-5.0.6.tar.gz SHLVL=1 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin _=/usr/bin/env #查看目錄 root@a996cb04b002:/data# ls #查看容器內運行的進程 root@a996cb04b002:/data# ps -ef bash: ps: command not found #進入redis客戶端 root@a996cb04b002:/data# redis-cli #退出redis客戶端 127.0.0.1:6379> quit #遠程鏈接redis root@a996cb04b002:/data# redis-cli -h 192.168.*.* -p 6379 #操做redis-cli時需認證 auth yourpassword
應用示例redis
#模糊查詢 root@a996cb04b002:/data# redis-cli -h 192.168.*.* -p 6379 --scan --pattern '*GetPartAccount_*' DVMProxyApi-devGetPartAccount_facebook DVMProxyApi-alphaGetPartAccount_facebook DVMProxyApi-testGetPartAccount_facebook #查詢value數據 root@a996cb04b002:/data# redis-cli -h 192.168.*.* -p 6379 :6379> type DVMProxyApi-devGetPartAccount_facebook hash :6379> Hkeys DVMProxyApi-devGetPartAccount_facebook 1) "absexp" 2) "data" 3) "sldexp" :6379> HGET DVMProxyApi-devGetPartAccount_facebook sldexp "-1" :6379> HGET DVMProxyApi-devGetPartAccount_facebook data "{\"data\":[******
#.net core:獲取滑動過時時間 :6379> HGET DVMProxyApi-devGetPartAccount_facebook sldexp "864000000000" #查詢redis過時時間:以秒爲單位返回 :6379> ttl DVMProxyApi-testGetPartAccount_facebook (integer) 79765 #powershell 時間段轉爲ttl > New-TimeSpan -Hours 24 #New-TimeSpan -seconds 76765 Days : 1 Hours : 0 Minutes : 0 Seconds : 0 Milliseconds : 0 Ticks : 864000000000 TotalDays : 1 TotalHours : 24 TotalMinutes : 1440 TotalSeconds : 86400 TotalMilliseconds : 86400000
#刪除fields :6379> hdel DVMProxyApi-devGetPartAccount_facebook data absexp sldexp (integer) 3 #查看過時時間 :6379> ttl DVMProxyApi-testGetPartAccount_facebook (integer) 78700 #獲取在哈希表中指定 key 的全部字段和值 :6379> hgetall DVMProxyApi-devGetPartAccount_facebook (empty list or set) #刪除key :6379> del DVMProxyApi-devGetPartAccount_facebook (integer) 0 #檢查給定 key 是否存在。 :6379> exists DVMProxyApi-devGetPartAccount_facebook (integer) 0 :6379> quit root@a996cb04b002:/data# redis-cli -h 192.168.*.* -p 6379 --scan --pattern '*GetPartAccount_*' DVMProxyApi-alphaGetPartAccount_facebook DVMProxyApi-testGetPartAccount_facebook root@a996cb04b002:/data#