Spartan Java

Very easy FIFO cache

by on May.02, 2008, under Performance, Tips

There are plenty of times when you need a cache for Java objects in your code. A very easy to use and simple implementation of a cache is provided with Apache ORO, wisely called CacheFIFO (http://jakarta.apache.org/oro/api/org/apache/oro/util/CacheFIFO.html).

This is a very simple example of how I used it to cache user POJOs in an enterprise applications back-end:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import org.apache.oro.util.CacheFIFO;
 
public class UserHandler {
 
    private static CacheFIFO cache = new CacheFIFO(100);
 
    public static void addUser (String login, User u){
        cache.addElement(login, u);
    }
 
    public static User getUser (String login){
        return (User)cache.getElement(login);
    }
}


:, , ,

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...