0

consider the following scenario: i have a bean that handles user-searches with a lot of parameters used on many pages with different urls. many users may spent a larger time with custom-searches and currently i am hitting the database to load those static lists everytime.

@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; }  
}



what is the recommend way regarding to the headline? my small gripe is, that i see 20 jpa-queries on the console although the list-data which is build with <h:selectOneMenu> on client-side does not change on new pages but must be included on every subpage.

  1. leave it as it is

  2. put all those lists as session-attributes and remove them in @predestroy when user leaves.

  3. put the whole bean as sessionbean (i already have 2 session-beans ("user" and "language" and i read that having more is not a good design)

  4. store the list-data as a json-string in a cookie and recreate the list if the cookie exists?

  5. other suggestions?

thanks for watching!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Steve
  • 384
  • 1
  • 7
  • 17

1 Answers1

2

None of all. Caching DB entities isn't the responsibility of a front end (UI) framework. That's the responsibility of the persistence (DB) framework, which is thus JPA in your case.

JPA offers 2nd level caching possibilities. Main advantage as compared to all your proposals is that it knows precisely which entities are more queried and thus need to be cached, and when exactly to invalidate a cached entity because of an entity change. JSF as being a dumb HTML form based MVC framework which only delegates user interface events/data to business services knows nothing of this all.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555