我不会做它在UI层,但使用的Ehcache而缓存你的查找方法。如果你使用hibernate,你可以使用它的二级缓存(也可以查询查询缓存)。如果你使用Spring,你可以很容易地标注您的服务方法是这样的:
@Cacheable("countries")
public List<Country> findCountries(){
// ... your lookup code here....
}
您也可以缓存失效当一个国家被更新:
@CacheEvict(value = "countries", allEntries=true)
public void saveCountry(Country country){
// your code for saving a country
}
在ehcache.xml中,你可以
最后定义缓存的实时时间(timeToLiveSeconds)。你甚至可以定义它永远不应该失效(永恒=真),当你确定只能通过你的服务门面应用更改。
<cache name="countries" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120"/>
更新1:
所以,你的支持bean本身甚至可以要求或视图作用域:
@ManagedBean
@ViewScoped
public class Bean{
@Inject
private CountryService countryService;
public List<Country> getCountries(){
return countryService.findCountries();
}
}