redis实例

redis有序集合

127.0.0.1:6379> zadd orderlist 1 mysql

(integer) 1
127.0.0.1:6379> zadd orderlist 2 redis
(integer) 1
127.0.0.1:6379> zadd orderlist 3 mongodb
(integer) 1
127.0.0.1:6379> zrange orderlist 0 -1
1) "mysql"
2) "redis"
3) "mongodb"
127.0.0.1:6379> zrank orderlist mysql
(integer) 0
127.0.0.1:6379> zrevrank orderlist mysql
(integer) 2

127.0.0.1:6379>

设置过期时间为100秒的key,它的值是value

setex key 100 value

如果指定的key不存在那么设置key的值是value

setnx key value

队列和栈

队列先进先出 rpush lpop

栈先进后出  rpush rpop

集合

> sadd books python
(integer) 1
> sadd books python  #  重复
(integer) 0
> sadd books java golang
(integer) 2
> smembers books  # 注意顺序,和插入的并不一致,因为 set 是无序的
1) "java"
2) "python"
3) "golang"
> sismember books java  # 查询某个 value 是否存在,相当于 contains(o)
(integer) 1
> sismember books rust
(integer) 0
> scard books  # 获取长度相当于 count()
(integer) 3
> spop books  # 弹出一个
"java"...

查看key的类型type

type books

redis锁

#!/bin/bash
/usr/bin/redis-cli set lock:codehole true ex 100 nx
echo "hello"
/usr/bin/redis-cli del lock:codehole

    A+
发布日期:2018年11月01日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: