I want to improve performance for api. I design cache in dao layer. I use caffine cache. Basic code is:
CacheConfig.java
@Configuration
public class CacheConfig {
@Autowired
private AppDao appDao;
@Bean(value = "appCache")
public LoadingCache<String, App> appCache() {
return Caffeine.newBuilder()
.maximumSize(1000)
.refreshAfterWrite(5, TimeUnit.MINUTES)
.build(key -> appDao.getByKeyFromDB(key));
}
}
AppDao.java
@Repository
public class AppDao {
@Autowired
@Qualifier(value = "appCache")
private LoadingCache<String, App> appCache;
@Autowired
private AppMapper appMapper;
// service call getByAppKey
public App getByKey(String appKey) {
App app = appCache.get(appKey);
return app;
}
// appCache load data by getByKey
public App getByKeyFromDB(String appKey) {
LambdaQueryWrapper<App> queryWrapper = Wrappers.lambdaQuery();
queryWrapper.eq(App::getAppKey, appKey);
return appMapper.selectOne(queryWrapper);
}
}
I maybe divide getByKey and getByKeyFromDB to two file, but I have no idea. How do I divide getByKey and getByKeyFromDB to different modules?