107_Redis basic commands-set, hash, set and transaction

 

Redis operation commands on collections:
1. Add one or more elements to the collection: sadd key member [member ....]
  If the elements are duplicated, they are ignored after the addition.
  sadd set01 abc
  sadd set01 ade
2. View all the elements in the set: smembers key
  smembers set01
3. Determine whether the specified value is in the set: sismemeber key value
  sismember set01 b
4. Remove one or more elements from the set: srem key member [member member ....]
  srem set01 e
  srem set01 bcd
5. Randomly return one or several elements from the set: srandmember key [count]
  |-> count> 0: Get elements are not repeated
  |-> count < 0: Get elements can be repeated
  srandmember set01: Randomly get an element
  srandmember set01 3: Randomly get 3 elements (can not be repeated)
  srandmember set01 -3: Randomly get 3 elements (can be repeated)
6. Randomly get one or more from the set Elements, and delete the obtained elements from the collection: spop key [count]
  spop set01
  spop set01 2
7. Move the specified element from one set to another: move src dest member
  smove set01 set02 f
8. Get the elements in the first set that are not in the other set: sdiff key key [key key .. ..]
  sdiff set01 set02 set03
9. Get all the elements in the specified set: sunlight key [key key ....]
  Only one repeated element.
  sunion set01 set02 set03
============================================== ===============
Operation commands for hash table in redis:
field-value stored in hash, for example name = "zhangSan", so you can use hash to represent object
1. The data is saved to the hash table: hset key field value [field value ....]
  If this field already exists in the hash table, the value put back will overwrite the value put before.
  hset stu01 id 1001
  hset stu01 name zhangsan email [email protected]
2. Get the field value specified in the hash table: hget key field
  hget stu01 id
3. Save the data in bulk to the hash table: hmset key field value [field value .. ..]
  hset stu02 id 1002 name lisi email [email protected]
4. Get the field values ​​specified in the hash table in bulk: hmget key field [field ....]
  hmget stu02 id name email
5. Get all the fields and values ​​in the hash table : Hgetall key
  hgetall stu01
6. Delete one or more fields and values ​​specified in the hash table: hdel key field [file ....]
  hdel stu02 name email
7. Get the logarithm of fild-vlaue in the hash table: hlen key
  hlen stu01
8. Check whether the specified field exists in the hash table: hexists key field
  hexists stu01 name
9. Get all files in the hash table: hkeys key
  hkeys stu01
10. Get all values ​​in the hash table: hvals key
  hvals stu01
11. Give hash Adding a certain number to a value in the table: hincrby key filed int
  hincrby stu01 age 5
12, Adding a certain number to a value in the hash table: hincrbyfloat key filed float
13. Save the data in the hash table. If this attribute already exists, it will not be saved: hsetnx key field value [field value ...]
================== ============================================
Operation of zset in redis Commands :
1. Save the data in an ordered collection: zadd key score member [score member ....]
  zadd zset01 20 z1 30 z2 15 z3 50 z4
  will automatically sort by score.
  zadd zset01 25 zz
2. Obtain the elements in the ordered set according to the subscript: zrange key startIndex endIndex [withscores]
  The subscripts start from 0, and the subscripts can be negative numbers. The subscript of the element.
  z3 z1 z2 z4
  zrange zset01 1 3 withscores
3. Get the elements in the ordered set according to the score: zrangebyscore key minScore maxScore [withscores]
  zrangebyscore zset01 20 40 withscores
4. Remove the elements in the ordered set: zrem key member [member. ..]
  zrem zset01 zz z2
5. Get the number of all elements in the ordered set: zcard key
  zcard zset01
6. Get the number of elements in the ordered set according to the score: zcount key minScore maxScore
  zcount zset01 20 40
7. Get the ranking of the specified elements in the ordered set (counting from the beginning, starting from 0): zrank key member
  zrank zset01 z1
8. Get the ranking of the specified elements in the ordered set (counting from back to front, starting from 0): zrevrank key member
  zrevrank zset01 z1
=================== ================================================== =
redis configuration file:
1. After redis software is installed, redis.conf is automatically provided in the redis installation directory;

  If the configuration file is not specified when the redis service is started, redis will use the default parameters;
  if the configuration file is specified when the redis service is started, the redis operation will refer to the parameters in the configuration file.

  If we want to modify the parameters in the configuration file, we must specify the configuration file when we start redis.
  When starting the redis service, specify the configuration file: redis-server configuration file name (redis.conf) &
2, configure the port number of redis: port 6379
  default is 6379
3, configure the IP address to access redis: bind ip
  if the configuration specifies access Redis ip address, you can use any ip on the machine where redis is located to access redis: 127.0.0.1,
  192.168.2.129, configure the specified ip address to access redis, then you can only use this ip to access redis service when accessing redis
  Actually, bind is generally the real IP of a certain network card on the designated server.
4. Databases: configure the number of Redis databases, the default is 16.
================================================== ====================
Persistence of redis:
RDB:
AOF:
===================== =================================================
Redis The transaction:
1. The transaction of the database: the sequence of operations and the atomicity of operations.
2. Redis transactions: sequential and atomic.
  Allows a set of commands to be executed sequentially in a transaction, under normal circumstances can guarantee atomicity (partial guarantee).
  multi: start a transaction. After the transaction is started, the instructions you enter will be pushed into the transaction queue instead of being directly executed; they will be executed together when the transaction is running.
  exec: execute transaction. All instructions pushed into the queue in the transaction will be executed together in sequence, and the transaction will end.

  multi
  set k1 v1
  set k2 v2
  exec
3. After the transaction is started, if an exception occurs during the instruction is pushed into the queue, all the instructions in this transaction will fail to execute (such as wrong input of the command).
  multi
  set k1 v1
  setkjkjkj k2 v2
  exec
4. After the transaction is started, if an exception occurs during the execution of the transaction, it will only affect the operation result of this instruction, and other instructions operate normally (such as incr for strings).
  multi
  set k1 vv1
  set k2 vv2
  incr k1
  exec

5. Clear all commands pushed into the queue:
  discard
6. Before the transaction is started, monitor a key; if the value of this key changes before the transaction is executed, all instructions in the transaction will be abandoned; if the value of this key is not Changes occur and the transaction executes normally:
  watch flag
  multi
  set k1 v1
  set k2 v2
  exec
7. Abandon all previous monitoring keys: unwatch
  watch flag
  unwatch
  watch flag 2
  multi
  set k1 v1
  set k2 v2
  exec



Guess you like

Origin www.cnblogs.com/pogusanqian/p/12697683.html