java learning - Redis Basics

1. Why use NoSql
As the number of users of the website increases, the data information increases, and the reading and writing pressure increases, the relational database can no longer meet the requirements.
At this time, NoSQl non relational database was born. NoSQL+RDBMS
2. Features of NoSQL

  • It is convenient to expand, and there is no relationship between the data. For example, map < string, Object >
  • High performance of large amount of data (Redis supports 110000 reads / s and 80000 writes / s)
  • Due to the diversity of database types, there is no need to realize the design database, which can be taken and used at any time

3.Redis (remote Dictionary Service) Chinese web address
Redis is an open source log and key value database written in ANSI C language, supporting network, memory based and persistent, and provides API s in multiple languages.
What can Redis do (it can be used as database, cache and message middleware)
1. Memory storage and persistence. Memory is lost when power is off.
2. It is efficient and can be used to tell the cache.
3. Publish and subscribe system.
4. Map information.
5. Timer, counter.
6.. . . . .
1. Download
2. Decompression

3. Start:

4. Use the redis client to connect to redis, and the connection is successful

1. Copy the installation package of linux version to linux and unzip it.
2. Enter make install in the unzipped folder
3. The default installation path of redis is in usr/local/bin
4. Redis Copy the connf file to where we need it[ root@localhost bin]# cp /opt/redis-5.0.8/redis. conf config
5. Modify redis Connf file bypasses redis background startup

6. Start the redis service (through the specified configuration file)

7. Test

8. Close exit

Basic knowledge
redis has 16 databases, and database 0 is used by default
Some commands
Set the expiration time of the key to 10s

flashdb clears the current database
Flash clear all databases
Use select 2 to switch to database 2
EXISTS key determines whether this key exists
type key view the type of key

move key 1 Remove current key
ttl key view remaining time

You can view the commands you won't encounter on the official website

Redis is a single thread, because cpu is not the bottleneck of redis. Single thread can be solved

Redis five data types
strings, hashes, lists, sets, sorted sets
1. String operation (string type)

APPEND key1 'abc'

STRLEN key  Get string length
incr key   Plus 1
INCRBY key 10  Plus 10
decr Minus 1
DECRBY key 10 Minus 10> getrange key 5 10  Intercept string
"dasdas"> getrange key 0 -1  View all strings
"adadsdasdasdas"> setrange key 1 mko Replace intermediate characters
(integer) 14> get key
setex key "dasdas" 60  Existence time: 60 seconds
setnx key "dasdas"  If it already exists key If the mapping returns 0, it means unsuccessful> mset k1 v1 k2 v2 k3 v3  Batch setting
OK> keys *
1) "key"
2) "k1"
3) "key1"
4) "k3"
5) "k2">> mget k1 k2 k3 Batch acquisition
1) "v1"
2) "v2"
3) "v3"> mset k1 v1 k2 v2 k3 v3  Atomic success and failure together

Storage object
mset user:1:age 10 user:1:name asdasda
OK> mget user:1:age user:1:name
1) "10"
2) "asdasda"> 

Get before setting> getset db redis
(nil)> get db

A pipe can be added left and again, which is equivalent to two stacks

Left join data> lpush list one
(integer) 1> lpush list two
(integer) 2> lpush list three
(integer) 3> lrange list 0 1
1) "three"
2) "two"> llen list  length
(integer) 2> lset list 0 as take list Replace the values in the table below 0 with as´╝îIf there is no list, an error will be reported> rpush list vvv
(integer) 4> lrange list  0 -1
1) "three"
2) "two"
3) "one"
4) "vvv"> lpop list 
"three"> rpop list

Query values according to the following table> lindex list 0
"two"> lrem list 1 one   Removes a specified value one> ltrim list 0 1  list Only the truncated ones are left

set> sadd set1 da

sdiff key1 key2  Return to these two set Difference set of

sinter key1 key2  Return to these two set Intersection of

sunion key1 key2  Return to these two set Union of

Key map is more suitable for storing objects.> hset myhast k1 v1 k2 v2
(integer) 1> hget myhast k1
"v1"> hget myhast k1 k2> hgetall myhast 
1) "k1"
2) "v1"> hdel myhast k1  delete
(integer) 1> hlen myhast  Get length

