与SpringBoot整合
原创大约 3 分钟
Springboot可以通过两种方式集成Caffeine。
依赖方式
引入依赖。
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
可以在application.properties
中加入缓存配置,不过这一步是可选的。
## CAFFEINE
customer.caffeine.initialCapacity=50
customer.caffeine.maximumSize=500
customer.caffeine.expireAfterWrite=86400
创建配置类。
package com.xiangwang.commons.configure;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* 缓存配置类
*
*/
@Configuration
@Component
public class WebConfiguration extends WebMvcAutoConfiguration {
@Bean(name = "cache")
public LoadingCache<String, String> cache() {
return Caffeine.newBuilder()
.initialCapacity(1024)
.maximumSize(1024)
.expireAfterWrite(1, TimeUnit.DAYS)
.build(key -> "default");
}
}
再编写缓存服务类就可以了。
package com.xiangwang.commons.configure;
import com.github.benmanes.caffeine.cache.LoadingCache;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* 缓存服务类
*
*/
@Service
public class CacheService {
@Resource
private LoadingCache<String, String> cache;
/**
* 保存缓存数据
*
*/
public void setCache(final String key, final String value) {
cache.put(key, value);
}
/**
* 读取缓存数据
*
*/
public String getCache(final String key) {
return cache.get(key);
}
/**
* 清除缓存数据
*
*/
public void clearCache(final String key) {
cache.invalidate(key);
}
}
注解方式
注解方式也需要引入依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
在application.properties
中加入缓存配置,不过这一步也是可选的。
## CAFFEINE
spring.cache.cache-names=test
spring.cache.type=caffeine
spring.cache.caffeine.spec=initialCapacity=50,maximumSize=500,expireAfterWrite=300s
首先定义实体类。
package com.xiangwang.commons.entity;
/**
* 用户实体类
*
*/
public class User {
private String name;
private Integer age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return name;
}
}
然后创建服务类。
package com.xiangwang.commons.service;
import com.xiangwang.commons.entity.User;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import javax.annotation.Resource;
/**
* 用户Service
*
*/
public class UserService {
@Resource
private MySQLDao mysqlDao;
/**
* 将新增的用户信息放入缓存
*
*/
@CachePut(value = "user", key = "#id")
public int addUser(final int id, final String username) {
String sql = "INSERT INTO user(id, username) VALUES(?, ?)";
try {
return mysqlDao.create(sql, id, username);
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
/**
* 从缓存读取用户信息
*
*/
@Cacheable(value = "user", key = "#id")
public User getUserById(final int id) {
// 这里的id正是从缓存中读出来的
System.out.println("从数据库读取用户:" + id);
String sql = "SELECT * FROM user WHERE id = ?";
return (User) mysqlDao.find(sql, new User(), id);
}
}
最后创建控制器类。
package com.xiangwang.commons.controller;
import com.xiangwang.commons.entity.User;
import com.xiangwang.commons.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* 用户Controller
*
*/
@RestController
public class UserController {
@Resource
private UserService userService;
/**
* 添加用户
*
*/
@PostMapping("/api/user/add/v1.0")
public String add(final int id, final String username) {
userService.addUser(id, username);
return "success";
}
/**
* 查询用户
*
*/
@PostMapping("/api/user/get/v1.0")
public String get(final int id) {
User user = userService.getUserById(id);
return user.getName();
}
}
先添加用户,再分别通过ID查询。
当超过设定的300s
后,再次读取又会从MySQL查询。
使用注解的方式简单、快速,但缺点是不能灵活操控,例如,无法执行异步存储以及无法查看统计信息。
感谢支持
更多内容,请移步《超级个体》。