play framework2開發(八)

來源:互聯網
上載者:User
The Play cache API

快取資料在應用開發中是可選擇的,Play提供一個全域。緩衝重要一點是你儲存的資料會消失。

對於任何儲存在緩衝中的資料,擷取時如果發現不存在則需要再次儲存進緩衝。Play會一直儲存資料直到生命週期結束。

Cache.set("data1", userForm);

/ Cache for 15 minutes

Cache.set("item.key", frontPageNews,
60 *
15);

News news =
Cache.get("data1");

Cache.remove("data1");

緩衝 HTTP responses
@Cached("homePage")public static Result index() {  return ok("Hello world");}

緩衝 in templates

You may also access the cache from a view template.

@cache.Cache.getOrElse("cached-content", 3600) {     <div>I’m cached for an hour</div>}

會話緩衝

Play提供一個全域緩衝,資料對於任何人都是可見的。怎樣才能限制只給特定的使用者可見呢?下邊這個例子:

// Generate a unique IDString uuid=session("uuid");if(uuid==null) {uuid=java.util.UUID.randomUUID().toString();session("uuid", uuid);}// Access the cacheNews userNews = Cache.get(uuid+"item.key");if(userNews==null) {userNews = generateNews(uuid);Cache.set(uuid+"item.key", userNews );}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.