Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用play Framework 2.3.5缓存字符串_Java_Caching_Playframework_Ehcache_Playframework 2.3 - Fatal编程技术网

Java 使用play Framework 2.3.5缓存字符串

Java 使用play Framework 2.3.5缓存字符串,java,caching,playframework,ehcache,playframework-2.3,Java,Caching,Playframework,Ehcache,Playframework 2.3,我最亲爱的社区,我是play框架的新手,只想在缓存中添加一个字符串。在下一步中,我想在网站上显示这个字符串 我的模型类如下所示: package models; public class Input { public String text; public void setText(String text){ this.text = text; } public String getText(){ return text; } } Cache

我最亲爱的社区,我是play框架的新手,只想在缓存中添加一个字符串。在下一步中,我想在网站上显示这个字符串

我的模型类如下所示:

package models;

public class Input {

  public String text;

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

  public String getText(){
    return text;
  }

}
   Cache.getOrElse[Input]("item.key") {
       Input.findById(connectedUser)
   }
缓存已在设置中启用。我使用默认缓存(我认为它是EHCache)。谢谢你的回答

有两个选项可在游戏中使用缓存

  • 作为一个简单的键值存储

    val maybeUser: Option[User] = Cache.getAs[User]("item.key")
    
  • 缓存HTTP响应的步骤

    def userProfile = Authenticated {
         user =>
            Cached(req => "profile." + user) {
                Action {
                    Ok(views.html.profile(User.find(user)))
                }
            }
    }
    
  • 因此,如果您希望缓存模型类值,并且您有一些键来标识此模型,则可以如下所示使用它:

    package models;
    
    public class Input {
    
      public String text;
    
      public void setText(String text){
        this.text = text;
      }
    
      public String getText(){
        return text;
      }
    
    }
    
       Cache.getOrElse[Input]("item.key") {
           Input.findById(connectedUser)
       }
    

    不过OP显然是在使用PlayJava。原理相同,但语法不同,尤其是缓存HTTP响应时。