Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cached Collection?

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet that uses a java.util.Set with a couple hundred items. I expect the set to get larger over time.
I was wondering if it's possible to keep a cached copy of this Set on the server (in RAM) instead of using a database or flat file to store/update the set?
I'm thiniking this would be better for performance that creating a database connection or reading from the file system each time the servlet is called.
Thanks,
Drew
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing wrong with keeping this Set as an instance variable of the servlet, providing that it doesn't hold data specific to a particular request. That would be the simplest "cache" - you would want to provide some mechanism to avoid changing it while a request is actually using it.
Bill
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:
There is nothing wrong with keeping this Set as an instance variable of the servlet, providing that it doesn't hold data specific to a particular request. That would be the simplest "cache" - you would want to provide some mechanism to avoid changing it while a request is actually using it.
Bill

Well, that's what I'm doing right now but I have to manually change the set and this is a pain.
Is it possible to have a small, in memory, centralized database type object that persists that all instances of this servlet could use?
Say, it loads up from a file whenever the server loads and populates the java.util.set and just hangs around until the server is shut down.
Drew
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say, it loads up from a file whenever the server loads and populates the java.util.set and just hangs around until the server is shut down.

Nothing prevents you from doing that using William's suggestion. Load up the set from the DB in the init() method of the servlet, and let it hang around until the servlet is taken out of service.
If you code in a means to refresh the cache from the DB while running, be sure to synchronize around that.
[ February 25, 2004: Message edited by: Bear Bibeault ]
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:

Nothing prevents you from doing that using William's suggestion. Load up the set from the DB in the init() method of the servlet, and let it hang around until the servlet is taken out of service.
If you code in a means to refresh the cache from the DB while running, be sure to synchronize around that.
[ February 25, 2004: Message edited by: Bear Bibeault ]

OK, that is what I needed to know. It's been a while since I've done this.
Could I actually put the data that I want to use for the java.util.Set directly in the web.xml file as an init-param?
Drew
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the data is fairly static and expressable as text, you could put it in the web.xml as init params. Be aware that you would need to restart the app whenever the data needed to be changed. The DB mechanism would be better suited to on-the-fly updating.
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
If the data is fairly static and expressable as text, you could put it in the web.xml as init params. Be aware that you would need to restart the app whenever the data needed to be changed. The DB mechanism would be better suited to on-the-fly updating.

If I use the DB mechanism, can I call the Servlet's init method again (after it's already been invoked once) to updated the Set from the database? What syntax would I use to do that?
Drew
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, but that's not the way I would code it to begin with. The DB update should be in a separate unit (perhaps a private method), that woul be able to be called from the init method as well as from a doPost or doGet that responds to a 'command' to update the cache.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic