Redis operation details

There are five main data types supported by Redis: String, Hash, List, Set, and Sorted Set.

One: String                                                  

1.set key value [EX n] (expiration time) [NX] (do not overwrite if the key already exists)   insert data (the key already exists, overwrite by default)

2.get key to get data

3.Append key "string" to append data

4.del key delete data

5.getset key newValue first get the original value, and then set the new value

6. incrby key n (n is a number) data addition and addition operation incr key is plus one

7.decrby key n data subtraction operation decr key is minus one

8.strlen key Get the str length, if the key or value does not exist, return 0

9.setex key second (time) value set value, and specify the expiration time

10.setnx key value insert data, if it exists, do not perform the operation and return 0; if it does not exist, insert data and return 1

11.setarrange key index value replaces from the specified position

12.getrange key startIndex end intercepts the string, and returns the entire string when end is equal to -1

13.mget key [key2 key3 ...] batch read

14.mset key value [key2 value2...] batch modification

15.msetnx key value [key2 value2] batch modification, when a key already exists, all inserts are not executed

 

Two: List                                                    

 The List type data structure is a linked list. Support paging query, sorting; can monitor List (blpop\brpop\blpoprpush)

 

1.rpush listKey value [value2 value3...] Insert data from the right side into the List whose key is listKey

2.lpush listKey value [value2 value3...] Insert data from the left into the List whose key is listKey

3. linsert listKey BEFORE|AFTER pivot value   Find the data whose value is pivot in the List whose key is listKey , and insert a piece of data whose value is value before and after it

4.lrange listKey start stop View list data, start start position, stop end position

5.lpop listKey removes a piece of data from the left side of the list and returns

6 .rpop listKey removes a piece of data from the right side of the list and returns

7.llen listKey to view the length of the list

8.lindex listKey index to get the given location data

9.lset listKey index value Modify the value of the specified index position

10.rpoplpush AB adds the tail element of A to the head element of B

11. blpop\brpop listKey time           removes a data from the left and right sides of the list. If the list is empty, wait until data is inserted into the list. The waiting time is time seconds. If time is set to 0, it means blocking until there is data in the list. end of blocking

12. brpoplpush AB time              adds the tail element of A to the head element of B. If A is empty, it will block. The waiting time is time seconds. If time is set to 0, it will block until there is data in the list.

 

Three: Set                                                     

  The Set data structure is a hash table with no duplicate data.

1.sadd mySet member Add member member to the Set whose key is mySet

2.smembers mySet get all the elements in the set

3. sismember mySet value Determine whether there is a value in mySet, whether it is 1 or 0

4.srem member1 [member2...] delete elements, return the number of deletions

5.scard mySet to view the number of elements in mySet

6. srandmember mySet randomly gets an element in mySet

7.spop deletes an element at random and returns

8.smove mySet mySet2 member Move the member element in mySet to mySet2

9.sinter mySet mySet2 [mySet3...] to        find the intersection

10.sunion mySet mySet2 [mySet3...] union set

11sdiff mySetmySet2 [mySet3 ...] Complement

 

Four: Hash                                                    

  Hash是一个field和value的映射表,一个key可以对应多个field,一个field对应一个value。适合存储对象。

1.hset|hsetnx myHash field value         向myHash中添加一堆映射field,value。若field存在则覆盖原有值。(hsetnx 若field存在则取消操作)

2.hget myHash key                读取myHash中key对应的value

3.hlen myHash                  获取myHash中的field数量

4.hexits myHash field              判断myHash中是否存在field,是1否0

5.hdel myHash field [field2...]        删除myHash中的field字段

6.hincrby myHash field n            myHash中field字段的value加n(负数为减)

7.hmset myHash field1 value1 [field2 value2...] 批量插入

8.hmget myHash field [field2...]        批量获取

9.hgetall myHash                获取myHash中所有键值对

10.hkeys myHash                 获取myHash中所有field

11.hvals myHash                 获取myHash中所有values

 

五:Zset                                                    

  Zset类型与Set类型一样不能有重复数据,但Zset是有序的,每个成员都有一个“分数”与之对应。

1.zadd myzset score member [score2 member2...] 向myzset中添加分数为score的member

2.zcard myzset                  查看myzset中的成员个数

3.zrange myzset start stop [withscores]     查看制定范围内的成员,withssores为输出结果带分数

4.zrank myzset member               获取myzset中member的下标位置,不存在则返回null

