I want to improve performance for api. I design cache in dao layer. I use caffine cache. Basic code is:


public class CacheConfig {

    private AppDao appDao;

    @Bean(value = "appCache")
    public LoadingCache<String, App> appCache() {
        return Caffeine.newBuilder()
                .refreshAfterWrite(5, TimeUnit.MINUTES)
                .build(key -> appDao.getByKeyFromDB(key));


public class AppDao {

    @Qualifier(value = "appCache")
    private LoadingCache<String, App> appCache;

    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?


1 Answer 1


Well if you want to distinct classes, you should create two classes:

class CahedDao {
  AppCache cache;

class DbDao {


A. Codereview is to review working code, if you want to solve a problem you should better ask on stackexchange.

B. There are some issues in your code. Thanks to the DI and (bad) field injection you did not got them.

  • \$\begingroup\$ Thank you! What is the issues? \$\endgroup\$ May 11, 2021 at 10:57
  • \$\begingroup\$ You have a dependency loop, CacheConfig needs an AppDao to produce appCache and AppDao needs and appCache. \$\endgroup\$
    – gervais.b
    May 11, 2021 at 11:30
  • \$\begingroup\$ Thanks! I got it. \$\endgroup\$ May 11, 2021 at 12:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.