Originally posted by saikrishna cinux:
Hi Steve,
when you say caching, is there only one way to achive this ?
There are many different ways to cache an object. You are caching an object to a static class member, which is great for small number of objects to be reached quickly. Other ways include serializing, storing inside a Map of some sort, and there are Caching frameworks out there that let you cache to local memory then store to more persistent media when memory runs low or last-access-time times out.
Originally posted by saikrishna cinux:
why dont you accept it as single ton too?
because i'll be using only one object of that particular inner class which is associated to that bean(EMP)
A Singleton is an object that follows a very specific design pattern that makes it so you CAN have ONLY one object of that type created. You do have only one object of the type created, but you COULD have more if you continue to call new EmpFirstNameComparator() a few more times - or if you give the code to me and I call new EmpFirstNameComparator().
You should read
This Wikipedia Page to learn more. By the way - the comparators in this case do not need to be Singletons, and so should NOT be made into Singletons. Don't over-use the Singleton Pattern.
Originally posted by saikrishna cinux:
Please, if you dont mind can you please explain me more about caching technique.
1.what are the ways to achive caching? Do i need to create only inner class singleton objects for achiving caching?
No, As a matter of fact there is usually no need to cache singletons. You use a Cache to store frequently used objects that don't change much so that you don't have to make them all the time. This is especially important if creating those objects is costly and the code that uses them has heavy traffic - which could then cause a bottle neck. A singleton can be created only once, so you don't need to build a Cache to get it, just call the Singleton's getInstance() method. In a sense, the Singleton is a self-caching Object.
Originally posted by saikrishna cinux:
2. is there any other way to achive?
Yes, to a database, to serialized objects, to a Map instead of several instance variables ... there are as many ways to implement a Cache as there is to store objects. The key is to make sure that accessing the Cache is quicker than re-making the object.
Originally posted by saikrishna cinux:
3. If caching is implemented in the application for loading millions of records from database it may take more time and more memory(heap) ...
Yes. The trade of with Cache is you spend more Memory to hold the objects in
exchange for quicker responses to requests for the object. There are 2 different kinds of Cache's I can think of, which I call the preemptive cache and the lazy cache. A Preemptive Cache will actively search for objects to make and store for later use. Because the objects are expensive to make then Caching up front like this can take a lot of time, but we may be willing to spend all this extra time now so we get quick responsiveness later, like for instance if we are making a game it is good to cache all the graphics we can up front, so when they come into play we done have a slow down as the graphic gets pulled from wherever it is stored. In the Comparator example above you used this preemptive caching technique.
The Lazy Cache waits for an object to be requested and made for the first time, it then stores it so later on it can be accessed much quicker. Here you get quicker start-up times and lower memory consumption at the start of the application, while your first access to data may be slow, later access will be quick. An example of when to use this would be if you perform searches over a database. You search and store the results, next time someone does the same search, the Cache would check to see if the DB was changed, and if not just return the Cached values. If we wanted to use Lazy Loading Cache in the Comparator example, it might look like this:
Of course for our Comparator application it doesn't really matter...
If you do have millions of objects coming from a DB, then you would have to be memory conscious about storing your cache. A few ways to help you out with this would be to store the most frequently used objects in a memory cache (or the last, 1000 objects referenced). Then use serialization to store other objects into a BLOB in a special DB table, or use a DB View Table that was designed to hold the results of a specific Query, so you can do a select all on it without making the DB filter a larger data set.
Also, if caching from a DB then it is a good idea to keep track of times when Caches were made, and each time the cache is accessed query the DB for last change time so you know if the data you are retrieving is up to date. You don't want to be giving out old data.
Finally, with memory intensive Caches you should implement a time out strategy. If the object hasn't been used in a while, let it die and get garbage collected. These Caches may have tiers of storage.
1) Newly Cached items get stored as normal references
2) Items not accessed in a while get moved to Soft References
SoftReferences will be collected when the gc() needs memory, but are not the priority for cleanup. You would guarantee that the older items get cleaned when memory gets low, but generally keep them alive when you have enough memory to work with.
Originally posted by saikrishna cinux:
4. when do you think this is the best solution for designing an applciation?
Depends entirely on the application. You should do a google search. There are several Frameworks that will help you add Caching to your application, and many articles that talk about strategies and design aspects. For example, a Cache should be invisible to the class trying to get a hold of an object, so a Cache is usually implemented is conjunction with a Factory Pattern on the Data Access Tier.
[ October 15, 2008: Message edited by: Steve Luke ]