美文网首页
JavaWeb项目启动时加载热数据到缓存

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

作者: stevejobson | 来源:发表于2017-12-16 17:05 被阅读0次

前言

项目中为了提高系统响应速度经常把热数据存入缓存中如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方法启动线程加载热数据。

结尾

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

相关文章

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

    前言 基本配置 1.思路 应用启动时加载缓存常用的方法有: 编写linstener监听应用启动 Bean实现Ini...

  • 如何在项目启动时加载缓存

    最近在看公司的b2b项目,在项目启动的时候加载公司信息等缓存,实现的方式是新建一个servlet,复写servle...

  • SpringBoot 启动时让方法自动执行

    一、前言 在springBoot中我们有时候需要让项目在启动时提前加载相应的数据或者执行某个方法,那么实现提前加载...

  • 加载不同的 storyboard

    根据条件加载不同的 storyboard 项目启动时,根据条件加载不同的 storyboard,或加载 story...

  • atomic.Value代替sync.RWMutex

    记一次性能优化,读公司项目代码时候,发现好些使用sync.RWMutext的使用场景:项目启动时候对高频数据缓存到...

  • Dubbo服务暴露流程

    官方图 源码分析 当项目启动时,ClassPathXmlApplicationContext加载provider....

  • Redis的7个使用场景

    一:缓存——热数据 热点数据(经常会被查询,但是不经常被修改或者删除的数据),首选是使用redis缓存,毕竟强大到...

  • Redis的7个应用场景

    一:缓存——热数据 热点数据(经常会被查询,但是不经常被修改或者删除的数据),首选是使用redis缓存,毕竟强大到...

  • [SpringBoot]容器启动时,自动执行自定义方法

    一、为什么 在实际项目开发过程中,我们有时候需要让项目在启动时执行特定方法。如要实现这些功能: 提前加载相应的数据...

  • 项目启动后几种 初始化操作 方法

    当需要在项目启动时进行一些初始化操作,.例如初始化数据库、读取数据库加入缓存等,有以下几种方式。 Applicat...

网友评论

      本文标题:JavaWeb项目启动时加载热数据到缓存

      本文链接:https://www.haomeiwen.com/subject/lyaswxtx.html