0

I'm writting a Class that implements an LRU cache with a LinkedHashMap. Typically I would need to override the methods put and get in order to write to disk when an object is added to the cache and fetch from disk if the object is not found in the cache.

My LRUCache class looks like:

public class LRUCache<K, V> extends LinkedHashMap<K, V>
    implements Serializable {
    /** 
     * File where the elements of the cache are stored
     */
    private File cacheFile = null;

    /**
     * UID of the class for serialization.
     */
    private static final long serialVersionUID = 1L;
    /**
     * Maximum number of entries in the cache.
     */
    private final int maxEntries;

    /**
     * Default constructor of the cache.
     *
     * @param newMaxEntries
     *      the maximum number of entries in the cache.
     */
    public LRUCache(final int newMaxEntries, String fileName) {
        super(newMaxEntries + 1, 1.0f, true);
        this.maxEntries = newMaxEntries;
        this.cacheFile = new File(fileName);
    }


    @Override
    public V get(Object key) {
        V VObject = super.get(key);
        if (VObject == null) {
            // TODO: Fetch from disk

        }
        return VObject;
    }

    @Override
    public V put(K key, V value) {
        // TODO: Write to disk

        return super.put(key, value);
    }

    @Override
    protected final boolean
            removeEldestEntry(final Map.Entry<K, V> eldest) {
        return super.size() > maxEntries;
    }

}

My question is how I can override these two methods to do it as fastest as possible. Would it be a good idea that the cached objects implement Externalize?

Thanks

5
  • 1
    Please don't reinvent the wheel unless you have to. Use Guava cache support. It's light weight and threadsafe. Commented Nov 13, 2012 at 11:22
  • Guava does not store more data than what would fit in RAM. Guava caches are local to a single run of your application so they do not store data in files. Commented Nov 13, 2012 at 15:53
  • Holy crap I didn't see the File. Holy crap don't don't do that! You should look at something like Ehcache (or memcache or redis or the myriad of things that already do this). Seriously don't write your own distributed cache. It is not an easy problem and there are tons of concurrency issues to consider. Commented Nov 13, 2012 at 16:38
  • @AdamGent: Thanks for your advice. I think that JCS is the most suitable option for me but I can't find if it is persistent, do you know anything about it? Commented Nov 13, 2012 at 16:47
  • I have only used Guava's cache support, Ehcache and Redis. I used memcache a long time ago. Redis is persistent but its more than just cache. Ehcache has persistence. Commented Nov 13, 2012 at 16:53

1 Answer 1

1

Sorry to say that, but actually you have to solve various problems here:

  • What is the best/fastest java object serialization? Already discussed in other questions.
  • How to update a previous written object to the file?
  • How do you effectively find to the byte offset of the stored object in the file, when retrieval is needed?
  • How to delete the item from the file and compact the file?
  • How to write fast and avoid random access to the hard disk storage?

All answers to the last questions you find in books on databases.

If you just have a view objects that do not change very often and you are up for a simple solution, just serialize the HashMap into the file on every put. If the data is critical maybe it is a good thing to write always a new file and delete the old one afterwards, to prevent data loss.

BTW: Within your sample code the removal of a stored disk object is missing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.