correct way of processing cache

M

mark jason

hi,
I need to check for a cache file that contains a MyCache object with
some precomputed values( based on data from image files in a folder).
The algorithm is as follows,
1.If a cache file exists, retrieve the MyCache object from cache file
and get a list of image file names from MyCache object,
and compare it with current List of filenames.
2.If both lists are same ,I can use the image data from MyCache
object.
3.if lists are different ,compute the image based data,create new
MyCache object with this image based data and current list of
filenames.
4.do step 3 if no cache file exists.

I tried to implement this as follows.However,I am not sure if this is
the 'object oriented way' to do this.Can somebody advise?

thanks and regards,
mark

<code>
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io_ObjectInputStream;
import java.util.List;


public class CacheChecker {
private MyCache cachefile;

public void checkCache(String folder){
List<String> newFileNames = parseFolder(folder);
try {
MyCache cache = getExistingCache(folder);
compareListsAndProcessCache(folder,newFileNames,cache);
} catch (IOException e) {
//cache file not found,need to create new cache
calculateAndCreateNewCache();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

}

private void compareListsAndProcessCache(String folder, List<String>
newFileNames, MyCache cache) {
List<String>oldFileNames = cache.getImgNames();
//if lists are same get data from cache
if(newFileNames.equals(oldFileNames)){
this.cachefile = cache;
}else{
//lists not same
calculateAndCreateNewCache();
}
}

private void calculateAndCreateNewCache() {
//do calculations
//create MyCache from calculated data and filenames list
//write MyCache object to 'mycachefile'
}

private MyCache getExistingCache(String folder)throws IOException,
ClassNotFoundException{
FileInputStream fin = new FileInputStream(folder+File.separator
+"mycachefile");
ObjectInputStream oin = new ObjectInputStream(fin);
MyCache cache = (MyCache)oin.readObject();
oin.close();
fin.close();
return cache;
}

private List<String> parseFolder(String folder) {
// return current list of image filenames
return null;
}
}


class MyCache {
private List<String> imgNames;
private double[][] imagesData;

public MyCache(List<String> imgNames,double[][] imagesData){
this.imgNames = imgNames;
this.imagesData = imagesData;
}
public List<String> getImgNames() {
return imgNames;
}
public void setImgNames(List<String> imgNames) {
this.imgNames = imgNames;
}
public double[][] getImagesData() {
return imagesData;
}
}


</code>
 
R

Roedy Green

I tried to implement this as follows.However,I am not sure if this is
the 'object oriented way' to do this.Can somebody advise?

here is some code I use to check the sizes of images which I keep in a
cache to avoid recomputing them. Note how the client of the method
does not concern itself with whether the info is cached on not. That
is none of its business. That is a purely implementation detail.


/**
* capacity of HashMap to store dimensions of cached images
*/
private static final int IMAGE_CACHE_CAPACITY = 4000;

/**
* to avoid work of checking image dimensions on disk, we cache
results of good images found earlier
* we store two shorts in the width x height.
*/
private static final HashMap<String, Integer> doneBefore = new
HashMap<String, Integer>( IMAGE_CACHE_CAPACITY );


/**
* Find out width and height of an image in the project image
tree. Do not confuse with the more general
* ImageInfo.getImageDimensions.
*
* @param projectImage name of image file in the image tree, e.g.
navigate/home.png
*
* @return array[2] [0[=width [1]=height 0 0 means invalid. Does
not throw exception.
*/
public static int[] getProjectImageDimensions( String projectImage
)
{
// see if we have done this one before
Integer wxh = doneBefore.get( projectImage );
final int width;
final int height;

if ( wxh != null )
{
// use previous dimensions, saving time to look up image.
// packed width in msw and height in lsw
width = wxh >>> 16;
height = wxh & 0xffff;
return new int[] { width, height };
}
else
{
int[] dimensions = ImageInfo.getImageDimensions(
configuration.getWebrootOnEWithSlashes() + "/image/" + projectImage );

width = dimensions[ 0 ];
height = dimensions[ 1 ];

if ( width == 0 || height == 0 )
{
return new int[] { 0, 0 };
}

// new good one
// pack width in msw and height in lsw
doneBefore.put( projectImage, width << 16 | height );
return dimensions;
}
}
--
Roedy Green Canadian Mind Products
http://mindprod.com

Doubling the size of a team will probably make it produce even more slowly.
The problem is the more team members, the more secrets, the less each team
member understands about how it all fits together and how his changes may
adversely affect others.
 
E

Eric Sosman

hi,
I need to check for a cache file that contains a MyCache object with
some precomputed values( based on data from image files in a folder).
The algorithm is as follows,
1.If a cache file exists, retrieve the MyCache object from cache file
and get a list of image file names from MyCache object,
and compare it with current List of filenames.
2.If both lists are same ,I can use the image data from MyCache
object.
3.if lists are different ,compute the image based data,create new
MyCache object with this image based data and current list of
filenames.
4.do step 3 if no cache file exists.

I tried to implement this as follows.However,I am not sure if this is
the 'object oriented way' to do this.Can somebody advise?

Just a few comments, "food for thought" more than "advice."
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io_ObjectInputStream;
import java.util.List;


public class CacheChecker {
private MyCache cachefile;

public void checkCache(String folder){
List<String> newFileNames = parseFolder(folder);

It is strange that your cache represents its contents as a List,
when a Set would seem more natural. As Lists, [ "a.jpg", "b.jpg" ]
and [ "b.jpg", "a.jpg" ] are different, while as Sets they would be
the same, { "a.jpg" "b.jpg" }.
[...]
private void calculateAndCreateNewCache() {
//do calculations
//create MyCache from calculated data and filenames list
//write MyCache object to 'mycachefile'

Perhaps you meant to imply this, but it's not entirely clear from
the commentary: If the old cache held { "a.jpg" "b.jpg" } and the new
one holds { "b.jpg" "c.jpg" }, there's no need to re-process "b.jpg".

Also, I think that "on general principles" it would be better to
separate the calculation of the new cache from the writing thereof to
persistent storage. As Patricia Shanahan recently remarked on another
thread (I paraphrase), a method whose "natural" name is doOneAndTwo
would likely be better off as two methods with one purpose each.
private MyCache getExistingCache(String folder)throws IOException,
ClassNotFoundException{
FileInputStream fin = new FileInputStream(folder+File.separator
+"mycachefile");
ObjectInputStream oin = new ObjectInputStream(fin);
MyCache cache = (MyCache)oin.readObject();
oin.close();
fin.close();

Closing oin also closes whatever it wraps, in this case fin. (And
closing fin also closes whatever invisible interior things it in turn
may wrap.) I think the double-close may have become harmless with the
introduction of the Closeable interface, but it makes my skin crawl
anyhow.

It would be a very bad idea to close fin and *then* close oin ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top