第一种方式(只使用Caffeine)
gradle添加依赖
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.3'
runtimeOnly 'mysql:mysql-connector-java'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation ( 'org.springframework.boot:spring-boot-starter-test' ) {
exclude group: 'org.junit.vintage' , module: 'junit-vintage-engine'
}
compile group: 'com.github.ben-manes.caffeine' , name: 'caffeine' , version: '2.8.4'
}
编写配置类
package org. example. base. config;
import com. github. benmanes. caffeine. cache. Cache;
import com. github. benmanes. caffeine. cache. Caffeine;
import org. springframework. context. annotation. Bean;
import org. springframework. context. annotation. Configuration;
import java. util. concurrent. TimeUnit;
@Configuration
public class CacheConfig {
@Bean ( value = "caffeineCache" )
public Cache< String, Object> caffeineCache ( ) {
return Caffeine. newBuilder ( )
. expireAfterWrite ( 60 , TimeUnit. SECONDS)
. initialCapacity ( 1000 )
. maximumSize ( 10000 )
. build ( ) ;
}
@Bean ( value = "caffeineCache2" )
public Cache< String, Object> caffeineCache2 ( ) {
return Caffeine. newBuilder ( )
. expireAfterWrite ( 120 , TimeUnit. SECONDS)
. initialCapacity ( 1000 )
. maximumSize ( 10000 )
. build ( ) ;
}
}
测试
package org. example. base;
import com. github. benmanes. caffeine. cache. Cache;
import org. example. base. bean. User;
import org. junit. jupiter. api. Test;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. beans. factory. annotation. Qualifier;
import org. springframework. boot. test. context. SpringBootTest;
@SpringBootTest
class BaseApplicationTests {
@Qualifier ( "caffeineCache" )
@Autowired
Cache< String, Object> cache;
@Qualifier ( "caffeineCache2" )
@Autowired
Cache< String, Object> cache2;
@Test
public void test ( ) {
User user = new User ( 1 , "张三" , 18 ) ;
cache. put ( "123" , user) ;
User user1 = ( User) cache. getIfPresent ( "123" ) ;
assert user1 != null;
System. out. println ( user1. toString ( ) ) ;
User user2 = ( User) cache2. getIfPresent ( "1234" ) ;
System. out. println ( user2 == null) ;
}
}
输出
第二种方式(使用Caffeine和spring cache)
gradle添加依赖
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.3'
runtimeOnly 'mysql:mysql-connector-java'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation ( 'org.springframework.boot:spring-boot-starter-test' ) {
exclude group: 'org.junit.vintage' , module: 'junit-vintage-engine'
}
compile group: 'com.github.ben-manes.caffeine' , name: 'caffeine' , version: '2.8.4'
compile ( 'org.springframework.boot:spring-boot-starter-cache' )
}
编写配置类
package org. example. base. config;
import com. github. benmanes. caffeine. cache. Caffeine;
import org. springframework. cache. CacheManager;
import org. springframework. cache. annotation. EnableCaching;
import org. springframework. cache. caffeine. CaffeineCache;
import org. springframework. cache. support. SimpleCacheManager;
import org. springframework. context. annotation. Bean;
import org. springframework. context. annotation. Configuration;
import java. time. Duration;
import java. util. ArrayList;
@Configuration
@EnableCaching
public class CacheConfig {
public enum CacheEnum {
FIRST_CACHE ( 300 , 20000 , 300 ) ,
SECOND_CACHE ( 60 , 10000 , 200 ) ;
private int second;
private long maxSize;
private int initSize;
CacheEnum ( int second, long maxSize, int initSize) {
this . second = second;
this . maxSize = maxSize;
this . initSize = initSize;
}
}
@Bean ( "caffeineCacheManager" )
public CacheManager cacheManager ( ) {
SimpleCacheManager cacheManager = new SimpleCacheManager ( ) ;
ArrayList< CaffeineCache> caffeineCaches = new ArrayList < > ( ) ;
for ( CacheEnum cacheEnum : CacheEnum. values ( ) ) {
caffeineCaches. add ( new CaffeineCache ( cacheEnum. name ( ) ,
Caffeine. newBuilder ( ) . expireAfterWrite ( Duration. ofSeconds ( cacheEnum. second) )
. initialCapacity ( cacheEnum. initSize)
. maximumSize ( cacheEnum. maxSize) . build ( ) ) ) ;
}
cacheManager. setCaches ( caffeineCaches) ;
return cacheManager;
}
}
编写service层
package org. example. base. service. impl;
import org. example. base. bean. User;
import org. example. base. service. UserService;
import org. springframework. boot. CommandLineRunner;
import org. springframework. cache. Cache;
import org. springframework. cache. CacheManager;
import org. springframework. stereotype. Service;
import javax. annotation. Resource;
@Service
public class UserServiceImpl implements UserService , CommandLineRunner {
@Resource ( name = "caffeineCacheManager" )
CacheManager cacheManager;
Cache cache1;
Cache cache2;
@Override
public void run ( String. . . args) throws Exception {
cache1 = cacheManager. getCache ( "FIRST_CACHE" ) ;
cache2 = cacheManager. getCache ( "SECOND_CACHE" ) ;
}
@Override
public void setUser ( User user) {
cache2. put ( "123" , user) ;
}
@Override
public User getUser ( String id) {
Cache. ValueWrapper valueWrapper = cache2. get ( id) ;
assert valueWrapper != null;
return ( User) valueWrapper. get ( ) ;
}
}
测试
package org. example. base;
import org. example. base. bean. User;
import org. example. base. service. UserService;
import org. junit. jupiter. api. Test;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. boot. test. context. SpringBootTest;
@SpringBootTest
class BaseApplicationTests {
@Autowired
private UserService userService;
@Test
public void test ( ) {
User user = new User ( 123 , "jack l" , 18 ) ;
userService. setUser ( user) ;
System. out. println ( userService. getUser ( "123" ) ) ;
}
}
输出结果
第三种方式(使用Caffeine和spring cache)
gradle依赖添加同方式二 配置类添加方式同方式二 编写service层
package org. example. base. service. impl;
import org. example. base. bean. User;
import org. example. base. service. UserService;
import org. springframework. cache. annotation. CacheConfig;
import org. springframework. cache. annotation. CacheEvict;
import org. springframework. cache. annotation. CachePut;
import org. springframework. cache. annotation. Cacheable;
import org. springframework. stereotype. Service;
@Service
public class UserServiceImpl implements UserService {
@Override
@CachePut ( key = "#user.getId()" , value = "SECOND_CACHE" , cacheManager = "caffeineCacheManager" )
public User setUser ( User user) {
System. out. println ( "已经存储进缓存了" ) ;
return user;
}
@Override
@CacheEvict ( value = "SECOND_CACHE" , cacheManager = "caffeineCacheManager" )
public void deleteUser ( Integer id) {
System. out. println ( "缓存删除了" ) ;
}
@Override
@Cacheable ( key = "#id" , value = "SECOND_CACHE" , cacheManager = "caffeineCacheManager" )
public User getUser ( Integer id) {
System. out. println ( "从数据库取值" ) ;
return null;
}
}
测试
package org. example. base;
import org. example. base. bean. User;
import org. example. base. service. UserService;
import org. junit. jupiter. api. Test;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. boot. test. context. SpringBootTest;
@SpringBootTest
class BaseApplicationTests {
@Autowired
private UserService userService;
@Test
public void test4 ( ) {
User user1 = new User ( 123 , "jack l" , 18 ) ;
userService. setUser ( user1) ;
System. out. println ( "从缓存中获取 " + userService. getUser ( 123 ) ) ;
System. out. println ( userService. getUser ( 123322222 ) ) ;
userService. deleteUser ( 123 ) ;
System. out. println ( userService. getUser ( 123 ) ) ;
}
}
输出结果