aspose file tools
The moose likes Java in General and the fly likes reading files & caching Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "reading files & caching" Watch "reading files & caching" New topic
Author

reading files & caching

Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
I just built a class that will read a .properties file and get values from it. Since I'll be making frequent calls to this for configuration values throughout my app, how can I cache access to this file so I'm not reading the entire file on every call?

Would I need to do this?

Here's the class:



I created another class which extends Property (above):



In my app I'd then make calls to those methods when needed. Won't I be adding some real overhead by constantly calling & reading this file?

Advice is much appreciated, thanks!

-v
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Why not just define your class as a Singleton?


There is no emoticon for what I am feeling!
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
dont use absolute file paths. use relative file paths or even better load the properties as a resource from classpath.

if you need frequent access to these properties then just keep a reference to it and pass it to the dependent objects.


pascal
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I wrote a properties cache for my first Java project. It has a map keyed on filename and each entry in the map is a collection of properties. It reads all the properties files in a list of directories into cache in a single call. For server apps I always have a management API that lets me clear and reload one or all files in the cache. It looks something like ...

String prop = Properties.getProperty( fileName, propertyName );


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
This is a good opportunity to use SoftReference. If you store your cache of Properties in a SoftReference, then Java will delete your cache automatically, in a low-memory situation. That's fine, because the cache contents can be recreated from the Properties files at any time. By using SoftReference, you can stop worrying about whether your cache is getting too big.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading files & caching
 
Similar Threads
refreshing the page automatically if the configuration file changes
InterruptedException
SMTP server connections
Working with files in EJB
Unable to register Derby driver