如何维护 Java/J2EE Web 应用程序的缓存?
每次需要连接到该服务时,我都会为此开发一个应用程序。我想将每个搜索保存在缓存中以供进一步使用。有什么选择可以做到这一点吗?
我听说过 Memcached。但我没有找到任何正确的网站可供参考。我们可以像在 Hibernate 中那样使用 Ehcache 吗?
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. But I didn't find any correct site for reference. Can we use Ehcache as we use in Hibernate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一篇关于缓存的好文章。 http://www.javaworld.com /javaworld/jw-05-2004/jw-0531-cache.html?page=1
here is the good article about caching. http://www.javaworld.com/javaworld/jw-05-2004/jw-0531-cache.html?page=1
Java 中有多种缓存解决方案。其中包括 Infinispan、EhCache 和 OSCache。
它们都可以独立使用,例如,它们都不是专门为充当 Hibernate 缓存提供程序而构建的。
缓存之间的功能略有不同。例如,Infinispan 为事务提供一流的支持(这意味着如果插入项目的事务回滚,则该项目不会被插入到缓存中)。 EhCache 对非常大的进程内但堆外缓存存储提供了很好的支持。
OSCache 附带了非常方便的标签来缓存 JSP 页面上的内容(但这不适用于 JSF 等)。
它们中的大多数都能够执行典型的溢出到磁盘的操作,并且具有一些使过时条目无效的机制。例如,Infinispan 有驱逐策略,这些策略确实从缓存中删除了陈旧的条目(节省内存)。 OSCache 从来不会真正从缓存中删除条目,而是将其标记为过时。当访问该条目时,会提醒调用者刷新该条目(以前是例外,但现在可能有所不同)。
这些东西通常是将“缓存”与简单的并发哈希图区分开来的。如果您的要求不高,请不要忽视这个简单的解决方案。缓存可能有点难以配置,应用程序范围内的并发映射也可能足以满足您的需求。
There are various caching solutions in Java. Among them are Infinispan, EhCache and OSCache.
All of them can also be used standalone, e.g. none of them were exclusively build to function as a Hibernate caching provider.
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). EhCache has great support for really large in-process but off heap storage for cache.
OSCache comes with very handy tags to cache content on JSP pages (but that doesn't work with e.g. 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). OSCache on its turn never really removes an entry from the cache, but marks it as stale. 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. 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.
您可以使用 OSCache 的 jsp 标记非常轻松地缓存每个用户(即会话)的数据。例如,想象一个 Web 应用程序,其中特定用户的“工作列表”没有更改,然后始终提供缓存的(即已生成的)jsp,直到列表更改(通过应用程序中其他位置的刷新缓存调用)
。 jsp层,带有如下缓存标记:
表示java代码myBean.getData()每个会话只会调用一次(除非以其他方式刷新)
You can cache data on a per user basis (ie session) with OSCache's jsp tags very easily. 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)
Wrapping code on the jsp layer, with an cache tag as follows:
means the java code myBean.getData() will only be called once per session (unless otherwise flushed)