1. Introduction
1. Scenario
Since the data dictionary does not change very frequently, and the system accesses the data dictionary more frequently, it is necessary for us to store the data in the data dictionary. into the cache to reduce database pressure and increase access speed. Here, we use Redis as the distributed cache middleware of the system.
2. RedisTemplate
In the Spring Boot project, Spring Data Redis is integrated by default. Spring Data Redis provides a very convenient operation template for Redis, RedisTemplate, and can automatically manage the connection pool.
2. Introduction of Redis
1. Integrate Redis in the project
Add redis dependency in the service-base module. Spring Boot 2.0 and above connect to Redis through the commons-pool2 connection pool by default
<!-- spring boot redis缓存引入 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- 缓存连接池--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <!-- redis 存储 json序列化 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> </dependency>
2. Add Redis connection configuration
Add the following configuration to application.yml of service-core
#spring:
redis:
host: 192.168.100.100
port: 6379
database: 0
password: 123456 #Default is empty
timeout: 3000ms #Maximum waiting time, an exception will be thrown if it times out, otherwise the request will wait forever
lettuce:
pool:
max-active: 20 #Maximum number of connections, negative value means no limit, default is 8
max-wait: -1 #Maximum blocking waiting time, negative value means no limit, Default -1
max-idle: 8 #Maximum idle connection, default 8
min-idle: 0 #Minimum idle connection, default 0
3. Start Redis service
Connect to the Linux server remotely, here use redis on the centos virtual machine locally
#Start the service
cd /usr/local/redis-5.0.7
bin/redis- server redis.conf
3. Test RedisTemplate
1. Store value test
Create test class RedisTemplateTests
@SpringBootTest @RunWith(SpringRunner.class) public class RedisTemplateTests { @Resource private RedisTemplate redisTemplate; @Resource private DictMapper dictMapper; @Test public void saveDict(){ Dict dict = dictMapper.selectById(1); //向数据库中存储string类型的键值对, 过期时间5分钟 redisTemplate.opsForValue().set("dict", dict, 5, TimeUnit.MINUTES); } }
found that RedisTemplate defaults The JDK serialization method is used to store the key and value, which has poor readability
2. Add RedisConfig to the Redis configuration file
service-base. We You can configure the Redis serialization scheme in this configuration file
@Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); //首先解决key的序列化方式 StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringRedisSerializer); //解决value的序列化方式 Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); //序列化时将类的数据类型存入json,以便反序列化的时候转换成正确的类型 ObjectMapper objectMapper = new ObjectMapper(); //objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL); // 解决jackson2无法反序列化LocalDateTime的问题 objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.registerModule(new JavaTimeModule()); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); return redisTemplate; } }
Test again, the key uses string storage, and the value uses json storage
3. Value test
@Test public void getDict(){ Dict dict = (Dict)redisTemplate.opsForValue().get("dict"); System.out.println(dict); }
4. Store the data dictionary in redis
DictServiceImpl
Note: When the redis server goes down, we should not throw an exception and execute it normally. The following process enables the business to run normally
@Resource private RedisTemplate redisTemplate; @Override public List<Dict> listByParentId(Long parentId) { //先查询redis中是否存在数据列表 List<Dict> dictList = null; try { dictList = (List<Dict>)redisTemplate.opsForValue().get("srb:core:dictList:" + parentId); if(dictList != null){ log.info("从redis中取值"); return dictList; } } catch (Exception e) { log.error("redis服务器异常:" + ExceptionUtils.getStackTrace(e));//此处不抛出异常,继续执行后面的代码 } log.info("从数据库中取值"); dictList = baseMapper.selectList(new QueryWrapper<Dict>().eq("parent_id", parentId)); dictList.forEach(dict -> { //如果有子节点,则是非叶子节点 boolean hasChildren = this.hasChildren(dict.getId()); dict.setHasChildren(hasChildren); }); //将数据存入redis try { redisTemplate.opsForValue().set("srb:core:dictList:" + parentId, dictList, 5, TimeUnit.MINUTES); log.info("数据存入redis"); } catch (Exception e) { log.error("redis服务器异常:" + ExceptionUtils.getStackTrace(e));//此处不抛出异常,继续执行后面的代码 } return dictList; }
Summary of integrated redis:
(1) Import related dependencies;
(2) Configure redis connection information;
(3) Test connection, value test, and value storage test;
(4) Configure the serializer according to your own needs, otherwise the jdk serializer will be used by default.
redis business summary:
(1) First query whether there is corresponding cache information in redis, and if there is, it will be retrieved and returned directly without execution (2), if redis is connected for some reason If there is no downtime, print the error log at this time and continue to query the database;
(2) If not, query the database, store the data in redis and return the data.
The above is the detailed content of Redis cache example code analysis. For more information, please follow other related articles on the PHP Chinese website!

Redis goes beyond SQL databases because of its high performance and flexibility. 1) Redis achieves extremely fast read and write speed through memory storage. 2) It supports a variety of data structures, such as lists and collections, suitable for complex data processing. 3) Single-threaded model simplifies development, but high concurrency may become a bottleneck.

Redis is superior to traditional databases in high concurrency and low latency scenarios, but is not suitable for complex queries and transaction processing. 1.Redis uses memory storage, fast read and write speed, suitable for high concurrency and low latency requirements. 2. Traditional databases are based on disk, support complex queries and transaction processing, and have strong data consistency and persistence. 3. Redis is suitable as a supplement or substitute for traditional databases, but it needs to be selected according to specific business needs.

Redisisahigh-performancein-memorydatastructurestorethatexcelsinspeedandversatility.1)Itsupportsvariousdatastructureslikestrings,lists,andsets.2)Redisisanin-memorydatabasewithpersistenceoptions,ensuringfastperformanceanddatasafety.3)Itoffersatomicoper

Redis is primarily a database, but it is more than just a database. 1. As a database, Redis supports persistence and is suitable for high-performance needs. 2. As a cache, Redis improves application response speed. 3. As a message broker, Redis supports publish-subscribe mode, suitable for real-time communication.

Redisisamultifacetedtoolthatservesasadatabase,server,andmore.Itfunctionsasanin-memorydatastructurestore,supportsvariousdatastructures,andcanbeusedasacache,messagebroker,sessionstorage,andfordistributedlocking.

Redisisanopen-source,in-memorydatastructurestoreusedasadatabase,cache,andmessagebroker,excellinginspeedandversatility.Itiswidelyusedforcaching,real-timeanalytics,sessionmanagement,andleaderboardsduetoitssupportforvariousdatastructuresandfastdataacces

Redis is an open source memory data structure storage used as a database, cache and message broker, suitable for scenarios where fast response and high concurrency are required. 1.Redis uses memory to store data and provides microsecond read and write speed. 2. It supports a variety of data structures, such as strings, lists, collections, etc. 3. Redis realizes data persistence through RDB and AOF mechanisms. 4. Use single-threaded model and multiplexing technology to handle requests efficiently. 5. Performance optimization strategies include LRU algorithm and cluster mode.

Redis's functions mainly include cache, session management and other functions: 1) The cache function stores data through memory to improve reading speed, and is suitable for high-frequency access scenarios such as e-commerce websites; 2) The session management function shares session data in a distributed system and automatically cleans it through an expiration time mechanism; 3) Other functions such as publish-subscribe mode, distributed locks and counters, suitable for real-time message push and multi-threaded systems and other scenarios.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

WebStorm Mac version
Useful JavaScript development tools
