redis是一个开源的、使用C语言编写的、支持网络交互的、可基于内存也可持久化的Key-Value数据库。
redis 安装:yum install redis -y
支持的5中数据类型:1.字符串(strings)2.字符串列表(lists)3.字符串集合(sets)4.有序字符串集合(sorted sets)5.哈希(hashes)
1.通用命令:
查询所有键:keys *
查看以a开头的键:keys a*
判断键是否存在:exists key
查看键对应的value类型:type key
给存在的键设置过期时间:expire key time
查询过期时间:ttl key

选择数据库:select 0
del key 删除键
清空所有库:flushall
清空当前库:flushdb
1.字符串(strings)
redis中的String在在内存中按照一个name对应一个value来存储。
1、set(name, value, ex=None, px=None, nx=False, xx=False) #设置值
ex,过期时间(秒)
px,过期时间(毫秒)
nx,如果设置为True,则只有name不存在时,当前set操作才执行
xx,如果设置为True,则只有name存在时,当前set操作才执行

2、setex(name, value, time) #设置值

3、mset(*args, **kwargs) #批量设置值
mset(k1='v1', k2='v2')/ mset({'k1':'v1','k2':'v2'})

4、get(name) 获取值

5、mget(keys, *args) #批量获取
6、getset(name, value) 设置新值并获取原来的值
7、getrange(key, start, end) #截取字符

2.列表(list)
1.lpush/rpush <key> <value1> <value2> 从左或从右插入多个值

2.lpop/rpop <key> 从左边或者右边获取一直,并删除。

3.lindex <key> <index>按照索引下标获取元素 从左到右

4.llen <key> 获取列表长度获取列表长度
5、lrange <key> <index>按照索引下标获取元素 从左到右

6、linsert <key> before/after <value> <newvalue>在key中value前/后插入newvalue

7、rpoplpush <key1> <key2>从key1 右边吐出一个值到key2的左边

3、集合(set) 保证列表中不会有重复数据
1、sadd <key> <value1> <value2>将多个元素加入到key中,重复值忽略

2、smembers <key>取出该集合的所有值

3、sismember <key> <value>判断集合key中是否有该value值 有就1 没有0

4、scard <key>返回该集合的元素个数

5、srem <key> <value1> <value2>删除集合中的某个元素

6、spop <key>随机吐出该集合一个值,并删除

7、sunion <key1> <key2>返回两个集合的并集 并去重

4、哈希(hash) 键值对集合
1、hset <key> <filed> <value>给key 集合中的file 键赋值value

2、hget <key1> <field>从key1 集合file取出value

3、hmset <key1> <field1> <value1> <field2> <value2>批量设置hash的值
4、hkeys <key>列出key中所有的filed

5、hvals <key>列出该hash集合中所有的value

6、HDEL KEY_NAME FIELD1.. FIELDN 删除指定字段

5、有序集合(zset) 每个元素都关联着一个浮点数分值(Score),并按照分值从小到大的顺序排列集合中的元素
1、zadd<key><score1><value1><score2><value2>将一个或多个元素以及score加入zset

2、zincrby <key> <increment> <value>在key集合中的value上的score进行增加increment

3、zcount <key> <min><max>统计 区间内的元素个数

4、zscore key value 得到score

5、zrange<key><start><stop> withscore返回下标在区间内的集合,带有score

6、zrank <key><value>查询value在key中的排名,从0开始

7、移除一个或多个元素ZREM key member [member ...]元素不存在,自动忽略
网友评论