com.duowan.mobile.netroid.cache
Class DiskCache

java.lang.Object
  extended by com.duowan.mobile.netroid.cache.DiskCache

public class DiskCache
extends java.lang.Object

LruCache implementation that caches files directly onto the hard disk in the specified directory. The default disk usage size is 5MB, but is configurable.


Nested Class Summary
(package private) static class DiskCache.CacheHeader
          Handles holding onto the cache headers for an entry.
static class DiskCache.Entry
          Data and metadata for an entry returned by the cache.
 
Constructor Summary
DiskCache(java.io.File rootDirectory)
          Constructs an instance of the DiskCache at the specified directory using the default maximum cache size of 5MB.
DiskCache(java.io.File rootDirectory, int maxCacheSizeInBytes)
          Constructs an instance of the DiskCache at the specified directory.
 
Method Summary
 void clearCache()
          Clears the cache.
 DiskCache.Entry getEntry(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.
 void initialize()
          Initializes the DiskCache by scanning for all files currently in the specified root directory.
 void invalidate(java.lang.String key, long expireTime)
          Invalidates an entry in the cache.
 void putEntry(java.lang.String key, DiskCache.Entry entry)
          Puts the entry with the specified key into the cache.
(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 removeEntry(java.lang.String key)
          Removes the specified key from the cache if it exists.
(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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DiskCache

public DiskCache(java.io.File rootDirectory,
                 int maxCacheSizeInBytes)
Constructs an instance of the DiskCache at the specified directory.

Parameters:
rootDirectory - The root directory of the cache.
maxCacheSizeInBytes - The maximum size of the cache in bytes.

DiskCache

public DiskCache(java.io.File rootDirectory)
Constructs an instance of the DiskCache at the specified directory using the default maximum cache size of 5MB.

Parameters:
rootDirectory - The root directory of the cache.
Method Detail

clearCache

public void clearCache()
Clears the cache. Deletes all cached files from disk.


getEntry

public DiskCache.Entry getEntry(java.lang.String key)
Returns the cache entry with the specified key if it exists, null otherwise.


initialize

public void initialize()
Initializes the DiskCache by scanning for all files currently in the specified root directory. Creates the root directory if necessary.


invalidate

public void invalidate(java.lang.String key,
                       long expireTime)
Invalidates an entry in the cache.

Parameters:
key - Cache key
expireTime - The new expireTime

putEntry

public void putEntry(java.lang.String key,
                     DiskCache.Entry entry)
Puts the entry with the specified key into the cache.


removeEntry

public void removeEntry(java.lang.String key)
Removes the specified key from the cache if it exists.


getFileForKey

public java.io.File getFileForKey(java.lang.String key)
Returns a file object for the given cache key.


writeInt

static void writeInt(java.io.OutputStream os,
                     int n)
              throws java.io.IOException
Throws:
java.io.IOException

readInt

static int readInt(java.io.InputStream is)
            throws java.io.IOException
Throws:
java.io.IOException

writeLong

static void writeLong(java.io.OutputStream os,
                      long n)
               throws java.io.IOException
Throws:
java.io.IOException

readLong

static long readLong(java.io.InputStream is)
              throws java.io.IOException
Throws:
java.io.IOException

writeString

static void writeString(java.io.OutputStream os,
                        java.lang.String s)
                 throws java.io.IOException
Throws:
java.io.IOException

readString

static java.lang.String readString(java.io.InputStream is)
                            throws java.io.IOException
Throws:
java.io.IOException

writeStringStringMap

static void writeStringStringMap(java.util.Map<java.lang.String,java.lang.String> map,
                                 java.io.OutputStream os)
                          throws java.io.IOException
Throws:
java.io.IOException

readStringStringMap

static java.util.Map<java.lang.String,java.lang.String> readStringStringMap(java.io.InputStream is)
                                                                     throws java.io.IOException
Throws:
java.io.IOException