Automatically sort the key s of number> zadd myset 1 one Add a value
(integer) 1> zadd myset 3 three 2 two  Add multiple values
(integer) 2> zrange myset 0 -1
1) "one"
2) "two"
3) "three"> zrangebyscore myset -inf +inf Sort output from small to large
1) "one"
2) "two"
3) "three"> zrangebyscore myset 0 -1 Sort output from large to small> zrangebyscore myset -inf +inf withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"> zrem myset one   Remove operation
(integer) 1> zrangebyscore myset -inf +inf
1) "two"
2) "three">> zcard myset   View length
(integer) 2

Three special types

Insert City longitude and latitude> geoadd china:city 116.397128 39.916527 beijing
(integer) 1> geoadd china:city 121.47 31 shanghai
(integer) 1> geoadd china:city 1.6 29 chongqing
(integer) 1> geoadd china:city 108.69 34 xian
(integer) 1

Get latitude and longitude> geopos china:city beijing
1) 1) "116.39712899923324585"
   2) "39.91652647362980844"

Query the straight-line distance between two points> geodist china:city beijing xian km
"949.1553"> georadius china:city 100 30 5000 km  //Look at all locations around 100 30 5000 km
1) "xian"
2) "shanghai"
3) "beijing"> georadius china:city 100 30 5000 km withdist //Plus display distance
1) 1) "xian"
   2) "932.1327"
2) 1) "shanghai"
   2) "2057.4183"
3) 1) "beijing"
   2) "1852.5507"> georadius china:city 100 30 5000 km withcoord
1) 1) "xian"
   2) 1) "108.68999987840652466"
      2) "34.00000062127011091"
2) 1) "shanghai"
   2) 1) "121.47000163793563843"
      2) "31.00000097648057817"
3) 1) "beijing"
   2) 1) "116.39712899923324585"
      2) "39.91652647362980844"> georadius china:city 100 30 5000 km withcoord count 2
1) 1) "xian"
   2) 1) "108.68999987840652466"
      2) "34.00000062127011091"
2) 1) "beijing"
   2) 1) "116.39712899923324585"
      2) "39.91652647362980844"> georadiusbymember china:city shanghai 5000 km//Designated city
1) "xian"
2) "shanghai"
3) "beijing"

The algorithm of cardinality statistics, for example, is useful when the website counts the number of visits, and the official error is 0.81%> pfadd mykey a b c d e f g
(integer) 1> pfcount mykey
(integer) 7> pfadd mykey2 a s i k h g f  t u i h
(integer) 1> pfmerge mykey3 mykey mykey2
OK> pfcount mykey3
(integer) 12

Bit storage (check the number of active people and check whether the user has clocked in. It can be used as long as there are only two statuses)> pfcount mykey3
(integer) 12> setbit sign 0 0
(integer) 0> setbit sign 1 0
(integer) 0> setbit sign 2 1
(integer) 0> setbit sign 3 0
(integer) 0> getbit sign 2
(integer) 1> bitcount sign  Statistics
(integer) 1

Transaction (Redis does not have the concept of isolation)
Transaction: essentially a set of commands (one-time, sequential, exclusive)
Redis single pick commands guarantee atomicity, but redis transactions do not guarantee atomicity, and transactions are not executed directly. They can only be executed when calling commands
Redis transaction:

  • Open transaction (multi)
  • Order to join the team
  • Execute transaction> multi
OK> set k1 v1
QUEUED> set k2 v2
QUEUED> get k1
QUEUED> exec
1) OK
2) OK
3) "v1">> set k1 v1
QUEUED> set k2 v2
QUEUED> discard   Abandon transaction

Compiled exceptions will cause all transactions to fail and all commands will not be executed
The syntax and logic are wrong at runtime, and other commands will be executed normally, so there is no atomicity

Pessimistic lock

Optimistic lock
Redis watch> set m 100
OK> set o 0
OK> watch m
OK> multi
OK> decrby m 20
QUEUED> incrby o 20
//If m is changed before execution, the transaction will fail to commit> exec
1) (integer) 80
2) (integer) 20

//When you run out, you can only remember unwatch> unwatch

Posted by Hillary on Sun, 15 May 2022 12:32:24 +0300