简体   繁体   English

如何实现Java Web应用程序属性缓存?

[英]How to implement java web application properties caching?

I have simple web application built with servlets 3.0 and Ajax, no freamwork is being used. 我有一个使用servlets 3.0和Ajax构建的简单Web应用程序,没有使用任何繁琐的工作。

The application renders GUI components from DB. 该应用程序从数据库呈现GUI组件。 In order to avoid cases where DB failure causes the application to not work I'm considering using kind of caching that will serve the requests instead of accessing DB for every HTTP GET request and if the property requested is not available in that cache then fetch from DB, add to cache and serve request. 为了避免数据库故障导致应用程序无法正常工作的情况,我正在考虑使用一种将为请求提供服务的缓存,而不是针对每个HTTP GET请求访问数据库,并且如果请求的属性在该缓存中不可用,则从DB,添加到缓存并服务请求。

I was reading about properties files and in-memory cache implementations then run into statless beans and session beans and got confused. 我在阅读有关属性文件和内存高速缓存实现的信息,然后遇到无状态Bean和会话Bean并感到困惑。 So what is the way to achieve this? 那么实现这一目标的方法是什么?

PS: Prefer not to use any freamwork. PS:最好不要使用任何框架。

I'm afraid you are mixing concepts here.... 恐怕您在这里混概念。

Properties files are plain text files containing key-value-pairs to store configuration and read them during runtime. 属性文件是纯文本文件,包含键-值对,用于存储配置并在运行时读取它们。 They have little in common with your problem. 它们与您的问题几乎没有共通之处。

Stateless and statefull beans are again something different. 无状态和有状态的bean再次有所不同。 A statefull bean may have an internal state. 有状态的Bean可能具有内部状态。 That is two calls to the same method may yield different results even though the context (db-contents etc) did not change. 即使上下文(db-contents等)未更改,对同一方法的两次调用也可能产生不同的结果。

So what is the way to achieve caching? 那么实现缓存的方法是什么? There is not built-in utility to cache DB-requests. 没有内置的实用程序来缓存数据库请求。 The most simple (but as well naive) approach would be something like 最简单(但也很幼稚)的方法类似于

private final Map<String, String> componentCache = new HashMap<>();

public String getComponentMarkup(String componentId, String... parameters){
    //Build a key for the parameters. Maybe simple stringconcatenation.
    String key=buildKey(componentId, parameters); 
    if (!componentCache.containsKey(key)){
        //Delegate the call to the "old" method which queries the database.
        String component = getComponentMarkupFromDb(componentId,parameters); 
        componentCache.put(key, component);
    }
    return componentCache.get(key);
}

But the bottom line is you should think about why you want to implement the cache by yourself. 但最重要的是,您应该考虑为什么要自己实现缓存。 There are tons of well tested solutions for you problem ( spring , hazelcast , etc ...). 有大量经过良好测试的解决方案可以解决您的问题( 春季榛子酒 )。 Your question indicates a not invented here syndrom ... 您的问题表明这里没有发明综合症 ...

There are a few ways to do it depending what you want to do: 有几种方法可以执行此操作,具体取决于您要执行的操作:

  • Cache web requests in the web tier using a servlet filter. 使用Servlet过滤器将Web请求缓存在Web层中。
  • Cache SQL requests in the database access tier 在数据库访问层中缓存SQL请求

A web cache will allow you cache requests based on URL pattern and requests parameters and trivial to do using Cacheonix web cache. Web缓存允许您基于URL模式和请求参数来缓存请求,而使用Cacheonix Web缓存则很简单。 The benefit of caching in the web tier is that the application doesn't have to spend time waiting for the database and rendering the response. 在Web层中进行缓存的好处是,应用程序不必花时间等待数据库并呈现响应。

Caching SQL requests is a bit more involved but the benefit is that you have a fine-grained control of what queries go do the database and what ResultSets are cached. 缓存SQL请求要花点时间,但是好处是您可以对数据库执行哪些查询以及缓存哪些ResultSet进行细粒度的控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM