springboot+redis+QuartzConfig点赞功能的实现
时间: 2023-08-27 21:05:01 浏览: 170
首先,在pom.xml文件中添加redis和quartz依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
```
然后在application.properties文件中添加redis和quartz的配置:
```properties
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.database=0
# Quartz Configuration
org.quartz.scheduler.instanceName = QuartzScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.threadPool.threadCount = 10
```
接着,创建一个RedisService类来操作Redis:
```java
@Service
public class RedisService {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public Long incr(String key) {
return redisTemplate.opsForValue().increment(key);
}
public Long decr(String key) {
return redisTemplate.opsForValue().decrement(key);
}
public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
}
```
然后,创建一个LikeJob类来定时将点赞次数写入数据库:
```java
public class LikeJob implements Job {
@Autowired
private RedisService redisService;
@Autowired
private LikeService likeService;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
Object likeCount = redisService.get("like_count");
if (likeCount != null) {
int count = Integer.parseInt(likeCount.toString());
likeService.writeLikeCount(count);
redisService.set("like_count", 0);
}
}
}
```
最后,创建一个LikeController类来接收点赞请求:
```java
@RestController
public class LikeController {
@Autowired
private RedisService redisService;
@GetMapping("/like")
public String like() {
redisService.incr("like_count");
return "success";
}
}
```
这样,每次访问 /like 接口时,Redis中的like_count计数器就会加1,而定时任务会在固定时间将计数器的值写入数据库,并将计数器重置为0。这样就实现了点赞功能。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)