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 );}