View Javadoc
1 /* Reattore HTTP Server 2 3 Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz> 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 19 $Id: FileCache.java,v 1.2 2003/01/24 03:01:52 michaelh Exp $ 20 */ 21 22 package juju.reattore.server.intercept.impl; 23 24 import java.io.*; 25 import java.util.*; 26 import org.apache.commons.logging.*; 27 28 import juju.reattore.io.impl.ChannelFileSource; 29 import juju.reattore.io.ByteSource; 30 import juju.reattore.util.GaugeStat; 31 32 /*** A simple request based cache for ChannelFileSources. 33 */ 34 public class FileCache { 35 36 /*** File to List of CachedChannelFileSource */ 37 private final Map cache = new HashMap(); 38 39 private static GaugeStat allocStat = new GaugeStat(FileCache.class, "Alloc"); 40 41 private class CachedChannelFileSource 42 extends ChannelFileSource { 43 44 private File fon; 45 private String req; 46 47 private CachedChannelFileSource(String req, File fon) 48 throws IOException { 49 50 super(fon); 51 this.fon = fon; 52 this.req = req; 53 } 54 55 public void close() { 56 rewind(); 57 ((List)cache.get(req)).add(this); 58 59 allocStat.dec(); 60 } 61 } 62 63 /*** Attempt to fetch from the cache. 64 65 @param req The request key to fetch. 66 @return The data, or null on cache miss. 67 */ 68 public ByteSource tryGet(String req) { 69 /* Look it up in the cache */ 70 List l; 71 72 if ((l = (List)cache.get(req)) == null) { 73 l = new LinkedList(); 74 cache.put(req, l); 75 } 76 77 if (l.size() > 0) { 78 /* Have one in the cache. Use it. */ 79 80 allocStat.inc(); 81 return (ByteSource)l.remove(0); 82 } 83 else { 84 return null; 85 } 86 } 87 88 /*** Fetch from the cache or from disk, throwing on failure. 89 90 @param req The request key to fetch. 91 @param resolved The file to fetch from. 92 @return The data, or null on cache miss. 93 @throws IOException from the ChannelFileSource if the file 94 can't be found. 95 */ 96 public ByteSource get(String req, File resolved) 97 throws IOException { 98 99 ByteSource ret; 100 101 if ((ret = tryGet(req)) != null) { 102 return ret; 103 } 104 else { 105 /* Not in the cache - create a new one */ 106 CachedChannelFileSource cf = new CachedChannelFileSource(req, resolved); 107 108 allocStat.inc(); 109 return cf; 110 } 111 } 112 }

This page was automatically generated by Maven