JavaWeb项目启动时加载热数据到缓存

前言

项目中为了提高系统响应速度经常把热数据存入缓存中如redis,memcached中。本文介绍一种使用的项目启动加载缓存的方法

基本配置

1.思路

应用启动时加载缓存常用的方法有:

  • 编写linstener监听应用启动
  • Bean实现InitializingBean接口,并实现afterPropertiesSet()方法
  • 在Bean的init-method中进行逻辑代码编写
  • 。。。。。

2.相关配置

本文采用实现InitializingBean接口,并实现afterPropertiesSet()方法的方式。
spring初始化bean的时候,如果bean实现了InitializingBean接口,会自动调用afterPropertiesSet方法,并且afterPropertiesSet方法优先于init-method方法执行

可参考Spring AbstractAutowireCapableBeanFactory中的实现

protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable {
        boolean isInitializingBean = bean instanceof InitializingBean;
        if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
            }

            if (System.getSecurityManager() != null) {
                try {
                    AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                        public Object run() throws Exception {
                            ((InitializingBean)bean).afterPropertiesSet();
                            return null;
                        }
                    }, this.getAccessControlContext());
                } catch (PrivilegedActionException var6) {
                    throw var6.getException();
                }
            } else {
                ((InitializingBean)bean).afterPropertiesSet();
            }
        }

        if (mbd != null) {
            String initMethodName = mbd.getInitMethodName();
            if (initMethodName != null && (!isInitializingBean || !"afterPropertiesSet".equals(initMethodName)) && !mbd.isExternallyManagedInitMethod(initMethodName)) {
                this.invokeCustomInitMethod(beanName, bean, mbd);
            }
        }

    }

3.代码实现

为了代码的可用性,设计CacheInterface接口

public interface CacheInterface {

    /**
     * 实现接口在应用启动时加载热数据
     *
     * @return
     */

    boolean loadCache();

    boolean deleteCache();

}

加载热数据时实现CacheInterface接口

@Service
public class DemoLoadCache implements CacheInterface {

    @Override
    public boolean loadCache() {

        //TODO  从关系型数据库中查询相关的数据并存储到缓存中。
        return true;
    }

    @Override
    public boolean deleteCache() {
        return false;
    }
}

将所有实现CacheInterface接口的加载热数据类注入到处理类中,实际应用中考虑使用线程池

@Service
public class CacheLoadService implements Runnable, InitializingBean {

    private static final Logger logger = LoggerFactory.getLogger(CacheLoadService.class);

    @Autowired
    private List<CacheInterface> cacheInterfaceList;


    @Override
    public void run() {
        logger.info("start load cache");
        long start = System.currentTimeMillis();
        if (cacheInterfaceList != null && cacheInterfaceList.size() > 0) {
            for (CacheInterface cacheInterface : cacheInterfaceList) {
                cacheInterface.loadCache();
            }
        }

        long end = System.currentTimeMillis();
        logger.info("load cache finished in {}秒 ", (end - start) / 1000);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Thread t = new Thread(this);
        t.start();
    }
}

Spring应用启动的时候,调用CacheLoadService中afterPropertiesSet方法启动线程加载热数据。

结尾

实际应用中考虑真是场景考虑加载数据的方式。对热数据的增删改也要同步到缓存中。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,974评论 6 342
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,010评论 19 139
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,378评论 11 349
  • 今天在奇葩说看到老罗的演讲:描述的是一个职场中的新人被认为跟老板有关系,所谓是关系户。那么谈到的一个重要观点是在职...
    ckcmp阅读 359评论 0 0
  • 文<经典> 北国的风雪 南国的思念 你不来我不走 向阳花跟着太阳 小草跟着春天 而我跟着你 多想拥有一座房子 大大...
    苦笑嫣然阅读 282评论 0 2