Redis learning notes-Redis basics and integration
-
NoSQL classification
NoSQL (referring to non-relational databases in general) is divided into the following four categories:
- Key-Value storage database: Redis
- Column store database
- Document database
- Graph database
-
Getting started with Redis
-
What is Redis
Redis (Remote Dictionary Server), the remote dictionary service, is an open source log-based, Key-Value database written in ANSI C language, supporting the network, memory-based or persistent, and providing APIs in multiple languages.
-
What can Redis do
- Memory storage, persistence (rdb, aof)
- High efficiency, can be used for cache
- Publish and Subscribe System
- Map information analysis
- Timer, counter (views 1)
- …
-
characteristic
- Various data types
- Endurance
- Cluster
- Affairs
- …
Official website: https://redis.io/
Recommend to develop on linux
-
-
Redis installation
- window github:https://github.com/tporadowski/redis/releases
- linux abbreviation
-
Redis use
- Switch database | SELECT 1 There are 16 databases by default
- View database size | DBSIZE [-n]
- View all keys in the database| KEYS *
- Clear the database | FLUSHDB /FLUSHALL
- Check if the key exists | EXISTS [key name]
- Move key to a database | MOVE KEY -n
- Set the expiration time of the key | EXPIRE [key name] -s
- View key expiration time | TTL [key name]
- View the type of key | TYPE [key name]
- Set expiration time-EXPIRE Set expiration time
-
Redis five data types
- strings
- APPEND NAME VALUE append string
- STRLEN string length
- INCR | DECR KEY2 (integer only)
- RANGE intercept string GETRANGE replace SETRANGE
- setex ,setnx set the expiration time, the setting does not exist to create
- MSET MGET
- Object
- hashes
- HSET HGET HGETALL Set to get data
- HMGET HMSET
- HDEL delete the specified key
- HLEN gets the field constant of the hash
- HEXISTS HASH FIELD Determine whether the field exists
- HKEYS / HVALS get all key / values
- HINCRBY
- HSET
- lists
- LINDEX positioning
- [L/R]PUSH POP add delete
- LREM remove
- LTRIM trim
- LRANGE range
- LSET replaces the value of the specified subscript
- LINSERT insert
- sets
- SADD increase
- SMEMBERS View set members
- SMOVE moves the value of a set to another set
- SISMEMBER determines whether the value is in the set
- SDIFF SET1 SET difference set
- SINTER SET1 SET intersection
- SUNION SET1 SET union
- SCARD gets the number of collection elements
- Zset adds a value on the basis of set
- ZADD
- ZRANGE MYSET 0 -1
- ZRANGEBYSCORE MYSET -INF +INF WITHSCORES Sort from small to large
- ZREVRANGE MYSET 0 -1 WITHSCORES Sort from high to low
- Remove the specified element ZREM
- ZCARD gets the number of elements in an ordered set
- ZCOUNT MYSET 0 1 Get the specified number of intervals
- strings
-
Three special data types
-
geospatial zset
- GEOADD Add geographic location (longitude, latitude, name) to the specified key
GEOADD chia:city 116.40 30.90 beijin
GEOPOS gets the longitude and latitude of the specified city
GEODIST chia:city beijin shanghai km
- m for meters.
- km for kilometers.
- mi for miles.
- ft for feet.
GEORADIUS chia:city 100 30 400 km
-
hyperloglogs base
- PFADD KEY A B C D
- PFCOUNT KEY
- PFMERGE KEY1 KEY KEY2 merged into KEY1
-
Bitmaps bit storage
- SETBIT GETBIT
- BITCOUNT
-
-
Redis transaction
-
Redis single instruction guarantees atomicity, transaction does not guarantee atomicity
-
Essence: a set of commands Exec
-
redis transaction
- Open transaction multi
- Order to join the team. .
- Execute transaction exec
-
Discard transaction
-
Compilation type exception code error, the command of the transaction will not be executed if the compilation fails
-
Abnormal during runtime, other commands are executed normally
-
Monitoring watch
- Pessimistic lock
- Lock anytime
- Optimistic lock
- Will not be locked when updating data
- Get version
- Compare version when updating, using watch can be used as redis optimistic lock operation
- Pessimistic lock
-
-
Jedis
- java connect redis tool
<!-- jedis--> <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.5.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.75</version> </dependency>
-
springboot integration
- Import launcher dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>