天天看點

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.

你不應依靠緩存資料,進而需要處理緩存的失誤。

繼續閱讀