天天看点

Play Framework Cookbook (play框架食谱...)3

Play Framework Cookbook (play框架食谱...)2续

There's more...

更多...

The router module is a small, but handy module, which is perfectly suited to take a first look 

at modules and to understand how the routing mechanism of the Play framework works at its 

core. 

路由器模块是一个小的,但是唾手可得的模块,非常适合从模块入手,以便了解play框架的“路由选择“在其核心工作原理是怎样的。 

You should take a look at the source if you need to implement custom mechanisms of URL routing.

你应该看一看源码,如果你需要实现URL路由定制的机制。

Mixing the configuration file and annotations is possible

混合配置文件和注释是可能的

You can use the router module and the routes file—this is needed when using modules as they cannot be specified in annotations.

你可以同时使用路由器模块和路由文件-当使用模块时不能被注释时,就很需要了。

However, keep in mind that this is pretty confusing. 

无论如何,要牢记在心,因为这是相当令人困惑的

You can check out more info about the router module at  http://www.playframework.org/modules/router.

更多关于路由器模块的信息可以查看http://www.playframework.org/modules/router

Basics of caching

缓存基础知识

Caching is quite a complex and multi-faceted technique, when implemented correctly. 

缓存正确被实现,是一个相当复杂、多方面的技术。

However, implementing caching in your application should not be complex, but rather the 

mindwork before, where you think about what and when to cache, should be. 

然而,在你的应用中实现缓存时不应复杂,但是动手前,应该思考下是否要缓存,何时去缓存。

There are many different aspects, layers, and types (and their combinations) of caching in any web application.

在任何一个web应用中,有许多不同的方面,层次,类型(和他们的组合)的缓存。

This recipe will give a short overview about the different types of caching and  how to use them.

本章将给一个简短的大概了解不同类型的缓存,以及如何使用它们。

You can find the source code of this example in the  chapter2/caching-general directory.

你能找到这个例子的源代码在chapter2/caching-general目录。

Getting ready First, it is important that you understand where caching can happen—inside and outside 

of your Play application. 

首先准备好,这是很重要的去理解缓存可以发生在play应用的内部和外部。

So let's start by looking at the caching possibilities of the HTTP protocol.

让我们先从了解HTTP协议的缓存发展开始。

HTTP sometimes looks like a simple protocol, but is tricky in the details. However,  

it is one of the most proven protocols in the Internet, and thus it is always useful to rely on  

its functionalities.

HTTP有时候看上去像一个简单的协议,但是细节是一件很复杂的事情。然而,

这是在互联网 上最行之有效的协议,因此信赖它的功能总是有用的

HTTP allows the caching of contents by setting specific headers in the response. There are 

several headers which can be set:

HTTP允许通过在响应中设置专门的头部信息缓存内容。有几中头部信息可设置:

   Cache-Control: This is a header which must be parsed and used by the client and 

also all the proxies in between.

缓存控制:这是一个头部信息必须通过客户还有所有代理服务器之间解析和使用的。

   Last-Modified : This adds a timestamp, explaining when the requested resource had 

been changed the last time. On the next request the client may send an If-Modified-Since header with this date. Now the server may just return a HTTP 304 code without 

sending any data back.

最近修改:这个增加了一个时间戳,解释当请求的资源最后一次被改变的时间。客户可能发送一个“从何时更改”的日期的头部信息在下一个请求中。此时服务器可能返回HTTP响应代码304,不发送任何返回信息

   ETag: An ETag is basically the same as a Last-Modified header, except it has a semantic meaning. 

ETAG基本上与Last-Modified相同,除了语义上的一点区别。

It is actually a calculated hash value resembling the resource behind the requested URL instead of a timestamp. 

实际上这是一种类似于哈希值计算资源您访问的地址或文件代替时间戳

This means the server can decide when a resource has changed and when it has not. This could also be used for some 

type of optimistic locking.

这意味着服务器可以决定当一个资源被更改与否。这可能也被用于一些类型的乐观锁

So, this is a type of caching on which the requesting client has some influence on. 

所以,这是一个不同的缓存,请求的客户也有一定影响。

There are also other forms of caching which are purely on the server side.

仅在服务器端也有其他形式的缓存。

In most other Java web frameworks, the HttpSession object is a classic example, which belongs to this case.

在大多数其它Java web框架中.HttpSession的对象是一个典型的例子,它属于这种情况。

Play has a cache mechanism on the server side. It should be used to store big session data, in this case any data exceeding the 4KB maximum cookie size. 

有缓存机制发挥在服务器端。它应被用于大会话数据的存储,比如超过了cookie最大值4 KB的数据。

Be aware that there is a semantic difference between a cache and a session. 

注意,在缓存和会话之间有语义上的区别。

You should not rely on the data being  in the cache and thus need to handle cache misses.

你不应依靠缓存数据,从而需要处理缓存的失误。

继续阅读