⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractdiskpersistencelistener.java

📁 oscache-2.4.1-full
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package com.opensymphony.oscache.plugins.diskpersistence;

import com.opensymphony.oscache.base.Config;
import com.opensymphony.oscache.base.persistence.CachePersistenceException;
import com.opensymphony.oscache.base.persistence.PersistenceListener;
import com.opensymphony.oscache.web.ServletCacheAdministrator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;

import java.util.Set;

import javax.servlet.jsp.PageContext;

/**
 * Persist the cache data to disk.
 *
 * The code in this class is totally not thread safe it is the resonsibility
 * of the cache using this persistence listener to handle the concurrency.
 *
 * @author <a href="mailto:fbeauregard@pyxis-tech.com">Francois Beauregard</a>
 * @author <a href="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
 * @author <a href="&#109;a&#105;&#108;&#116;&#111;:chris&#64;swebtec.&#99;&#111;&#109;">Chris Miller</a>
 * @author <a href="mailto:amarch@soe.sony.com">Andres March</a>
 */
public abstract class AbstractDiskPersistenceListener implements PersistenceListener, Serializable {
    public final static String CACHE_PATH_KEY = "cache.path";

    /**
    * File extension for disk cache file
    */
    protected final static String CACHE_EXTENSION = "cache";

    /**
    * The directory that cache groups are stored under
    */
    protected final static String GROUP_DIRECTORY = "__groups__";

    /**
    * Sub path name for application cache
    */
    protected final static String APPLICATION_CACHE_SUBPATH = "application";

    /**
    * Sub path name for session cache
    */
    protected final static String SESSION_CACHE_SUBPATH = "session";

    /**
    * Property to get the temporary working directory of the servlet container.
    */
    protected static final String CONTEXT_TMPDIR = "javax.servlet.context.tempdir";
    private static transient final Log log = LogFactory.getLog(AbstractDiskPersistenceListener.class);

    /**
    * Base path where the disk cache reside.
    */
    private File cachePath = null;
    private File contextTmpDir;

    /**
    * Root path for disk cache
    */
    private String root = null;

    /**
    *        Get the physical cache path on disk.
    *
    *        @return        A file representing the physical cache location.
    */
    public File getCachePath() {
        return cachePath;
    }

    /**
    *        Get the root directory for persisting the cache on disk.
    *        This path includes scope and sessionId, if any.
    *
    *        @return        A String representing the root directory.
    */
    public String getRoot() {
        return root;
    }

    /**
    *        Get the servlet context tmp directory.
    *
    *        @return        A file representing the servlet context tmp directory.
    */
    public File getContextTmpDir() {
        return contextTmpDir;
    }

    /**
    * Verify if a group exists in the cache
    *
    * @param group The group name to check
    * @return True if it exists
    * @throws CachePersistenceException
    */
    public boolean isGroupStored(String group) throws CachePersistenceException {
        try {
            File file = getCacheGroupFile(group);

            return file.exists();
        } catch (Exception e) {
            throw new CachePersistenceException("Unable verify group '" + group + "' exists in the cache: " + e);
        }
    }

    /**
    * Verify if an object is currently stored in the cache
    *
    * @param key The object key
    * @return True if it exists
    * @throws CachePersistenceException
    */
    public boolean isStored(String key) throws CachePersistenceException {
        try {
            File file = getCacheFile(key);

            return file.exists();
        } catch (Exception e) {
            throw new CachePersistenceException("Unable verify id '" + key + "' is stored in the cache: " + e);
        }
    }

    /**
    * Clears the whole cache directory, starting from the root
    *
    * @throws CachePersistenceException
    */
    public void clear() throws CachePersistenceException {
        clear(root);
    }

    /**
    * Initialises this <tt>DiskPersistenceListener</tt> using the supplied
    * configuration.
    *
    * @param config The OSCache configuration
    */
    public PersistenceListener configure(Config config) {
        String sessionId = null;
        int scope = 0;
        initFileCaching(config.getProperty(CACHE_PATH_KEY));

        if (config.getProperty(ServletCacheAdministrator.HASH_KEY_SESSION_ID) != null) {
            sessionId = config.getProperty(ServletCacheAdministrator.HASH_KEY_SESSION_ID);
        }

        if (config.getProperty(ServletCacheAdministrator.HASH_KEY_SCOPE) != null) {
            scope = Integer.parseInt(config.getProperty(ServletCacheAdministrator.HASH_KEY_SCOPE));
        }

        StringBuffer root = new StringBuffer(getCachePath().getPath());
        root.append("/");
        root.append(getPathPart(scope));

        if ((sessionId != null) && (sessionId.length() > 0)) {
            root.append("/");
            root.append(sessionId);
        }

        this.root = root.toString();
        this.contextTmpDir = (File) config.get(ServletCacheAdministrator.HASH_KEY_CONTEXT_TMPDIR);

        return this;
    }

    /**
    * Delete a single cache entry.
    *
    * @param key The object key to delete
    * @throws CachePersistenceException
    */
    public void remove(String key) throws CachePersistenceException {
        File file = getCacheFile(key);
        remove(file);
    }

    /**
    * Deletes an entire group from the cache.
    *
    * @param groupName The name of the group to delete
    * @throws CachePersistenceException
    */
    public void removeGroup(String groupName) throws CachePersistenceException {
        File file = getCacheGroupFile(groupName);
        remove(file);
    }

    /**
    * Retrieve an object from the disk
    *
    * @param key The object key
    * @return The retrieved object
    * @throws CachePersistenceException
    */
    public Object retrieve(String key) throws CachePersistenceException {
        return retrieve(getCacheFile(key));
    }

    /**
    * Retrieves a group from the cache, or <code>null</code> if the group
    * file could not be found.
    *
    * @param groupName The name of the group to retrieve.
    * @return A <code>Set</code> containing keys of all of the cache
    * entries that belong to this group.
    * @throws CachePersistenceException
    */
    public Set retrieveGroup(String groupName) throws CachePersistenceException {
        File groupFile = getCacheGroupFile(groupName);

        try {
            return (Set) retrieve(groupFile);
        } catch (ClassCastException e) {
            throw new CachePersistenceException("Group file " + groupFile + " was not persisted as a Set: " + e);
        }
    }

    /**
    * Stores an object in cache
    *
    * @param key The object's key
    * @param obj The object to store
    * @throws CachePersistenceException
    */
    public void store(String key, Object obj) throws CachePersistenceException {
        File file = getCacheFile(key);
        store(file, obj);
    }

    /**
    * Stores a group in the persistent cache. This will overwrite any existing
    * group with the same name
    */
    public void storeGroup(String groupName, Set group) throws CachePersistenceException {
        File groupFile = getCacheGroupFile(groupName);
        store(groupFile, group);
    }

    /**
    * Allows to translate to the temp dir of the servlet container if cachePathStr
    * is javax.servlet.context.tempdir.
    *
    * @param cachePathStr  Cache path read from the properties file.
    * @return Adjusted cache path
    */
    protected String adjustFileCachePath(String cachePathStr) {
        if (cachePathStr.compareToIgnoreCase(CONTEXT_TMPDIR) == 0) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -