DiskBasedCache.java revision 9c19fc62ddd9d1b371cb3ead4e10bb5ff1100a6c
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.volley.toolbox; 18 19import android.os.SystemClock; 20 21import com.android.volley.Cache; 22import com.android.volley.VolleyLog; 23 24import java.io.ByteArrayOutputStream; 25import java.io.File; 26import java.io.FileInputStream; 27import java.io.FileOutputStream; 28import java.io.IOException; 29import java.io.InputStream; 30import java.io.ObjectInputStream; 31import java.io.ObjectOutputStream; 32import java.io.OutputStream; 33import java.util.Iterator; 34import java.util.LinkedHashMap; 35import java.util.Map; 36 37/** 38 * Cache implementation that caches files directly onto the hard disk in the specified 39 * directory. The default disk usage size is 5MB, but is configurable. 40 */ 41public class DiskBasedCache implements Cache { 42 43 /** Map of the Key, CacheHeader pairs */ 44 private final Map<String, CacheHeader> mEntries = 45 new LinkedHashMap<String, CacheHeader>(16, .75f, true); 46 47 /** Total amount of space currently used by the cache in bytes. */ 48 private long mTotalSize = 0; 49 50 /** The root directory to use for the cache. */ 51 private final File mRootDirectory; 52 53 /** The maximum size of the cache in bytes. */ 54 private final int mMaxCacheSizeInBytes; 55 56 /** Default maximum disk usage in bytes. */ 57 private static final int DEFAULT_DISK_USAGE_BYTES = 5 * 1024 * 1024; 58 59 /** High water mark percentage for the cache */ 60 private static final float HYSTERESIS_FACTOR = 0.9f; 61 62 /** Current cache version */ 63 private static final int CACHE_VERSION = 1; 64 65 /** 66 * Constructs an instance of the DiskBasedCache at the specified directory. 67 * @param rootDirectory The root directory of the cache. 68 * @param maxCacheSizeInBytes The maximum size of the cache in bytes. 69 */ 70 public DiskBasedCache(File rootDirectory, int maxCacheSizeInBytes) { 71 mRootDirectory = rootDirectory; 72 mMaxCacheSizeInBytes = maxCacheSizeInBytes; 73 } 74 75 /** 76 * Constructs an instance of the DiskBasedCache at the specified directory using 77 * the default maximum cache size of 5MB. 78 * @param rootDirectory The root directory of the cache. 79 */ 80 public DiskBasedCache(File rootDirectory) { 81 this(rootDirectory, DEFAULT_DISK_USAGE_BYTES); 82 } 83 84 /** 85 * Clears the cache. Deletes all cached files from disk. 86 */ 87 @Override 88 public synchronized void clear() { 89 File[] files = mRootDirectory.listFiles(); 90 if (files != null) { 91 for (File file : files) { 92 file.delete(); 93 } 94 } 95 mEntries.clear(); 96 mTotalSize = 0; 97 VolleyLog.d("Cache cleared."); 98 } 99 100 /** 101 * Returns the cache entry with the specified key if it exists, null otherwise. 102 */ 103 @Override 104 public synchronized Entry get(String key) { 105 CacheHeader entry = mEntries.get(key); 106 // if the entry does not exist, return. 107 if (entry == null) { 108 return null; 109 } 110 111 File file = getFileForKey(key); 112 FileInputStream fis = null; 113 try { 114 fis = new FileInputStream(file); 115 CacheHeader.readHeader(fis); // eat header 116 byte[] data = streamToBytes(fis); 117 return entry.toCacheEntry(data); 118 } catch (IOException e) { 119 VolleyLog.d("%s: %s", file.getAbsolutePath(), e.toString()); 120 remove(key); 121 return null; 122 } finally { 123 if (fis != null) { 124 try { 125 fis.close(); 126 } catch (IOException ioe) { 127 return null; 128 } 129 } 130 } 131 } 132 133 /** 134 * Initializes the DiskBasedCache by scanning for all files currently in the 135 * specified root directory. 136 */ 137 @Override 138 public synchronized void initialize() { 139 File[] files = mRootDirectory.listFiles(); 140 if (files == null) { 141 return; 142 } 143 for (File file : files) { 144 FileInputStream fis = null; 145 try { 146 fis = new FileInputStream(file); 147 CacheHeader entry = CacheHeader.readHeader(fis); 148 entry.size = file.length(); 149 putEntry(entry.key, entry); 150 } catch (IOException e) { 151 if (file != null) { 152 file.delete(); 153 } 154 } finally { 155 try { 156 if (fis != null) { 157 fis.close(); 158 } 159 } catch (IOException ignored) { } 160 } 161 } 162 } 163 164 /** 165 * Invalidates an entry in the cache. 166 * @param key Cache key 167 * @param fullExpire True to fully expire the entry, false to soft expire 168 */ 169 @Override 170 public synchronized void invalidate(String key, boolean fullExpire) { 171 Entry entry = get(key); 172 if (entry != null) { 173 entry.softTtl = 0; 174 if (fullExpire) { 175 entry.ttl = 0; 176 } 177 put(key, entry); 178 } 179 180 } 181 182 /** 183 * Puts the entry with the specified key into the cache. 184 */ 185 @Override 186 public synchronized void put(String key, Entry entry) { 187 pruneIfNeeded(entry.data.length); 188 File file = getFileForKey(key); 189 try { 190 FileOutputStream fos = new FileOutputStream(file); 191 CacheHeader e = new CacheHeader(key, entry); 192 e.writeHeader(fos); 193 fos.write(entry.data); 194 fos.close(); 195 putEntry(key, e); 196 return; 197 } catch (IOException e) { 198 } 199 boolean deleted = file.delete(); 200 if (!deleted) { 201 VolleyLog.d("Could not clean up file %s", file.getAbsolutePath()); 202 } 203 } 204 205 /** 206 * Removes the specified key from the cache if it exists. 207 */ 208 @Override 209 public synchronized void remove(String key) { 210 boolean deleted = getFileForKey(key).delete(); 211 removeEntry(key); 212 if (!deleted) { 213 VolleyLog.d("Could not delete cache entry for key=%s, filename=%s", 214 key, getFilenameForKey(key)); 215 } 216 } 217 218 /** 219 * Creates a pseudo-unique filename for the specified cache key. 220 * @param key The key to generate a file name for. 221 * @return A pseudo-unique filename. 222 */ 223 private String getFilenameForKey(String key) { 224 int firstHalfLength = key.length() / 2; 225 String localFilename = String.valueOf(key.substring(0, firstHalfLength).hashCode()); 226 localFilename += String.valueOf(key.substring(firstHalfLength).hashCode()); 227 return localFilename; 228 } 229 230 /** 231 * Returns a file object for the given cache key. 232 */ 233 private File getFileForKey(String key) { 234 return new File(mRootDirectory, getFilenameForKey(key)); 235 } 236 237 /** 238 * Prunes the cache to fit the amount of bytes specified. 239 * @param neededSpace The amount of bytes we are trying to fit into the cache. 240 */ 241 private void pruneIfNeeded(int neededSpace) { 242 if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) { 243 return; 244 } 245 if (VolleyLog.DEBUG) { 246 VolleyLog.v("Pruning old cache entries."); 247 } 248 249 long before = mTotalSize; 250 int prunedFiles = 0; 251 long startTime = SystemClock.elapsedRealtime(); 252 253 Iterator<Map.Entry<String, CacheHeader>> iterator = mEntries.entrySet().iterator(); 254 while (iterator.hasNext()) { 255 Map.Entry<String, CacheHeader> entry = iterator.next(); 256 CacheHeader e = entry.getValue(); 257 boolean deleted = getFileForKey(e.key).delete(); 258 if (deleted) { 259 mTotalSize -= e.size; 260 } else { 261 VolleyLog.d("Could not delete cache entry for key=%s, filename=%s", 262 e.key, getFilenameForKey(e.key)); 263 } 264 iterator.remove(); 265 prunedFiles++; 266 267 if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) { 268 break; 269 } 270 } 271 272 if (VolleyLog.DEBUG) { 273 VolleyLog.v("pruned %d files, %d bytes, %d ms", 274 prunedFiles, (mTotalSize - before), SystemClock.elapsedRealtime() - startTime); 275 } 276 } 277 278 /** 279 * Puts the entry with the specified key into the cache. 280 * @param key The key to identify the entry by. 281 * @param entry The entry to cache. 282 */ 283 private void putEntry(String key, CacheHeader entry) { 284 if (!mEntries.containsKey(key)) { 285 mTotalSize += entry.size; 286 } else { 287 CacheHeader oldEntry = mEntries.get(key); 288 mTotalSize += (entry.size - oldEntry.size); 289 } 290 mEntries.put(key, entry); 291 } 292 293 /** 294 * Removes the entry identified by 'key' from the cache. 295 */ 296 private void removeEntry(String key) { 297 CacheHeader entry = mEntries.get(key); 298 if (entry != null) { 299 mTotalSize -= entry.size; 300 mEntries.remove(key); 301 } 302 } 303 304 /** 305 * Reads the contents of an InputStream into a byte[]. 306 * */ 307 private static byte[] streamToBytes(InputStream in) throws IOException { 308 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 309 byte[] buffer = new byte[1024]; 310 int count; 311 while ((count = in.read(buffer)) != -1) { 312 bytes.write(buffer, 0, count); 313 } 314 byte[] output = bytes.toByteArray(); 315 bytes.close(); 316 return output; 317 } 318 319 /** 320 * Handles holding onto the cache headers for an entry. 321 */ 322 private static class CacheHeader { 323 /** The size of the data identified by this CacheHeader. (This is not 324 * serialized to disk. */ 325 public long size; 326 327 /** The key that identifies the cache entry. */ 328 public String key; 329 330 /** ETag for cache coherence. */ 331 public String etag; 332 333 /** Date of this response as reported by the server. */ 334 public long serverDate; 335 336 /** TTL for this record. */ 337 public long ttl; 338 339 /** Soft TTL for this record. */ 340 public long softTtl; 341 342 private CacheHeader() { } 343 344 /** 345 * Instantiates a new CacheHeader object 346 * @param key The key that identifies the cache entry 347 * @param entry The cache entry. 348 */ 349 public CacheHeader(String key, Entry entry) { 350 this.key = key; 351 this.size = entry.data.length; 352 this.etag = entry.etag; 353 this.serverDate = entry.serverDate; 354 this.ttl = entry.ttl; 355 this.softTtl = entry.softTtl; 356 } 357 358 /** 359 * Reads the header off of an InputStream and returns a CacheHeader object. 360 * @param is The InputStream to read from. 361 * @throws IOException 362 */ 363 public static CacheHeader readHeader(InputStream is) throws IOException { 364 CacheHeader entry = new CacheHeader(); 365 ObjectInputStream ois = new ObjectInputStream(is); 366 int version = ois.readByte(); 367 if (version != CACHE_VERSION) { 368 // don't bother deleting, it'll get pruned eventually 369 throw new IOException(); 370 } 371 entry.key = ois.readUTF(); 372 entry.etag = ois.readUTF(); 373 if (entry.etag.equals("")) { 374 entry.etag = null; 375 } 376 entry.serverDate = ois.readLong(); 377 entry.ttl = ois.readLong(); 378 entry.softTtl = ois.readLong(); 379 return entry; 380 } 381 382 /** 383 * Creates a cache entry for the specified data. 384 */ 385 public Entry toCacheEntry(byte[] data) { 386 Entry e = new Entry(); 387 e.data = data; 388 e.etag = etag; 389 e.serverDate = serverDate; 390 e.ttl = ttl; 391 e.softTtl = softTtl; 392 return e; 393 } 394 395 /** 396 * Writes the contents of this CacheHeader to the specified OutputStream. 397 */ 398 public boolean writeHeader(OutputStream os) { 399 try { 400 ObjectOutputStream oos = new ObjectOutputStream(os); 401 oos.writeByte(CACHE_VERSION); 402 oos.writeUTF(key); 403 oos.writeUTF(etag == null ? "" : etag); 404 oos.writeLong(serverDate); 405 oos.writeLong(ttl); 406 oos.writeLong(softTtl); 407 oos.flush(); 408 return true; 409 } catch (IOException e) { 410 VolleyLog.d("%s", e.toString()); 411 return false; 412 } 413 } 414 } 415} 416