在Play Framework中使用缓存 [英] Using Cache in Play Framework
问题描述
我正在尝试实施测验应用程序.该应用程序用ajax逐一加载问题.当用户单击到下一个问题"按钮时,他/她的答案将保存在缓存中.但是当我调试时,缓存列表始终为空...
I'm trying to implement a quiz application. The application loads the questions with ajax one by one. When a user clicks the 'to next question' button his/her answer is saved in cache. But when I debug, cache list is always null...
此代码创建第一个缓存数组:
This code creates the first cache array:
public static void viewQuiz(@Required String user, @Required String test) {
if(validation.hasErrors()) {
flash.error("Hoop kullanıcı lazım…");
index();
} else{
TestClass selectedTest = TestClass.find("title", test).first();
List<String> choiceList = new ArrayList<String>();
session.put("testID", selectedTest.id);
Cache.set("choices", choiceList, "30mn");
render();
}
}
这段代码试图一一保存答案:
And this code is trying to save the answers one by one:
public static void question(@Required Long id, String answer){
Long testId = Long.parseLong(session.get("testID"));
TestClass test = TestClass.findById(testId);
List<Question> questList = Question.find("test_id", test.id.intValue()).fetch();
Question quest = questList.get(id.intValue());
if(answer != null){
List<String> choiceList= Cache.get("choices",List.class);
choiceList.add(id.intValue(), answer);
Cache.set("choices", choiceList, "30mn");
}
int count = questList.size()-1;
render(quest, count, id);
}
此代码是第二个代码的html视图:
And this code is the html view of the second:
#{extends 'main.html' /}
#{set title:'question.html' /}
<script type="text/javascript">
var questionId = ${id};
$('#nextButton').click(function(){
$('#questionDiv').html('<p><img id = "loaderGif" src="public/images/loading.gif"/></p>');
$('#questionDiv').load("/test/" + ++questionId);
});
$('#endButton').click(function(){
$('#questionDiv').html('<p><img id = "loaderGif" src="public/images/loading.gif"/></p>');
$('#questionDiv').load("/result");
});
</script>
<legend>Soru ${id+1}</legend>
<p>&{quest.question}</p>
#{list items:quest.choices, as:'choice'}
<p><input type="radio" name = "answer" id = "answer" size="30" value="${choice}"/>&{choice}</p>
#{/list}
#{if id < count}
<input id = "nextButton" name="nextButton" type="button" value="İleri"/>
#{/if}
#{else}
<input id = "endButton" name="endButton" type="button" value="Bitti"/>
#{/else}
推荐答案
不要使用缓存来存储"对象.可以将其存储在会话中,也可以创建一个新模型来存储答案.通常,您不能指望缓存会保留您放入的对象.这是缓存,而不是商店.
Don't use the cache to 'store' objects. Either store it in the session or create a new model to store the answers. Generally, you cannot expect the cache to retain the objects you put into; it's a cache, not a store.
引用Play的话!网站: http://www.playframework.org/documentation/1.2.2/cache
To quote from the Play! website: http://www.playframework.org/documentation/1.2.2/cache
重要的是要了解缓存协定是明确的:何时 您将数据放在缓存中,就不能指望数据会保留在那里 永远.实际上,您不应该这样做.缓存速度很快,但值已过期, 并且缓存通常仅存在于内存中(没有持久性 备份).
It is important to understand that the cache contract is clear: when you put data in a cache, you can’t expect that data to remain there forever. In fact you shouldn’t. A cache is fast, but values expire, and the cache generally exists only in memory (without persistent backup).
这篇关于在Play Framework中使用缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!