Redis must know API


Some nonsense

1. Redis is an open-source, key based storage system with multiple data structures and functions.
2. Redis supports persistence, does not lose data after power failure, and asynchronously saves data updates to disk.
3. Redis supports string, hash, list, set, ordered set, bitmap, unique count of ultra small memory, and geographic information positioning.
4. Redis supports multilingual clients, publish and subscribe, Lua scripts, things, independent of external libraries, single thread model, master-slave replication, high availability and distribution.
5. Typical use scenarios of redis include cache system, counter, message queue system, leaderboard, social network and real-time system.
6. Starting mode is divided into

  • The simplest way to start redis server
  • Dynamic parameter redis server — port 6380
  • Configuration file redis server configpath

7. Verify start

  • ps -ef | grep redis
  • netstat -antpl | grep redis
  • redis-cli -h ip -p port ping

8. The return values of redis client include status reply, error reply, integer reply, string reply and multi line string reply.
9. Common configuration

  • Is daemonize a daemon
  • Port external port
  • Logfile log
  • Dir working directory

10. Run only one command at a time, refuse long and slow commands, and do not easily execute keys, flushdb, fluhall, show Lua script, etc.

11. In fact, redis is not a one-way process, such as the following API fysnc file descriptor, close file descriptor

Redis API

1. General Command

  • Keys: calculate all keys o (n)
  • Dbsize: database size
  • Exists keys: whether the key exists
  • Del: delete key
  • Expire key seconds: set expiration time
  • Type key: get the data type of key
  • TTL key: view the remaining expiration time of key
  • Persist key: remove the expiration time of key

2. List type

  • rpush key value1 value2 valueN O(1-n)
  • lpush key value1 value2 valueN O(1-n)
  • linsert key before|after value newValue O(n)
  • lpop key
  • rpop key
  • Lrem key count value (delete count value elements) 0 (n)
  • Ltrim key start end o (n)
  • Lrange key start end (including end)
  • lindex key index o(n)
  • llen key
  • lset key index newValue
  • Blpop key timeout (blocking version of lpop)
  • Brpop key timeout (blocking version of rpop)
  • lpush + lpop = stack
  • lpush + rpop = queue
  • lpush + ltrim = Capped Collection
  • lpush + brpop = Message Queue

3. String type

  • Scenario: cache, counter, distributed lock
  • get key
  • set key value
  • incr key
  • decr key
  • incrby key k
  • decrby key k
  • Setnx key value does not exist before setting
  • Set key value XX exists before setting
  • Get o (n) in batch by mget
  • Mset batch setting o (n)
  • GetSet key newvalue set new value return old value
  • Append key value append the new value to the old value
  • Strlen gets the length of the value
  • Incrbyfloat key 3.5 add corresponding key 3.5
  • getrange key start end
  • setrange key start value

4. Set type

  • Unordered and unrepeated support for inter collection operations
  • Sadd key element
  • SREM key element (delete)
  • scard key
  • sismember key element
  • Srandmember key count (randomly select count elements)
  • Pop key (pop an element at random)
  • Smembers key (take out all elements and use with care)
  • Sscan (traversal set)
  • Sdiff difference sets
  • Single intersection
  • Union of Sunion
  • Sadd = label
  • Spop / srandmember = random
  • sadd + sinter = Social Graph

5. Ordered set type

  • zadd key score element O(logN)
  • Zrem key element (delete)
  • zscore key element
  • Zincrby key score element
  • Zcard key (number returned)
  • Zrange key start end WithCores (O (logn + m))
  • zrangebyscore key minScore maxScore
  • zcount key minScore maxScore (O(logN + m))
  • Zremrangebyrank key start end
  • Zremrangebyscore key start end
  • zrevrank
  • zrevrange
  • zrevrangebyscore
  • zinterstore
  • zunionstore

6. Hash type

  • hget key filed
  • hset key field value
  • hdel key field
  • hgetall key O(n)
  • hexists key field
  • HLEN key gets the number of fields
  • hmget key field1 field2 O(n)
  • hmset key field1 value1 field2 value2 O(n)
  • hincrby key field value
  • Hvals key returns the value o (n) of all fields corresponding to the hash key
  • Hkeys key returns all field o (n) corresponding to hash key
  • hsetnx key field value
  • hincrby key field intCounter
  • hincrbyfloat key field floatCounter

Redis must know API