Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't say what part of the process you need help with, but here's what I would do:</p> <p>First you have to create a class to hold the cache and metadata. Let's call it ImageCache. Of course you need a String for the image name and a byte[] to hold the image contents itself. Then add a collection of them in the class that currently extracts the images from the database. When an image request comes in, search through the collection for the filename. If found, just send the byte[] back. If it's not in the collection, read it from the db then create a new ImageCache object from it and add it to the collection. </p> <p>For bonus points, include a Date lastAccessed field and a Comparator. The Comparator and the date field will let you maintain the cache. Let's say your cache can hold 50 images (based on available memory). Your Comparator can be used to sort the collection of ImageCache objects in <a href="http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used" rel="nofollow">LRU (Least Recently Used)</a> order, sorting by lastAccesed in ascending order. When the collection reaches the maximum entries, find the entry with the oldest lastAccessed, and replace it. That will give you the best cache hit rate while not taking all the heap memory.</p> <p>Another option is to just use Hibernate as an abstraction layer between the program and the database. This will offer many benefits, one of which will be highly configurable and completely automatic caching.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload