• notice
  • Congratulations on the launch of the Sought Tech site

How to achieve high performance praise

Like is a small module of the whole system, and the code is under  user-service user service.

This article is based on SpringCloud. After users initiate likes and cancel likes, they are first stored in Redis, and then the like data is read from Redis and written to the database for persistent storage every two hours.

The like function is available in many systems, but despite the small function, there are quite a lot of things to consider if you want to do it well.

Upvoting and canceling likes are high-frequency operations. If the database is read and written every time, a large number of operations will affect the performance of the database, so caching is required.

As for how often to retrieve data from Redis and store it in the database, it depends on the actual situation of the project. I temporarily set it to two hours.

Project requirements need to check who has liked, so it is necessary to store the likes and likes of each like, and cannot simply count.

The article is divided into four parts:

  • Redis cache design and implementation

  • Database Design

  • database operation

  • Enable the persistent storage of scheduled tasks to the database

1. Redis cache design and implementation

1.1 Redis installation and operation

Please refer to the relevant tutorials for Redis installation.

Let’s talk about Docker installation and running Redis

docker run -d -p 6379:6379 redis:4.0.8

If Redis has been installed, open the command line and enter the command to start Redis

redis-server

1.2 Integration of Redis and SpringBoot projects

1.  pom.xml Introduce dependencies in

<dependency> <groupId>org.springframework.boot</groupId> < artifactId >spring-boot-starter-data-redis</artifactId> </dependency>

2. Add annotations to the startup class @EnableCaching

@SpringBootApplication @EnableDiscoveryClient @EnableSwagger2 @EnableFeignClients(basePackages = "com.solo.coderiver.project.client") @EnableCaching public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args) ; } }

3. Write Redis configuration class RedisConfig


import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework .context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data .redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import java.net.UnknownHostException; @Configuration public class RedisConfig { @Bean @ConditionalOnMissingBean(name = "redisTemplate") public RedisTemplate<String, >redisTemplate( RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class); ObjectMapper om = new ObjectMapper(); enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(redisConnectionFactory); template.setKeySerializer(jackson2JsonerizerRedis) setValueSerializer(jackson2JsonRedisSerializer); template.setHashKeySerializer(jackson2JsonRedisSerializer); template.setHashValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet();return template; } @Bean @ConditionalOnMissingBean(StringRedisTemplate.class) public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate();

So far, the configuration of Redis in the SpringBoot project has been completed and can be used happily.


Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+