java GWT 中的客户端缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2362626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:46:58  来源:igfitidea点击:

Client side caching in GWT

javagwtcaching

提问by Silfverstrom

We have a gwt-client, which recieves quite a lot of data from our servers. Logically, i want to cache the data on the client side, sparing the server from unnecessary requests.

我们有一个 gwt-client,它从我们的服务器接收大量数据。从逻辑上讲,我想在客户端缓存数据,使服务器免受不必要的请求。

As of today i have let it up to my models to handle the caching of data, which doesn't scale very well. It's also become a problem since different developers in our team develop their own "caching" functionality, which floods the project with duplications.

截至今天,我已经让我的模型来处理数据的缓存,这不能很好地扩展。这也成为一个问题,因为我们团队中的不同开发人员开发了他们自己的“缓存”功能,这使项目充满了重复。

I'm thinking about how one could implement a "single point of entry", that handles all the caching, leaving the models clueless about how the caching is handled.

我在考虑如何实现一个“单一入口点”,处理所有缓存,让模型对如何处理缓存一无所知。

Does anyone have any experience with client side caching in GWT? Is there a standard approach that can be implemented?

有没有人对 GWT 中的客户端缓存有任何经验?是否有可以实施的标准方法?

采纳答案by Robert Munteanu

I suggest you look into gwt-presenterand the CachingDispatchAsync. It provides a single point of entry for executing remote commands and therefore a perfect opportunity for caching.

我建议您查看gwt-presenterCachingDispatchAsync. 它提供了执行远程命令的单一入口点,因此是缓存的绝佳机会。

A recent blog postoutlines a possible approach.

一个最近的博客文章概述了一种可能的方法。

回答by Jeroen

You might want to take a look at the Command Pattern; Ray Ryan held a talk at Google IO about best practices in GWT, here is a transcript: http://extgwt-mvp4g-gae.blogspot.com/2009/10/gwt-app-architecture-best-practices.html

你可能想看看命令模式;Ray Ryan 在 Google IO 上发表了关于 GWT 最佳实践的演讲,这里是一个成绩单:http: //extgwt-mvp4g-gae.blogspot.com/2009/10/gwt-app-architecture-best-practices.html

He proposes the use of the Command Pattern using Action and Response/Result objects which are thrown in and out the service proxy. These are excellent objects to encapsulate any caching that you want to perform on the client.

他建议使用命令模式使用 Action 和 Response/Result 对象,这些对象被抛出和抛出服务代理。这些是封装您要在客户端执行的任何缓存的极好对象。

Here's an excerpt: "I've got a nice unit of currency for implementing caching policies. May be whenever I see the same GET request twice, I'll cache away the response I got last time and just return that to myself immediately. Not bother with a server-side trip."

这是一个摘录:“我有一个很好的货币单位来实施缓存策略。可能每当我看到相同的 GET 请求两次时,我就会缓存上次得到的响应并立即将其返回给我自己。不是打扰服务器端旅行。”

In a fairly large project, I took another direction. I developed a DtoCache object which essentially held a reference to each AsyncCallback that was expecting a response from a service call in a waiting queue. Once the DtoCache received the objects from the server, they were cached inside the DtoCache. The cached result was henceforth returned to all queued and newly created AsyncCallbacks for the same service call.

在一个相当大的项目中,我采取了另一个方向。我开发了一个 DtoCache 对象,它本质上持有对每个 AsyncCallback 的引用,这些 AsyncCallback 期待来自等待队列中的服务调用的响应。一旦 DtoCache 从服务器接收到对象,它们就会被缓存在 DtoCache 中。此后,缓存的结果将返回给所有排队的和新创建的同一服务调用的 AsyncCallback。

回答by Charles Kendrick

For an already-fully-built, very sophisticated caching engine for CRUD operations, consider Smart GWT. This example demonstrates the ability to do client-side operations adaptively (when the cache allows it) while still supporting paging for large datasets:

对于用于 CRUD 操作的已经完全构建的、非常复杂的缓存引擎,请考虑使用 Smart GWT。此示例演示了自适应地执行客户端操作的能力(当缓存允许时),同时仍支持大型数据集的分页:

http://www.smartclient.com/smartgwt/showcase/#grid_adaptive_filter_featured_category

http://www.smartclient.com/smartgwt/showcase/#grid_adaptive_filter_featured_category

This behavior is exposed via the ResultSet class if you need to put your own widgets on top of it:

如果您需要将自己的小部件放在它上面,则此行为通过 ResultSet 类公开:

http://www.smartclient.com/smartgwtee/javadoc/com/smartgwt/client/data/ResultSet.html

http://www.smartclient.com/smartgwtee/javadoc/com/smartgwt/client/data/ResultSet.html

回答by Trung

There are two levels of caching:

有两个级别的缓存:

  • Caching during one browser session.
  • Caching cross browser sessions, e.g the cached data should be available after browser restarted.
  • 在一个浏览器会话期间缓存。
  • 缓存跨浏览器会话,例如,在浏览器重新启动后缓存数据应该可用。

What to cache: depend on your application, you may want to cache

缓存什么:取决于您的应用程序,您可能想要缓存

  • Protected data for particular user
  • Public static (or semi-static, e.g rarely to change) data
  • 特定用户的受保护数据
  • 公共静态(或半静态,例如很少更改)数据

How to cache:

如何缓存:

  • For the first caching level, we can use GWT code as suggested in the answers or write your own one.
  • For the second one, we must use Browser caching features. The standard approach is put your data inside html (whether static html files or dynamic generated by jsp/servlet for example). Your application then use http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsOverlay.htmltechniques to get the data.
  • 对于第一个缓存级别,我们可以按照答案中的建议使用 GWT 代码,也可以编写自己的代码。
  • 对于第二个,我们必须使用浏览器缓存功能。标准方法是将您的数据放在 html 中(例如,静态 html 文件或由 jsp/servlet 生成的动态文件)。然后,您的应用程序使用http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsOverlay.html技术来获取数据。

回答by JP Richardson

I thought Itemscriptwas kind of neat. It's a RESTful JSON database that works on both the client (GWT) and server.

我认为Itemscript有点整洁。它是一个 RESTful JSON 数据库,适用于客户端 (GWT) 和服务器。

Check it out!

看看这个!

-JP

-J.P