What are the ways to use Redis cache in SpringBoot
This article mainly introduces the relevant knowledge of "what is the use of Redis cache in SpringBoot". The editor shows you the operation process through an actual case, which is simple, fast and practical. I hope this article "what is the use of Redis cache in SpringBoot" can help you solve the problem.
1. Import the dependency org.springframework.boot spring-boot-starter-data-redis2 of Redis. Configure redis in application.properties # redis configuration # Redis server address spring.redis.host=127.0.0.1#Redis server connection port spring.redis.port=6379#Redis database index (default 0) spring.redis.database=0 # maximum number of connections in connection pool (no limit using negative values) maximum blocking wait time in spring.redis.jedis.pool.max-active=50# connection pool (using negative values Maximum idle connections in spring.redis.jedis.pool.max-wait=3000# connection pool minimum idle connection spring.redis.jedis.pool.min-idle=2# connection timeout in milliseconds spring.redis.timeout=50003. Write an encapsulated Redis utility class to facilitate the simple operation of Redis cache package com.example.demo.Util;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Component;@Componentpublic class RedisUtil {@ Autowired private RedisTemplate redisTemplate / * * read cache * @ param key * @ return * / public String get (final String key) {return redisTemplate.opsForValue () .get (key);} / * * write cache * @ param key * @ param value * @ return * / public boolean set (final String key,String value) {boolean result = false Try {redisTemplate.opsForValue () .set (key,value); result = true;} catch (Exception e) {e.printStackTrace ();} return result } / * Update cache * @ param key * @ param value * @ return * / public boolean update (final String key,String value) {boolean result = false; try {redisTemplate.opsForValue (). GetAndSet (key,value); result = true;} catch (Exception e) {e.printStackTrace () } return result;} / * delete cache * @ param key * @ return * / public boolean delete (final String key) {boolean result = false; try {redisTemplate.delete (key); result = true;} catch (Exception e) {e.printStackTrace ();} return result }} 4. Write test classes to test the results of the operation
Insert operation:
/ * insert a key-value pair with key of "username" and value of "supper" * / @ Testpublic void set () {redisUtil.set ("username", "supper");}
Running result:
127.0.0.1: 6379 > get username "supper"
Read operation:
/ * read the value with key as "username" * / @ Testpublic void get () {System.out.println (redisUtil.get ("username"));}
Running result:
Supper
Update operation:
Update the value of the key-value pair whose key is "username" to "chen" * / @ Testpublic void update () {redisUtil.update ("username", "chen");}
Running result:
127.0.0.1: 6379 > get username "chen"
Delete operation:
/ * Delete the key-value pair with key of "username" * / @ Testpublic void del () {redisUtil.delete ("username");}
Running result:
127.0.0.1: 6379 > get username (nil) on "what is the use of Redis cache in SpringBoot"? thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.