How to Use Redis and Quartz to Optimize Java Like/Collect Operations
This article explains how to reduce database pressure by counting likes and views in Redis, designing a hash‑based storage structure, synchronizing cached data to MySQL with Quartz, and marking articles as liked for each user, while also outlining current limitations.
Overall Idea
Because like and favorite actions occur frequently, writing to the database on each request would create high load. The solution is to use Redis to count likes and views, then periodically batch‑write the aggregated data to the relational database.
Redis Storage Design
The plan uses a Redis hash named blog_like. Each field is an article ID and its value is a set containing the user IDs that have liked the article. This allows constant‑time membership checks and easy counting of likes by the set size.
Implementation Workflow
1. The front‑end sends a like request.
2. The service retrieves the corresponding set from Redis; if it exists the new userId is added, otherwise a new set is created.
3. After the update the set is visible in Redis as a string.
Periodic Sync to Database
Quartz is used to schedule a job that transfers the cached likes to MySQL.
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>The job class extends QuartzJobBean and overrides executeInternal where the actual sync logic is placed.
public class LikeTask extends QuartzJobBean {
@Autowired
BlogService blogService;
@Autowired
RedisMapper redisMapper;
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
// sync logic here
}
}The Quartz configuration creates a JobDetail and a Trigger that runs every 20 seconds (adjustable).
@Configuration
public class QuartzConfig {
@Bean
public JobDetail quartzDetail_1() {
return JobBuilder.newJob(LikeTask.class)
.withIdentity("LIKE_TASK_IDENTITY")
.storeDurably()
.build();
}
@Bean
public Trigger quartzTrigger_1() {
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(20)
.repeatForever();
return TriggerBuilder.newTrigger()
.forJob(quartzDetail_1())
.withIdentity("USER_TRENDS_TRIGGER")
.withSchedule(scheduleBuilder)
.build();
}
}Marking Articles as Liked for the Current User
A transient field Liked is added to the entity (not persisted) to indicate whether the logged‑in user has liked the article.
// Used to indicate if the user has liked this blog
@TableField(exist = false)
private boolean Liked;When querying articles, the service checks the Redis set for the userId; if present, it sets Liked to true. The front‑end then displays the like icon accordingly.
Current Limitations
Insufficient mastery of Java collections and map interfaces.
Missing exception handling in several places.
Need deeper analysis of the order and consistency between Redis and database writes.
Utility class StringUtil may require further refinement.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