5.zcount myzset min max              获取分数从min到max的成员个数

6.zrem myzset member [member2...]         删除成员

7.zscore myzset member               获取指定成员的分数

8.zincrby myzset n member             将member的分数加n

9.zrangebyscore myzset min max          获取分数从min到max的members

10.zremrangebyscore myzset min max        删除分数从min到max的成员

11.zremrangebyrank myzset start stop       删除下标从start到stop的成员

12.zrevrange myzset start stop          倒叙排序数据,输出

13.revrangebyscore myzset max min [withscores][limit offset count] 按分数倒序输出

14.zunionstore Cset n Aset Bset[Dset...]    将Aset、Bset的并集添加到Cset中(n为集合数)

15.zinterstore Cset n Aset Bset[Dset...]    将Aset、Bset的交集添加到Cset中(n为集合数)

六:Key操作                                                 

1.del key [key2...]               输出key

2.exists key                   判断key是否存在,是1否0

3.move key db                  将key移动到指定数据库

4.rename key newkey               重命名,若newkey已存在则覆盖原值

5.expire key seconds              设置过期时间(相对于当前时间)

6.expireat key seconds             设置过期时间(相对于1970/1/1)

7.ttl key                    返回过期时间,当key不存在时(包括已过期的key)返回-2,永久不过期的返回-1

8.persist key                  清楚key的过期时间,使其持久化

9.randomkey                   随机返回一个key

10.type key                   返回key对应的数据类型

七:事务操作                                                 

需要注意的是,redis的事务中,如果是一条命令语法出错,则事务中所有命令都不执行;如果是运行时错误,redis无法发现,其他命令照常执行。

1.multi                     开启事务

2.exec                      执行事务

3.discard                    回滚事务

4.watch  key[key2...]             监控key,如果被监控的key在事务之前发生修改,且紧接着的一个事务中包含该key,则该事务将被回滚。(紧邻的事务执行过后,监控自动取消)

5.unwatch key                 取消监控

 

其他:

1.flushdb                   清楚当前数据库的全部数据

2.select db                  切换当前数据库

  redis中为了将数据分类存储,可以开启多个redis服务,也可以选择将数据存在不同的数据库中。redis是支持多数据库的,且数据库由一个整数索引标识,可以在配置文件中控制数据库的数量

databases num ,num为数据库数量。默认值16,且默认连接到的数据库所以为0。

八:启动停止                                            

1.redis-server [配置文件路径(可选,默认为根目录下redis.conf)]   启动redis服务

2.redis-cli shutdown             停止redis服务

3.redis-cli [-p 6379]                  打开客户端

4.脚本方式启动

 

九:配置文件                                                 

1.port 6379                  监听端口

2.logfile "/home/root/logs/redis.log"    日志文件

3.dir /home/root/data/redisData       持久化文件存放位置

4.daemonize no                是否默认后台运行

5.save 900 1                 900秒后至少有一个key被更改则快照保存到硬盘

 save 300 10

 save 60 10000              

 三个save为“或”的关系,如果不想持久化则删除所有save即可。

 Redis快照实现过程:(快照方式即为RDB)

  1. Redis使用fork函数复制一份当前进程(父进程)的副本(子进程);
  2. 父进程继续接收并处理客户端发来的命令,而子进程开始将内存中的数据写入硬盘中的临时文件;
  3. 当子进程写入完所有数据后会用该临时文件替换旧的RDB文件,至此一次快照操作完成。

6.appendonly yes               AOF方式的持久化

7.appendfilename appendonly.aof       配置aof文件名

8.appendfsync always/everysec/no      设置同步方式,每次写入、每秒、每30秒

 

十:备份与还原                                                   

  自动,RDB方式与AOF方式对比:

    RDB:启动时redis直接将RDB文件中的数据载入内存,速度很快,且不必时刻向硬盘写数据,但是一旦redis发生异常退出,则上一次快照之后的数据都将丢失。

    AOF:每执行一条更改都会将该命令写入硬盘中的AOF文件,启动时redis会逐个执行AOF文件中的命令来将数据加载到内存。速度较慢,但是不会丢失数据。

 

  手动:

    save/bgsave              发送命令对当前数据库进行快照。

    save由主进程进行快照,会阻塞其他请求;

    bgsave会fork子进程进行快照,即后台执行。

    还原只需将备份文件拷贝到数据目录,启动服务

 

十一:启动脚本:utils/redis_init_script                          

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326763670&siteId=291194637