Java缓存和动态更新 [英] Java cache and update dynamically

查看:3006
本文介绍了Java缓存和动态更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在servlet启动时从数据库预加载一些数据。

I need to "preload" some data from a database on servlet startup.

所以我想创建一些缓存,例如使用 HashMap 或类似的同步版本。

So I thought to create some cache e.g. using a HashMap or some similar synchronized version.

我还需要更新数据库更新更改的缓存。

所以我想添加某种监听器。

I also need to update the cache on database update changes.
So I thought to add some kind of "listener".

我的问题是:这是以某种方式提供还是我必须实际实现它?

My question is: is this somehow available or do I have to actually implement it?

如果是,那么最好的设计模式是什么?

If yes what design pattern would be the best approach here?

更新:

未使用JPA或ORM。但Spring是可用的

Update:
No JPA or ORM used. But Spring is available

推荐答案

当然可以实现那个

我会画一个小的架构然后生病解释给你:

Yes of course you can implement that
I'll draw a small architecture then ill explain it to u:

首先,您可以了解Mappers 这里和TDG 此处
映射器有一个名为 cacheAll()的方法,它调用并委托给TDG的方法 cacheAll(),后者的任务是获取所有行db中的表(要在缓存对象中缓存的行)。

first of all , you can learn about Mappers here and TDGs here. A mapper has a method called cacheAll() which calls and delegate to TDG's method cacheAll() which in its turn has a mission to get all rows from a table from the db( the rows you want to cache in the cache object).

所以基本上首先必须创建一个实现ServletContextListener的侦听器
表示它是整个servlet上下文的监听器,在 contextInitialized 中你必须调用 mp.fill(Mapper.cacheAll()),所以它很像(这是通用代码,当然写得更好并优化它)

so basically first you have to create a listener implementing "ServletContextListener" which means its a listener for the whole servlet context, and inside its contextInitialized you have to call mp.fill(Mapper.cacheAll()), so it is sthg like ( this is general code, of course write it better and optimize it)

public class myServletContextListener implements ServletContextListener{

@Override
public void contextInitialized(ServletContextEvent sce) {
        mp.fill(Mapper.cacheAll());
 }

//
}

Don不要忘记在web.xml中添加你的监听器:

Don't forget to add your listener in web.xml:

<listener>
    <listener-class>myServletContextListener </listener-class>
</listener>

所以这将做什么,是在服务器启动时,将所有记录缓存到一个hashmap mp在缓存对象中。

so what this will do , is on startup of the server, will cache all record into a hashmap mp in a cache object.

至于根据数据库更改更新缓存,您必须使用观察者模式

As for updating cache based on database change, you will have to use observer pattern.

更新

我忘记提及,关于缓存对象,我假设你希望所有用户或您的应用都可以访问它,因此您应该将其编码为单例(单例模式),代码如下:

UPDATE
I forgot to mention, about the cache object, i assume you want it accessible for all users or your app, so you should code it as a singleton (singleton pattern), code like that:

 public class cacheObject
{
    private static Map cMap;
    private static cacheObject cObject;
    private cacheObject()
    {
        cMap = Mapper.cacheAll();
    }
    public static synchronized cacheObject getInstance()
    {
        if (cObject == null){
            cObject = new cacheObject();
        }
        return cObject;
    }

}

此外,如果你想要的数据用户可以更改进行缓存,因此请将其设为 Threadlocal 单例。

Also if the data that you want to cache can be changed by users, so make it a Threadlocal singleton.

这篇关于Java缓存和动态更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