cachegeneration.java
来自「很棒的web服务器源代码」· Java 代码 · 共 664 行 · 第 1/2 页
JAVA
664 行
// CacheGeneration.java// $Id: CacheGeneration.java,v 1.36 2003/02/14 16:25:01 ylafon Exp $// (c) COPYRIGHT MIT, INRIA and Keio, 1999.// Please first read the full copyright statement in file COPYRIGHT.htmlpackage org.w3c.www.protocol.http.cache;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;import java.io.File;import java.io.PrintStream;import org.w3c.util.LRUAble;import org.w3c.util.LRUList;import org.w3c.util.LookupTable;import org.w3c.util.SyncLRUList;public class CacheGeneration implements LRUAble { // the usual debug flag private static final boolean debug = true; // hashtable of resources private Hashtable lookupTable = null; // the LRUList of resources private SyncLRUList lruList = null; // the occupation of this generation private long bytecount = 0; // the capacity of this generation private long bytelimit = 0; // stored size private long bytestored = 0; // serialization flag private boolean saved = false; // serialization flag private boolean loaded = false; // resource count private int cr_count = 0; // the ID of this generation private int id = 0; // a Vector or resource to be removed private Vector toDel = null; // our father private CacheStore store = null; protected File generationFile = null; /** * set the file where the generation is stored * @param generationFile the file */ public void setGenerationFile(File generationFile) { this.generationFile = generationFile; } /** * get the generation file * @return a File */ public File getGenerationFile() { return generationFile; } /** * Is the generation loaded? * @return a boolean */ public boolean isLoaded() { return loaded; } /** * Set the generation as loaded or unloaded * @param loaded the new loaded flag */ protected void setLoaded(boolean loaded) { this.loaded = loaded; } /** * Is the generation saved? * @return a boolean */ public boolean isSaved() { return saved; } /** * Set the generation as saved or not. * @param saved a boolean */ protected void setSaved(boolean saved) { this.saved = saved; } /** * LRU management - previous entry. */ protected LRUAble prev = null; /** * LRU management - next entry. */ protected LRUAble next = null; /** * LRU management - Get next node. * @return A CacheGeneration instance. */ public LRUAble getNext() { return next; } /** * LRU management - Get previous node. * @return A CacheGeneration instance. */ public LRUAble getPrev() { return prev; } /** * LRU management - Set next node. */ public synchronized void setNext(LRUAble next) { this.next = next; } /** * LRU management - Set previous node. */ public synchronized void setPrev(LRUAble prev) { this.prev = prev; } /** * Get the ID of this generation * @return an int, the generation number */ public int getId() { return id; } /** * Set the ID of this generation * Useful to reuse generation * @param an integer, the new generation number */ public synchronized void setId(int id) { this.id = id; } /** * Give the acual occupation level of this generation * @return a long, the number of bytes of this generation */ public long getCachedByteCount() { return bytecount; } /** * Give the fill ratio for the cached resources * @return a float between 0 and 1 */ public float getFillRatio() { return ((float) bytecount / (float) bytelimit); } /** * Give the acual storeage occupation level of this generation * @return a long, the number of bytes of this generation */ public long getStoredByteCount() { return bytestored; } /** * Get the bytecount limit for this generation * @return a long, the maximum number of bytes */ public long getByteLimit() { return bytelimit; } /** * Set the new bytecount limit, not that it may perform a cleanup * if necessary. * @param long, the new maximum number of bytes */ public synchronized void setByteLimit(long newlimit) { bytelimit = newlimit; if (newlimit >= bytecount) { return; } // try to get some space long to_save = newlimit - bytecount; // be nice to_save -= collectSpace(newlimit - bytecount, true); // then get the space we want ;) to_save -= collectSpace(to_save, false); } /** * Deletes a resource from the "to be deleted" vector * it updates also the number of bye stored in this generation * @return the number of bytes saved. */ public long deleteStored(CachedResource cr) { if (! loaded) throw new UnloadedGenerationException("generation "+id); if (debug) { System.out.println("Deleting "+cr.getIdentifier()+ "from generation: "+id); } toDel.removeElement(cr); long saved = cr.delete(); synchronized(this) { bytestored -= saved; } store.getState().notifyResourceDeleted(cr); return saved; } /** * Check if a resource has been cached in this generation * @param url the resource url * @return a boolean */ public synchronized boolean containsResource(String url) { return (lookupTable.get(url) != null); } /** * Get all the files handled by this generation * @return an enumeration of File */ public synchronized Enumeration getFiles() { Vector files = new Vector(); if (loaded) { Enumeration enum = lookupTable.elements(); while (enum.hasMoreElements()) { CachedResource cr = (CachedResource) enum.nextElement(); File file = cr.getFile(); if (file != null) { files.addElement(file); } } } else { Enumeration enum = lookupTable.elements(); while (enum.hasMoreElements()) { String file = (String) enum.nextElement(); if (! file.equals("")) { files.addElement(new File(file)); } } } return files.elements(); } /** * Get the CachedResource relative to the given URL. * @param url the URL of the CachedResource to find * @return a CachedResource or null. */ public synchronized CachedResource lookupResource(String url) { if (! loaded) throw new UnloadedGenerationException("generation "+id); return (CachedResource)lookupTable.get(url); } /** * can this resource be stored? * If the resource is in the generation, only the delta will be taken * into account * @param CachedResource cr, the candidate. * @param long size, the size of the candidate. * @return a boolean, if this generation can cache it or not */ private boolean canStore(CachedResource cr, long size) { if (! loaded) throw new UnloadedGenerationException("generation "+id); CachedResource old_cr = null; old_cr = (CachedResource) lookupTable.get(cr.getIdentifier()); if (old_cr != null) { long delta = size - old_cr.getCurrentLength(); if ( (bytecount + delta) > bytelimit) { return false; } } if ((size + bytecount) > bytelimit) { return false; } return true; } /** * Adds this resource, if possible * @param cr, the candidate. * @param size, the size of the candidate. * @return a boolean, true if this resource has been cached */ public synchronized boolean addResource(CachedResource cr, long size, long oldsize) { if (! loaded) throw new UnloadedGenerationException("generation "+id); if (canStore(cr, size)) { CachedResource old_cr = null; old_cr = (CachedResource) lookupTable.get(cr.getIdentifier()); // do we already have this resource? if (old_cr != null) { // this is the real oldsize oldsize = old_cr.getCurrentLength(); long delta = size - oldsize; if ((bytecount + delta) > bytelimit) { return false; } lookupTable.remove(cr.getIdentifier()); lruList.remove(cr); bytecount -= oldsize; toDel.addElement(old_cr); store.getState().notifyResourceReplaced(cr, oldsize); } else { store.getState().notifyResourceAdded(cr, oldsize); } lookupTable.put(cr.getIdentifier(), cr); cr.generation = this; lruList.toHead(cr); bytestored += size;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?