java - Is this good practice for caching database data on my HTTP servlet? -


i'm writing http servlet (hosted on amazon elastic beanstalk) serves application server android app. application request data servlet, in turn pull database (simpledb).

since client requests may frequent, wanted implement cache on servlet cache requested data cut down on database reads. currently, initialize "servercache" object member variable of servlet. servercache contains lists cached data, , populate these go.

looks this.

public class servercache { /**  * servercache responsible caching data on server.  * create several data structures on server cache buy/sell listings listing objects.   * now, able cache entirety of database contents.   * -es  *   * 1 servercache should ever made, @ init() of server  */  private list<buylisting> listbl; //what danger of having public vs. private private list<selllisting> listsl;  public string bllasterror; public string sllasterror; public servercache() {     this.listbl = new arraylist<buylisting>();     this.listsl = new arraylist<selllisting>();      this.bllasterror = "initialized";     this.sllasterror = "initialized"; } //setters , getters ommitted 

and initialized here

public class helloworld extends httpservlet {  private simpledbconnect sdb; // contains functions accessing database private servercache cache;  //various constants private static final string buy_listings_domain = "buylistings"; private static final string sell_listings_domain = "selllistings"; private static final string user_id_domain = "userids"; private static final string messages_domain = "messages";    public helloworld() {     super();     sdb = new simpledbconnect();     sdb.createconnection();     cache = new servercache();      cache.setlistbl(sdb.getallbl());     cache.setlistsl(sdb.getallsl());     updatesdbstatus();     updateservletstatus("initialized");     updatecachestatus();  }  protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception  {     response.getoutputstream().println("servlet works \n simpledb status:" + sdb.dbconnectstatus + "\n \n sdb log:  " + this.sdbstatus + "\n \n servlet log:    " + this.servletstatus             + "\n \n buy cache status:  " + this.blcachestatus + "\n \n sell cache status:  " + this.slcachestatus); }  protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // ... code ommitted, following relevant part of how handle request data           if (packet.getheader() == constants.bl_request || packet.getheader() == constants.sl_request)             {                                msgstruct temp = new msgstruct();                    if (packet.getheader() == constants.bl_request){                     temp.setheader(constants.bl_request);                      type listoftestobject = new typetoken<list<buylisting>>(){}.gettype();                     string s = gson.tojson(cache.getlistbl(), listoftestobject);                      temp.setpayload(s);                     receivedstring = gson.tojson(temp);                 } 

is there reason why caching version of data on servlet bad idea? stab in dark, nobody has ever taught me in case. i'm aware concurrency issue, dont think should matter in case?

thanks

adding cache program complicate little bit, when want modify data. in case, database , cache should have same data, no matter get or post requests received may or may not write database.

a problem may run how keep cache in sync database. crucial update cache when writing database, , make sure when fulfill request cache, return same data contained in database. did you're caching data requested, if data ever written database, have check cached data updated.

you mentioned you'll instantiate 1 instance of class. might make more sense follow singleton pattern cache, see wikipedia page http://en.wikipedia.org/wiki/singleton_pattern more information. basically, causes cache initialize , make sure no other code can make instance. constructor private, , write getinstance() method have cache return instantiated servercache object.

the reason i'm going route because there's possibility write servlet needs access database, , difficult give cache access without making cache singleton (not mention multithreading issues associated multiple reads).

another thing remember, cache take ram on server. caching trades of ram usage access time, , depending on plan, may or may not have lot of spare. it's real possibility you'd replicate entire database in ram, defeating purpose. spend bunch of processing time making sure update database never use it. again, if see improvement in responsiveness, see no problem it, long manage well.

to use cache, you'd have keep strict limit on number of cached objects, getting rid of objects aren't used much, keeping ones accessed frequently. final point want make noticed cache using arraylists. aren't efficient data structures searching unsorted data. database made data storage, database has more efficient storage system anyway, in case make cache useful small amounts of accessed seldom modified data.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -