/* Reattore HTTP Server
Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id: FileCache.java,v 1.2 2003/01/24 03:01:52 michaelh Exp $
*/
package juju.reattore.server.intercept.impl;
import java.io.*;
import java.util.*;
import org.apache.commons.logging.*;
import juju.reattore.io.impl.ChannelFileSource;
import juju.reattore.io.ByteSource;
import juju.reattore.util.GaugeStat;
/** A simple request based cache for ChannelFileSources.
*/
public class FileCache {
/** File to List of CachedChannelFileSource */
private final Map cache = new HashMap();
private static GaugeStat allocStat = new GaugeStat(FileCache.class, "Alloc");
private class CachedChannelFileSource
extends ChannelFileSource {
private File fon;
private String req;
private CachedChannelFileSource(String req, File fon)
throws IOException {
super(fon);
this.fon = fon;
this.req = req;
}
public void close() {
rewind();
((List)cache.get(req)).add(this);
allocStat.dec();
}
}
/** Attempt to fetch from the cache.
@param req The request key to fetch.
@return The data, or null on cache miss.
*/
public ByteSource tryGet(String req) {
/* Look it up in the cache */
List l;
if ((l = (List)cache.get(req)) == null) {
l = new LinkedList();
cache.put(req, l);
}
if (l.size() > 0) {
/* Have one in the cache. Use it. */
allocStat.inc();
return (ByteSource)l.remove(0);
}
else {
return null;
}
}
/** Fetch from the cache or from disk, throwing on failure.
@param req The request key to fetch.
@param resolved The file to fetch from.
@return The data, or null on cache miss.
@throws IOException from the ChannelFileSource if the file
can't be found.
*/
public ByteSource get(String req, File resolved)
throws IOException {
ByteSource ret;
if ((ret = tryGet(req)) != null) {
return ret;
}
else {
/* Not in the cache - create a new one */
CachedChannelFileSource cf = new CachedChannelFileSource(req, resolved);
allocStat.inc();
return cf;
}
}
}