Reference code github.com/zhang-xiaox…

First familiarize yourself with the basic commands of Redis (from network collection)

1-string Add, delete, modify, and query the String type:

### add key to name Value ay 127.0.0.1:6379> set name 'ay' OK ### query name 127.0.0.1:6379> get name "ay" ### update name al 127.0.0.1:6379> set 127.0.0.1:6379> get name "al" ### delete name 127.0.0.1:6379> del name (integer) 1 ### 0: does not exist 127.0.0.1:6379> Exists Name (integer) 0 127.0.0.1:6379>Copy the code

Add, delete, modify and check the set of 2-list:

Add, delete, alter, query List Add key to user_list 127.0.0.1:6379> lpush user_list 'ay' 'al' (integer) 2 ### query user_list 127.0.0.1:6379> Lrange user_list 0 -1 1) "al" 2) "ay" ### Add "love" to list 127.0.0.1:6379> rpush user_list 'love' (integer) 3 User_list 'hope' (integer) 4 ### query user_list 'hope' (key) 127.0.0.1:6379> lrange User_list 0 -1 1) "hope" 2) "al" 3) "ay" 4) "love" ### Update index 0 value 127.0.0.1:6379> lset user_list 0 'wish' OK 1) "wish" 2) "al" 3) "ay" 4) "love" ### delete from list where index = 0 127.0.0.1:6379> lrem user_list 0 'wish' (integer) 1 ### 2) "ay" 3) "love" 127.0.0.1:6379>Copy the code

 

Add, delete, change and check of 3-set Set:

127.0.0.1:6379> sadd user_set "ay" "al" "love" (integer) 3 ## select * from 'user_set' where 'al' = 'al' and 'ay' = 'love' 127.0.0.1:6379> srem user_set 'love' (integer) 1 ### Query all values of set 127.0.0.1:6379> smembers user_set 1) "al" 2) "ay" 127.0.0.1:6379> sadd user_set 'love' (integer) 1 127.0.0.1:6379> smembers user_set 1) "al" 2) "love" 3) "ay" But there is no error 127.0.0.1:6379> sadd user_set 'love' (integer) 0Copy the code

Add, delete, alter and check the 3-hash set:

Flushdb OK ### flushdb OK ### create hash user_hset user1 Ay 127.0.0.1:6379> hset user_hset "user1" "AY" (integer) 1 ### add user2 to key user_hset Value: al 127.0.0.1:6379> hset user_hset "user2" "al" (integer) 1 ### User_hset 1) "user1" 2) "user2" ### DELETE all values of user_hset (127.0.0.1:6379> hkeys User_hset 1) "ay" 2) "al" ### query user_hset 1 where key is set to 127.0.0.1:6379> hget user_hset "user1" "ay" ### 127.0.0.1:6379> hgetall user_hset 1) "user1" 2) "ay" 3) "user2" 4) "al" ### update the value of user1 to new_ay 127.0.0.1:6379> hset User_hset "user1" "new_ay" (INTEGER) 0 ### Update the value of user2 to new_al 127.0.0.1:6379> hset user_hset "user2" "new_al" (integer) 0 Hgetall user_hset 1) "user1" 2) "new_ay" 3) "user2" 4) "new_al" User_hset user1 (integer) 1 ### retrieve all fields and values 127.0.0.1:6379> hGEtall whose key is user_hset User_hset 1) "user2" 2) "new_al" 127.0.0.1:6379>Copy the code

4- Add, delete, change and check SortedSet

127.0.0.1:6379> flushdb OK Zadd user_zset 1 "ay" (integer) 1 ###SortedSet Zadd user_zset 2 "al" (integer) 1 ###SortedSet 127.0.0.1:6379> zadd user_zset 3 "love" (integer) 1 ### Query user_zset set elements in ascending order by score 127.0.0.1:6379> zrange user_zset 0 Zrevrange user_zset 0-1 1) "love" 2) "al" 3) "al" 3) Zscore user_zset "ay" "1" ### query "ay" user_zset "love" "3"Copy the code

Springboot2 integrate redis

First add dependencies

<! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>Copy the code

Then do the cache configuration (basic configuration, just getting started testing, haha)

Redis. database=0 ### localhost spring.redis.host=localhost ### Port =6379 ### redis Password defaults to empty spring.redis.password=Copy the code

First, solve the problem of storing data to cache with RedisTemplate garble (the Redis client displays hexadecimal keys and values or simply garble), and write the configuration class in a package (equivalent to configuring a bean with XML; SpirngBoot recommends Java code configuration).

Actually garbled question also can run through it, because it is the result of the serialization, after all, in the network transmission, but for the sake of intuitive, so you can take in such a way that enhanced readability, generally put the key into readable, and the value generally do not need to (so as not to read from the redis instead to the embarrassment of object cannot be converted to String type), a word Generalize, garble is garble.

//package com.demo.redis.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; * @author 10905 2019/2/24 * @version 1.0 */ @configuration public class RedisConfig { @Autowired private RedisTemplate redisTemplate; @Bean public RedisTemplate<String, Object> stringSerializerRedisTemplate() { RedisSerializer<String> stringSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringSerializer); redisTemplate.setValueSerializer(stringSerializer); redisTemplate.setHashKeySerializer(stringSerializer); redisTemplate.setHashValueSerializer(stringSerializer); return redisTemplate; }}Copy the code

Write a class test in the test class.

//package com.demo.redis; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @runwith (springrunner.class) @springbooTtest Public Class RedisTests {/** * basic type of template */ @resource private RedisTemplate redisTemplate; @Autowired private StringRedisTemplate stringRedisTemplate; / * * * Test basic connection redis read string data and query * / @ new Test public void test01 () {/ / increased redisTemplate opsForValue (). The set (" name ", "xiao-xiang zhang"); redisTemplate.opsForValue().set("age", "18"); / / increase (key/value pair will overwrite the previous) redisTemplate. OpsForValue (). The set (" name ", "xiao-xiang zhang 2"); redisTemplate.opsForValue().set("age", "19"); / / query String name = (String) redisTemplate. OpsForValue () get (" name "); String age = (String) redisTemplate.opsForValue().get("age"); System.out.println(" query to cache data: "+ name +" "+ age); } /** * delete */ @test public void test02() {Boolean flag = redistemplate.delete ("age"); If (flag) {system.out. println(" Age deleted successfully "); } else {system.out. println(" Delete age failed!") ); }} / modify * / * * * @ Test public void test03 () {/ / modify key redisTemplate. OpsForValue (). The set (" name ", "long grass YanTuanZi"); System. The out. Println (" revised the name is: "+ redisTemplate. OpsForValue () get (" name")); }}Copy the code

Other redistemplates define operations on five data structures

redisTemplate.opsForList(); . / / the list redisTemplate opsForValue (); / / operation string redisTemplate opsForCluster (); // Use redistemplate.opsforgeo () for clustering; // Use redistemplate.opsForhash () for geographic locations; // Hash redistemplate.opsForset (); // set redistemplate.opsforzSet (); // set in orderCopy the code