?
Solved

Time based cache in Java programming

Posted on 2005-04-28
13
Medium Priority
?
280 Views
Last Modified: 2012-05-05
Hi Experts ,
   I have a sql Query which is cached based on a key .
I want this cache to clear after some hours.

It this possible?
0
Comment
Question by:skn73
  • 5
  • 3
  • 3
  • +2
13 Comments
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13885744
OSCache from OpenSymphony does this sort of thing but it's mostly aimed at JSPs rather than standalone apps, although you can do it. I just don't know how mature the API is for direct access. http://www.opensymphony.com/oscache/
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13886409
If you wanna do it simply yourself, you can write a simple Timer and keep its interval to whatever you want. Every time the timer ticks, you can clear the cache and reload it with whatever you want.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 15

Expert Comment

by:aozarov
ID: 13887090
You can store your query with an "executed" timestamp and then when you get it you can check if the delta to current time is > MAX_TIME_TO_LIVE.
If so then you can discard it (and probably generate a new query that will be saved in the cache with a new "executed" timestamp).
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13887117
BTW, if memory is a constraint for you then there are several strategies you can follow:
1. limit the size of your cache and remove old ones as you insert when full (LinkedHashMap can help here)
2. use WeakHashMap (too weak to my taste)
3. wrap your objects with SoftReference
4. use a Timer, as suggested above, to clean old ones based on the "executed" time
0
 

Author Comment

by:skn73
ID: 13894568
aozarov ,
  I like ur answer about storing it with the executed Timestamp.
can you list if there are any disadvantages to that?

Thanks,
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13894913
The only one that I can think of is if you are going to have many different sql queries cached such that will require periodic cleaning (in order not to run out of memory).
If you are going to have a constant set of queries to cache then I don't think that is going to be an issue for you otherwise you can consider one of the 4 options I suggested above.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13895975
So you want to cache more than one query? I thought your initial requirement was caching a single query:

>> I have a sql Query which is cached based on a key

If you want to cache multiple, I guess a HashMap (query, time-stamp) + Timer combination will be good.
0
 

Author Comment

by:skn73
ID: 13898249
Its a single Query .. but there could be  100000 different keys and each of this 100000 keys can have 100-1000 different values.

So I am worried about the Cache.

Can you eloborate on ur other options

Thanks,
0
 
LVL 15

Accepted Solution

by:
aozarov earned 2000 total points
ID: 13899419
>> 100000 different keys and each of this 100000 keys can have 100-1000 different values.
So, I guess the keys are based on the arguments for the query?
And I guess your unique key (to identify a specific instance) will be combined from those values...

As I said you can control your cache memory using different options:
** For all the options the value associated with the key should be an object that contains both the timetamp and the query result.
1. Controlling the size of your cache using a fixed Size LinkedHashMap when removing old ones (based on LRU) see: http://javaalmanac.com/egs/java.util/coll_Cache.html for an example
2. Put the key-value inside a WeakHashMap which will release its content before running out of memory (in effect it releases its content every Full G.C [for the one that are not referenced elsewhere] which might be too aggressive for your needs)
3. Adding your objects inside a SoftReference is a similar approach to 2 (though because Java doesn't provide SoftReference HashMap you will need to do it yourself). SoftReferences are cleared less aggressively then WeakReferences.
4. You can use java.util.Timer (as suggested by mayankeagle) to perform the cleanup.
That can be done in several ways:
a. run a periodic "cleanup task" that will check the timestamp of the queries and remove "old" ones
b. Add a TimerTask for each new query. The timer task logic will be to remove the associated query when invoked.

If none of the above is suitable for you then you can look for a third party solution (which one of them is oscache that was suggested by bloodredsun) or you can build a real cache service one your own (TimYates link shows you how to do that - code is provided).
For more open source cache solutions see:
http://java-source.net/open-source/cache-solutions (one of them is jcache which is simple and easy to use and is based on JSR-107 and another one is the Jakarta jcs cache which is also pretty simple)
0
 

Author Comment

by:skn73
ID: 13910748
Thanks!
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13910841
NP. :-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question