简体   繁体   English

Java Web应用程序的二级缓存及其替代品

[英]Second level cache for java web app and its alternatives

Between the transitions of the web app I use a Session object to save my objects in. I've heard there's a program called memcached but there's no compiled version of it on the site, besides some people think there are real disadvantages of it. 在Web应用程序的转换之间,我使用Session对象保存对象。我听说有一个名为memcached的程序,但网站上没有该程序的编译版本,此外,有人认为它有真正的缺点。 Now I wanna ask you. 现在我想问你。 What are alternatives, pros and cons of different approaches? 不同方法的替代方案,利弊是什么? Is memcached painpul for sysadmins to install? 是否为系统管理员安装了memcached painpul? Is it difficult to embed it to the existing infrastructure from the perspective of a sysadmin? 从系统管理员的角度来看,将其嵌入到现有的基础架构中很难吗?

What about using a database to hold temporary data between web app transitions? 使用数据库保存Web应用转换之间的临时数据该怎么办? Is it a normal practice? 这是正常的做法吗?

What about using a database to hold temporary data between web app transitions? 使用数据库保存Web应用转换之间的临时数据该怎么办? Is it a normal practice? 这是正常的做法吗?

Database have indeed a cache already. 数据库确实已经有一个缓存。 A well design application should try to leverage it to reduce the disk IO . 设计良好的应用程序应尝试利用它来减少磁盘IO

The database cache works at the data level. 数据库缓存在数据级别工作。 That's why other caching mechanism can be used to address different levels. 这就是为什么其他缓存机制可用于解决不同级别的原因。 At the java level, you can use the 2nd level cache of hibernate, which can cache entities and query result. 在Java级别,您可以使用hibernate的第二级缓存 ,它可以缓存实体和查询结果。 This can notably reduce the network IO between the app. 这可以显着减少应用之间的网络IO server and the database. 服务器和数据库。

Then you may want to address horizontal scalability , that is, to add servers to manage the load. 然后,您可能需要解决水平可伸缩性 ,即添加服务器来管理负载。 In this case, the 2nd level cache need to be distributed across the nodes. 在这种情况下,需要在节点之间分配 2级缓存。 This exists (see JBoss cache), but can get slightly complicated to manage. 它存在(请参阅JBoss缓存),但是管理起来可能会有些复杂。

Distributed cache tend to worker better if they have simpler scheme based on key/value . 如果分布式缓存具有基于键/值的更简单方案,则它们倾向于更好地工作。 That's what memcached is, but there are also other similar solutions. 这就是memcached ,但还有其他类似的解决方案。 The biggest problem with distributed caches is invalidation of outdated entries -- which can itself turn into a performance bottleneck. 分布式缓存的最大问题是过时条目的失效 -这本身可能会成为性能瓶颈。

Don't think that you can use a distributed cache as-is to make your performance problems vanish. 不要认为您可以按原样使用分布式缓存来消除性能问题。 Designing a scalable distributed architecture requires experience and is always a matter of trade-off between what to optimize and not. 设计可扩展的分布式体系结构需要经验,并且总是要在优化与不优化之间进行权衡。

To come back to your question: for regular application , there is IMHO no need of a distributed cache. 回到您的问题:对于常规应用程序 ,恕我直言,不需要分布式缓存。 Decent disk IO and network IO lead usually to decent performance. 体面的磁盘IO和网络IO通常会导致体面的性能。

EDIT 编辑

For non-persistent objects, you have several options: 对于非持久性对象,您有几种选择:

  • The HttpSession . HttpSession Objects need to implement Serializable . 对象需要实现Serializable The exact way the session is managed depends on the container. 会话的确切管理方式取决于容器。 In a cluster, the session is usually replicated twice, so that if one node crashes you still have one copy. 在群集中,会话通常被复制两次,因此,如果一个节点崩溃,您仍然拥有一个副本。 There is then session affinity to route the request to the server that has the session in memory. 然后存在会话亲缘关系,以将请求路由到内存中具有该会话的服务器。
  • Distributed cache. 分布式缓存。 A system like memcached may indeed make sense, but I don't know the details. memcached这样的系统可能确实有意义,但是我不知道细节。
  • Database. 数据库。 You could of course dump any Serializable object in the database in a BLOB . 您当然可以在BLOB转储数据库中的任何Serializable对象。 Can be an option if the web servers are not as reliable as the database server. 如果Web服务器不如数据库服务器可靠,则可以选择。

Again, for regular application , I would try to go as far as possible with the HttpSession . 再次,对于常规应用程序 ,我将尝试使用HttpSession

How about Ehcache ? Ehcache怎么样? It's an easy to use pure Java solution ready to plug in to Hibernate. 这是一种易于使用的纯Java解决方案,可以立即插入Hibernate。 As far as I remember it's supported by containers. 据我记得,它受容器支持。

It's quite painless in my experience. 根据我的经验,这是很轻松的。

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

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