java - Guice: How can I cache data -


i new guice , have question regarding caching.

i have webapp uses provider provide user configurations.

bind(userconfiguration.class).toprovider(userconfigurationprovider.class).in(servletscopes.request); 

the provider declared as:

public class userconfigurationprovider implements provider<userconfiguration>{     public userconfiguration get() {         userconfigurationdatabase.getuserconfiguration(<some param here>);     } } 

in method, calls db , make expensive db call.

i have number of classes userconfiguration injected like:

public class priceview {     private userconfiguration userconfiguration;     @inject     public myclass(userconguration userconfiguration){         this.userconfiguration = userconfiguration;     }      ..... }  public class orderview {     private userconfiguration userconfiguration;     @inject     public myclass(userconguration userconfiguration){         this.userconfiguration = userconfiguration;     }    ........ } 

the problem every time switch view, new xxxxview object created , hence new userconfiguration injected , hence expensive db call called.

i want make calling db limited once per user login. idea cache userconfiguration somewhere when user login , clear after user logout or expires.

but want know whehter guice offers more clever ways of caching use case?

many thanks.

if you're using standard servlet session, should enough use servletscopes.session scope instead of servletscopes.request. in case injected object should created once per session.

however, if not using standard servlet session custom, need create custom scope.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -