Modifier and Type | Class and Description |
---|---|
(package private) static class |
DiskBasedCache.CacheHeader
Handles holding onto the cache headers for an entry.
|
private static class |
DiskBasedCache.CountingInputStream |
Cache.Entry
Modifier and Type | Field and Description |
---|---|
private static int |
CACHE_MAGIC
Magic number for current version of cache file format.
|
private static int |
DEFAULT_DISK_USAGE_BYTES
Default maximum disk usage in bytes.
|
private static float |
HYSTERESIS_FACTOR
High water mark percentage for the cache
|
private java.util.Map<java.lang.String,DiskBasedCache.CacheHeader> |
mEntries
Map of the Key, CacheHeader pairs
|
private int |
mMaxCacheSizeInBytes
The maximum size of the cache in bytes.
|
private java.io.File |
mRootDirectory
The root directory to use for the cache.
|
private long |
mTotalSize
Total amount of space currently used by the cache in bytes.
|
Constructor and Description |
---|
DiskBasedCache(java.io.File rootDirectory)
Constructs an instance of the DiskBasedCache at the specified directory using
the default maximum cache size of 5MB.
|
DiskBasedCache(java.io.File rootDirectory,
int maxCacheSizeInBytes)
Constructs an instance of the DiskBasedCache at the specified directory.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears the cache.
|
Cache.Entry |
get(java.lang.String key)
Returns the cache entry with the specified key if it exists, null otherwise.
|
java.io.File |
getFileForKey(java.lang.String key)
Returns a file object for the given cache key.
|
private java.lang.String |
getFilenameForKey(java.lang.String key)
Creates a pseudo-unique filename for the specified cache key.
|
void |
initialize()
Initializes the DiskBasedCache by scanning for all files currently in the
specified root directory.
|
void |
invalidate(java.lang.String key,
boolean fullExpire)
Invalidates an entry in the cache.
|
private void |
pruneIfNeeded(int neededSpace)
Prunes the cache to fit the amount of bytes specified.
|
void |
put(java.lang.String key,
Cache.Entry entry)
Puts the entry with the specified key into the cache.
|
private void |
putEntry(java.lang.String key,
DiskBasedCache.CacheHeader entry)
Puts the entry with the specified key into the cache.
|
private static int |
read(java.io.InputStream is)
Simple wrapper around
InputStream.read() that throws EOFException
instead of returning -1. |
(package private) static int |
readInt(java.io.InputStream is) |
(package private) static long |
readLong(java.io.InputStream is) |
(package private) static java.lang.String |
readString(java.io.InputStream is) |
(package private) static java.util.Map<java.lang.String,java.lang.String> |
readStringStringMap(java.io.InputStream is) |
void |
remove(java.lang.String key)
Removes the specified key from the cache if it exists.
|
private void |
removeEntry(java.lang.String key)
Removes the entry identified by 'key' from the cache.
|
private static byte[] |
streamToBytes(java.io.InputStream in,
int length)
Reads the contents of an InputStream into a byte[].
|
(package private) static void |
writeInt(java.io.OutputStream os,
int n) |
(package private) static void |
writeLong(java.io.OutputStream os,
long n) |
(package private) static void |
writeString(java.io.OutputStream os,
java.lang.String s) |
(package private) static void |
writeStringStringMap(java.util.Map<java.lang.String,java.lang.String> map,
java.io.OutputStream os) |
private final java.util.Map<java.lang.String,DiskBasedCache.CacheHeader> mEntries
private long mTotalSize
private final java.io.File mRootDirectory
private final int mMaxCacheSizeInBytes
private static final int DEFAULT_DISK_USAGE_BYTES
private static final float HYSTERESIS_FACTOR
private static final int CACHE_MAGIC
public DiskBasedCache(java.io.File rootDirectory, int maxCacheSizeInBytes)
rootDirectory
- The root directory of the cache.maxCacheSizeInBytes
- The maximum size of the cache in bytes.public DiskBasedCache(java.io.File rootDirectory)
rootDirectory
- The root directory of the cache.public void clear()
public Cache.Entry get(java.lang.String key)
get
in interface Cache
key
- Cache keyCache.Entry
or null in the event of a cache misspublic void initialize()
initialize
in interface Cache
public void invalidate(java.lang.String key, boolean fullExpire)
invalidate
in interface Cache
key
- Cache keyfullExpire
- True to fully expire the entry, false to soft expirepublic void put(java.lang.String key, Cache.Entry entry)
public void remove(java.lang.String key)
private java.lang.String getFilenameForKey(java.lang.String key)
key
- The key to generate a file name for.public java.io.File getFileForKey(java.lang.String key)
private void pruneIfNeeded(int neededSpace)
neededSpace
- The amount of bytes we are trying to fit into the cache.private void putEntry(java.lang.String key, DiskBasedCache.CacheHeader entry)
key
- The key to identify the entry by.entry
- The entry to cache.private void removeEntry(java.lang.String key)
private static byte[] streamToBytes(java.io.InputStream in, int length) throws java.io.IOException
java.io.IOException
private static int read(java.io.InputStream is) throws java.io.IOException
InputStream.read()
that throws EOFException
instead of returning -1.java.io.IOException
static void writeInt(java.io.OutputStream os, int n) throws java.io.IOException
java.io.IOException
static int readInt(java.io.InputStream is) throws java.io.IOException
java.io.IOException
static void writeLong(java.io.OutputStream os, long n) throws java.io.IOException
java.io.IOException
static long readLong(java.io.InputStream is) throws java.io.IOException
java.io.IOException
static void writeString(java.io.OutputStream os, java.lang.String s) throws java.io.IOException
java.io.IOException
static java.lang.String readString(java.io.InputStream is) throws java.io.IOException
java.io.IOException
static void writeStringStringMap(java.util.Map<java.lang.String,java.lang.String> map, java.io.OutputStream os) throws java.io.IOException
java.io.IOException
static java.util.Map<java.lang.String,java.lang.String> readStringStringMap(java.io.InputStream is) throws java.io.IOException
java.io.IOException