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.. . . . .
install
windows
1. Download https://github.com/MicrosoftArchive/redis/releases
2. Decompression
3. Start:
4. Use the redis client to connect to redis, and the connection is successful
Linux
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
127.0.0.1:6379> getrange key 5 10 Intercept string "dasdas" 127.0.0.1:6379> getrange key 0 -1 View all strings "adadsdasdasdas" 127.0.0.1:6379> setrange key 1 mko Replace intermediate characters (integer) 14 127.0.0.1:6379> get key "amkosdasdasdas" setex key "dasdas" 60 Existence time: 60 seconds setnx key "dasdas" If it already exists key If the mapping returns 0, it means unsuccessful 127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 Batch setting OK 127.0.0.1:6379> keys * 1) "key" 2) "k1" 3) "key1" 4) "k3" 5) "k2" 127.0.0.1:6379> 127.0.0.1:6379> mget k1 k2 k3 Batch acquisition 1) "v1" 2) "v2" 3) "v3" 127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 Atomic success and failure together Storage object mset user:1:age 10 user:1:name asdasda OK 127.0.0.1:6379> mget user:1:age user:1:name 1) "10" 2) "asdasda" 127.0.0.1:6379> Get before setting 127.0.0.1:6379> getset db redis (nil) 127.0.0.1:6379> get db "redis"
List
A pipe can be added left and again, which is equivalent to two stacks
Left join data 127.0.0.1:6379> lpush list one (integer) 1 127.0.0.1:6379> lpush list two (integer) 2 127.0.0.1:6379> lpush list three (integer) 3 127.0.0.1:6379> lrange list 0 1 1) "three" 2) "two" 127.0.0.1:6379> llen list length (integer) 2 127.0.0.1:6379> 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 127.0.0.1:6379> rpush list vvv (integer) 4 127.0.0.1:6379> lrange list 0 -1 1) "three" 2) "two" 3) "one" 4) "vvv" 127.0.0.1:6379> lpop list "three" 127.0.0.1:6379> rpop list "vvv" Query values according to the following table 127.0.0.1:6379> lindex list 0 "two" 127.0.0.1:6379> lrem list 1 one Removes a specified value one 127.0.0.1:6379> ltrim list 0 1 list Only the truncated ones are left
set
127.0.0.1:6379> 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
Hash
Key map is more suitable for storing objects.
127.0.0.1:6379> hset myhast k1 v1 k2 v2 (integer) 1 127.0.0.1:6379> hget myhast k1 "v1" 127.0.0.1:6379> hget myhast k1 k2 127.0.0.1:6379> hgetall myhast 1) "k1" 2) "v1" 127.0.0.1:6379> hdel myhast k1 delete (integer) 1 127.0.0.1:6379> hlen myhast Get length
Zste
Automatically sort the key s of number
127.0.0.1:6379> zadd myset 1 one Add a value (integer) 1 127.0.0.1:6379> zadd myset 3 three 2 two Add multiple values (integer) 2 127.0.0.1:6379> zrange myset 0 -1 1) "one" 2) "two" 3) "three" 127.0.0.1:6379> zrangebyscore myset -inf +inf Sort output from small to large 1) "one" 2) "two" 3) "three" 127.0.0.1:6379> zrangebyscore myset 0 -1 Sort output from large to small 127.0.0.1:6379> zrangebyscore myset -inf +inf withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "three" 6) "3" 127.0.0.1:6379> zrem myset one Remove operation (integer) 1 127.0.0.1:6379> zrangebyscore myset -inf +inf 1) "two" 2) "three" 127.0.0.1:6379> 127.0.0.1:6379> zcard myset View length (integer) 2
Three special types
geospatial
Insert City longitude and latitude 127.0.0.1:6379> geoadd china:city 116.397128 39.916527 beijing (integer) 1 127.0.0.1:6379> geoadd china:city 121.47 31 shanghai (integer) 1 127.0.0.1:6379> geoadd china:city 1.6 29 chongqing (integer) 1 127.0.0.1:6379> geoadd china:city 108.69 34 xian (integer) 1
Get latitude and longitude 127.0.0.1:6379> geopos china:city beijing 1) 1) "116.39712899923324585" 2) "39.91652647362980844"
Query the straight-line distance between two points 127.0.0.1:6379> geodist china:city beijing xian km "949.1553"
127.0.0.1:6379> georadius china:city 100 30 5000 km //Look at all locations around 100 30 5000 km 1) "xian" 2) "shanghai" 3) "beijing" 127.0.0.1:6379> 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" 127.0.0.1:6379> 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" 127.0.0.1:6379> 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" 127.0.0.1:6379> georadiusbymember china:city shanghai 5000 km//Designated city 1) "xian" 2) "shanghai" 3) "beijing"
hyperloglog
The algorithm of cardinality statistics, for example, is useful when the website counts the number of visits, and the official error is 0.81%
127.0.0.1:6379> pfadd mykey a b c d e f g (integer) 1 127.0.0.1:6379> pfcount mykey (integer) 7 127.0.0.1:6379> pfadd mykey2 a s i k h g f t u i h (integer) 1 127.0.0.1:6379> pfmerge mykey3 mykey mykey2 OK 127.0.0.1:6379> pfcount mykey3 (integer) 12
BitMap
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)
127.0.0.1:6379> pfcount mykey3 (integer) 12 127.0.0.1:6379> setbit sign 0 0 (integer) 0 127.0.0.1:6379> setbit sign 1 0 (integer) 0 127.0.0.1:6379> setbit sign 2 1 (integer) 0 127.0.0.1:6379> setbit sign 3 0 (integer) 0 127.0.0.1:6379> getbit sign 2 (integer) 1 127.0.0.1:6379> 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
127.0.0.1:6379> multi OK 127.0.0.1:6379> set k1 v1 QUEUED 127.0.0.1:6379> set k2 v2 QUEUED 127.0.0.1:6379> get k1 QUEUED 127.0.0.1:6379> exec 1) OK 2) OK 3) "v1" 127.0.0.1:6379> 127.0.0.1:6379> set k1 v1 QUEUED 127.0.0.1:6379> set k2 v2 QUEUED 127.0.0.1:6379> 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
127.0.0.1:6379> set m 100 OK 127.0.0.1:6379> set o 0 OK 127.0.0.1:6379> watch m OK 127.0.0.1:6379> multi OK 127.0.0.1:6379> decrby m 20 QUEUED 127.0.0.1:6379> incrby o 20 QUEUED //If m is changed before execution, the transaction will fail to commit 127.0.0.1:6379> exec 1) (integer) 80 2) (integer) 20 //When you run out, you can only remember unwatch 127.0.0.1:6379> unwatch