Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

is session best option for caching?  RSS feed

 
Zeina Afif
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two questions, but both related to caching and using sessions.
Question 1, what are the limitations of using sessions for caching objects?
What I'm caching is db queried data that needs to be either displayed for printing or downloaded to excel. Would using session to store it be the better option?
Question 2: I have written two classes that dynamically builds search pages.
One class queries the db and returns 3 vectors:
Lookup data, Table information (column lookups)etcc.
The second class takes the Vectors and returns strings of the different display options.
When the jsp calls the methods in the second class, it take a while to load. What do I need to do to cache them? Data in the vectors are not frequently updated.
Thank you!
 
chanoch wiggers
Author
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could cache them in session but ths could quickly get out of hand. Depending on the type of searches you are making it might be better to actually use a cache - or implement one if you have to. Design a singleton that returns a cache to which you save both the data and a representation of the query that resulted in the result set. YOu can then query the cache first before trying the query against the databse.
btw, watch out for result sets taht are live connections to the database - you dont want to be caching connection to the database in session scope, unload the result set then let it go. If the data is read often write seldom then you dont need to guarantee correctness - when and if the user has to act on the data, you can always refresh the data to make sure it has not changed.
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add ... one of the other problems that can occur when storing many objects in the session is that with some application servers, session state can be replicated around a cluster for reasons around resilience and failover. Ideally, try to keep the session as small as possible.
Cheers
Simon
 
Zeina Afif
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon and Chanoch,
Thank you for replying, that's what I thought too (about sessions). I made a search on Singleton obj and found a lot of info in this forum.
But I still need advice on what to cache... I have two classes.
First one, DatabaseFields(), has two methods:
getLookupData() which returns a vector of vectors of the lookups, and getTableFields(String strTableName) which gets passed the tablename and returns a vector of vectors (column names, type and display names etc..).
The second class BuildFormElements(), has different methods that render the vectors returned in the DatabaseFields in different format (for searching, for editing etc...).
Then different jsp pages call different methods, either for lookup, or editing... etc...
It all works well now, but I know that performance will be an issue as soon as this app goes fully to production.
Which class should be cached? Hope this makes sense!
Thanks again!
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zeina Afif wrote: What do I need to do to cache them? Data in the vectors are not frequently updated.
This sounds like a classic use for the Application context to me.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!