001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 * 
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.common.file;
020
021import java.io.File;
022import java.io.IOException;
023import java.io.InputStream;
024
025/**
026 * A cache of {@link File}s.
027 * <p>
028 * The cache uses application-chosen keys.
029 * <p>
030 * To check presence in the cache, use {@link #getFile}.
031 * <p>
032 * To put a new {@link InputStream} in the cache, use {@link #putFile(String, InputStream)}. Or if you'd like a
033 * {@link File} object into which to write some data, get one using {@link #getTempFile}, put the actual binary in it,
034 * then pass this file to {@link #putFile(String, File)}.
035 *
036 * @see LRUFileCache
037 */
038public interface FileCache {
039
040    /**
041     * Gets the size of the cache, in bytes.
042     */
043    long getSize();
044
045    /**
046     * Gets the number of items in the cache.
047     */
048    int getNumberOfItems();
049
050    /**
051     * Creates a temporary file.
052     */
053    File getTempFile() throws IOException;
054
055    /**
056     * Puts a file in the cache.
057     *
058     * @param key the cache key
059     * @param in the input stream to cache (closed afterwards)
060     * @return the cached file
061     * @throws IllegalArgumentException if the key is illegal
062     */
063    File putFile(String key, InputStream in) throws IOException;
064
065    /**
066     * Puts a file in the cache.
067     * <p>
068     * The file must have been created through {@link #getTempFile()}. The file is "given" to this method, who will
069     * delete it or rename it.
070     *
071     * @param key the cache key
072     * @param file the file to cache
073     * @return the cached file
074     * @throws IllegalArgumentException if the key is illegal
075     */
076    File putFile(String key, File file) throws IOException;
077
078    /**
079     * Gets a file from the cache.
080     * <p>
081     * A returned file will never be deleted from the filesystem while the returned file object is still referenced,
082     * although it may be purged from the cache.
083     *
084     * @param key the cache key
085     * @return the cached file, or {@code null} if absent
086     */
087    File getFile(String key);
088
089    /**
090     * Clears the cache.
091     * <p>
092     * Files will not be deleted from the filesystm while the returned file objects are still referenced.
093     */
094    void clear();
095
096}