干净会话和最小数据库访问之间的优选方式

时间:2016-02-17 06:31:05

标签: performance session jsf caching cookies

考虑以下场景:我有一个bean处理用户搜索,其中许多参数在不同网址的许多页面上使用。许多用户可能会花更多的时间进行自定义搜索,而且目前我每次都要点击数据库来加载这些静态列表。

@ManagedBean
@ViewScoped
public class SearchBean extends DefaultBean {

private String searchPath; //seo: build a url-friendly path depending on search-parameters

private List<Currency>currencies;
private List<Country>countries;
private List<Market>markets;
private List<DrugTypes>drugTypes;
private List<Products>products;

/**
 *  ...15 other lists
 */

private List<ResultData>results;

@PostConstruct
public void init(){
    this.currencies = Currency.getAll(); //jpa-entities
    this.countries = Country.getAll();
    this.markets = Markets.getAll();
    this.drugTypes = DrugTypes.getAll();
    this.products = Products.getAll();
}

public String search(){
    this.results = ResultData.getByParameters(getSearchParams());
    //
     //e.g. localhost:8080/myApp/search/markets/germany/class-alpha-products/rhesus?faces-redirect=true
    return searchPath; 
}

public List<Currency> getCurrencies() { return currencies; }
public void setCurrencies(List<Currency> currencies) { this.currencies = currencies; }

public List<Country> getCountries() { return countries; }
public void setCountries(List<Country> countries) { this.countries = countries; }

public void setMarkets(List<Market> markets) { this.markets = markets; }
public List<Market> getMarkets() { return markets; }

public void setDrugTypes(List<DrugTypes> drugTypes) { this.drugTypes = drugTypes; }
public List<DrugTypes> getDrugTypes() { return drugTypes; }

public List<Products> getProducts() { return products; }
public void setProducts(List<Products> products) { this.products = products; }  
}


什么是关于标题的推荐方式?我的小抱怨是,我在控制台上看到了20个jpa查询,尽管在客户端使用<h:selectOneMenu>构建的列表数据在新页面上没有变化但必须包含在每个子页面上。

  1. 保持原样

  2. 将所有这些列表作为会话属性放入,并在用户离开时将其删除。@ / p>

  3. 把整个bean作为sessionbean(我已经有2个会话bean(“用户”和“语言”,我读到有更多不是一个好的设计)

  4. 将list-data作为json-string存储在cookie中,如果cookie存在则重新创建列表?

  5. 其他建议?

  6. 感谢收看!

1 个答案:

答案 0 :(得分:2)

一切都没有。缓存数据库实体不是前端(UI)框架的责任。这是持久性(DB)框架的责任,因此在您的情况下就是JPA。

JPA提供二级缓存功能。与您的所有提案相比,它的主要优势在于它知道精确哪些实体被更多查询,因此需要进行缓存,以及何时由于实体更改而使高速缓存的实体无效。 JSF是一个基于HTML形式的基于MVC的框架,它只将用户界面事件/数据委托给业务服务,并不知道这一切。

另见: