2014-10-19 41 views
-1

我最親愛的社區,我剛剛接觸Play框架,並希望簡單地向緩存添加字符串。在下一步中,我想在網站上顯示這個字符串。使用play Framework緩存字符串2.3.5

我的模型類看起來是這樣的:

package models; 

public class Input { 

    public String text; 

    public void setText(String text){ 
    this.text = text; 
    } 

    public String getText(){ 
    return text; 
    } 

} 

緩存在設置中啓用。我使用默認的Cache(我認爲它是EHCache)。感謝您的答案!

回答

0

https://www.playframework.com/documentation/2.3.5/ScalaCache

有2個選項使用緩存播放

  1. 舉一個簡單的key-value存儲

    val maybeUser: Option[User] = Cache.getAs[User]("item.key") 
    
  2. 要緩存HTTP響應

    def userProfile = Authenticated { 
        user => 
         Cached(req => "profile." + user) { 
          Action { 
           Ok(views.html.profile(User.find(user))) 
          } 
         } 
    } 
    

因此,如果你希望緩存模型類價值,你有一些關鍵識別這種模式下,你可以使用它像這樣:

Cache.getOrElse[Input]("item.key") { 
     Input.findById(connectedUser) 
    } 
+0

的OP顯然是使用可以玩Java雖然。原則相同,但語法不同,特別是緩存HTTP響應。 https://www.playframework.com/documentation/2.3.5/JavaCache – 2014-10-19 14:17:31

+0

錯過了這一個。 – mavarazy 2014-10-19 15:19:50

+0

謝謝你的回答:) – alexfi 2014-10-19 17:43:30