📄 jahiacache.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////// JahiaCache class// DJ 04.01.2001//// getOID ()// getValue (ID, JahiaCacheIO)// setValue (Object, ID, JahiaCacheIO)// removeValue (ID, JahiaCacheIO)//package org.jahia.data.cache;import java.util.*; // Hashtableimport org.jahia.utils.*; // JahiaConsoleimport org.jahia.data.fields.*; // JahiaFieldimport org.jahia.data.cache.*; // JahiaCache, JahiaCacheIOimport org.jahia.services.*; // ServicesRegistryimport org.jahia.exceptions.JahiaException;/** * This class provides an interface to cache any object in a transparent way. New * instance of this Class should be created using the JahiaCacheFactory. * * The cache is in charge of loading, saving and deleting objects. When creating a new * JahiaCache, among other parameters you should give a JahiaCacheIO object that supplies * the methods to save, load and delete objects on disk (in a database, file or whatever). * When the cache is implemented, you should never use direct access to data on the disk, * but rather use calls to JahiaCache, so the cache is always perfectly synchronized with * the data on the disk. * * The unique identifier of a JahiaCache is its name. * * @see org.jahia.services.cache.JahiaCacheFactory * @see org.jahia.data.cache.JahiaCacheIO * @author David Jilli */public class JahiaCache{ private static int OIDcounter = 0; private JahiaCacheIO cacheIO; private String cacheName; private String cachedesc; private int maxCachedObjects; /** * @associates Object */ private Hashtable myTable = new Hashtable(); /** * @associates String */ private Vector cachedObjectIDs = new Vector(); // list of all cached object IDS. /** * This constructor should only be called by JahiaCacheFactory, to keep a trace of * every instance of JahiaCache. */ public JahiaCache(String cacheName, String cachedesc, JahiaCacheIO cacheIO, int maxCachedObjects) { this.cacheIO = cacheIO; this.cacheName = cacheName; this.cachedesc = cachedesc; this.maxCachedObjects = maxCachedObjects; } /** * Returns the name of the cache. * @return cache name */ public String getCacheName() { return this.cacheName; } /** * Returns the desc of the cache. * @return cache desc */ public String getCachedesc() { return this.cachedesc; } /** * Returns the maximum number of cached objects in this cache. * @return max cached objects */ public int getMaxCachedObjects() { return this.maxCachedObjects; } /** * Returns the number of elements in this cache * @return an integer corresponding to the number of elements in this * cache. */ public int size() { return this.myTable.size(); } /** * Sets the maximum number of cached objects in the cache and resizes it on the fly. * @param maxCachedObjects Maximum number of cached objets */ public void setMaxCachedObjects(int maxCachedObjects) { this.maxCachedObjects=maxCachedObjects; while (myTable.size() > maxCachedObjects) { myTable.remove(cachedObjectIDs.get(0)); cachedObjectIDs.remove(0); } } /** * Gets the value of an object thru the cache. * Uses the loadValue method of the JahiaCacheIO parameter if not found in the cache. * * @param stringID String ID of the object requested * @param paramsIO optionals parameters for the cache IO (optional!) * @return The loaded object */ public Object getValue(String stringID, Object paramsIO ) throws JahiaException { Object thisObject = myTable.get(stringID); if (thisObject == null) { thisObject = cacheIO.loadValue (stringID, paramsIO); if (thisObject != null) { doCacheObject (stringID, thisObject); } } return thisObject; } // end getValue // if there is no paramsIO lets put null public Object getValue(String stringID) throws JahiaException { return getValue(stringID, null ); } // end getValue (no paramsIO) /** * Sets the value of an object thru the cache. * Uses the saveValue method of the JahiaCacheIO to save it on disk * * @param theObject The object to save * @param stringID String ID of the object * @param paramsIO JahiaCacheIO object including saveValue method */ public void setValue(Object theObject, String stringID, Object paramsIO) throws JahiaException { doCacheObject ( stringID, theObject ); cacheIO.saveValue ( theObject, stringID, paramsIO); } // end setValue // if there is no paramsIO lets put null public void setValue(Object theObject, String stringID) throws JahiaException { setValue(theObject, stringID, null); } // end setValue (no paramsIO) /** * Remove an object from the cache * Uses the deleteValue method of the JahiaCacheIO to remove it from disk * * @param stringID String ID of the object * @param paramsIO JahiaCacheIO object including saveValue method */ public void removeValue(String stringID, Object paramsIO) throws JahiaException { cachedObjectIDs.remove(stringID); myTable.remove (stringID); cacheIO.deleteValue (stringID, paramsIO); } // end removeValue // if there is no paramsIO lets put null public void removeValue(String stringID) throws JahiaException { removeValue(stringID, null); } // end removeValue (no paramsIO) /** * Internal method : adds an object to the cache, and handles the maximum object limits * * @param stringID String ID of the object * @param theObject the Object to add */ private void doCacheObject (String stringID, Object theObject) { if (maxCachedObjects != 0) { while (myTable.size() >= maxCachedObjects) // if it's > we have a problem! { myTable.remove (cachedObjectIDs.get(0)); cachedObjectIDs.remove(0); } // cache the object and put it in the "cached object list" myTable.put (stringID, theObject); cachedObjectIDs.add(stringID); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -