简体   繁体   English

如何维护Java / J2EE Web应用程序的缓存?

[英]How to maintain cache for Java/J2EE web application?

I am developing an application for that every time I need to connect to the service. 每次我需要连接到服务时,我正在开发一个应用程序。 I want to save each search in my cache for further use. 我想将每个搜索保存在缓存中以供进一步使用。 Is there any option to do that? 有没有选择呢?

I heard about Memcached. 我听说过Memcached。 But I didn't find any correct site for reference. 但我没有找到任何正确的网站供参考。 Can we use Ehcache as we use in Hibernate? 我们可以在Hibernate中使用Ehcache吗?

There are various caching solutions in Java. Java中有各种缓存解决方案。 Among them are Infinispan, EhCache and OSCache. 其中包括Infinispan,EhCache和OSCache。

All of them can also be used standalone, eg none of them were exclusively build to function as a Hibernate caching provider. 所有这些都可以单独使用,例如,它们都不是专门用作Hibernate缓存提供程序。

Functionalities between caches differ a little. 缓存之间的功能略有不同。 Infinispan for instance provides first class support for transactions (meaning an item won't be inserted into the cache if the transaction in which the item was inserted rollbacks). 例如,Infinispan为事务提供了一流的支持(意味着如果插入项目的事务是回滚,则不会将项目插入到缓存中)。 EhCache has great support for really large in-process but off heap storage for cache. EhCache非常支持真正的大型进程内但是用于缓存的堆外存储。

OSCache comes with very handy tags to cache content on JSP pages (but that doesn't work with eg JSF). OSCache带有非常方便的标签来缓存JSP页面上的内容(但这不适用于例如JSF)。

Most of them are capable of doing the typical spill over to disk thing, and have some mechanisms to invalidate stale entries. 他们中的大多数能够执行典型的溢出到磁盘的事情,并有一些机制来使陈旧的条目无效。 Infinispan for instance has eviction policies, and those really remove stale entries from the cache (saving memory). 例如,Infinispan具有逐出策略,并且那些真正从缓存中删除陈旧条目(节省内存)。 OSCache on its turn never really removes an entry from the cache, but marks it as stale. OSCache从来没有真正从缓存中删除条目,但将其标记为陈旧。 When the entry is accessed, the caller is alerted to refresh the entry (used to be an exception, but might be different now). 访问该条目时,将提醒调用者刷新该条目(以前是一个例外,但现在可能不同)。

Those things are typically what sets a "cache" apart from a simple concurrent hashmap. 这些东西通常是将“缓存”与简单的并发hashmap区分开来的。 If your requirements are modest, don't overlook this simple solution though. 如果您的要求适中,请不要忽视这个简单的解决方案。 A cache can be somewhat hard to configure and a concurrent map in application scope may also suffice for you. 缓存可能有点难以配置,应用程序范围内的并发映射也可能就足够了。

You can cache data on a per user basis (ie session) with OSCache's jsp tags very easily. 您可以非常轻松地使用OSCache的jsp标记基于每个用户(即会话)缓存数据。 For example, imagine a web application, where a particular users "worklist" hasn't changed, then always serve the cached (ie already generated) jsp until the list has changed ( via a flush cache call somewhere else in application) 例如,想象一个Web应用程序,其中特定用户“工作列表”没有更改,然后始终提供缓存(即已生成的)jsp,直到列表发生更改(通过应用程序中的其他位置的刷新缓存调用)

Wrapping code on the jsp layer, with an cache tag as follows: 在jsp层上包装代码,缓存标记如下:

<cache:cache key="foobar" scope="session">
  <%= myBean.getData() %>
</cache:cache>

means the java code myBean.getData() will only be called once per session (unless otherwise flushed) 表示每个会话只调用一次java代码myBean.getData()(除非另外刷新)

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

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