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; 18 19/** 20 * An interface for a cache keyed by a String with a byte array as data. 21 */ 22public interface Cache { 23 /** 24 * Retrieves an entry from the cache. 25 * @param key Cache key 26 * @return An {@link Entry} or null in the event of a cache miss 27 */ 28 public Entry get(String key); 29 30 /** 31 * Adds or replaces an entry to the cache. 32 * @param key Cache key 33 * @param entry Data to store and metadata for cache coherency, TTL, etc. 34 */ 35 public void put(String key, Entry entry); 36 37 /** 38 * Performs any potentially long-running actions needed to initialize the cache; 39 * will be called from a worker thread. 40 */ 41 public void initialize(); 42 43 /** 44 * Invalidates an entry in the cache. 45 * @param key Cache key 46 * @param fullExpire True to fully expire the entry, false to soft expire 47 */ 48 public void invalidate(String key, boolean fullExpire); 49 50 /** 51 * Removes an entry from the cache. 52 * @param key Cache key 53 */ 54 public void remove(String key); 55 56 /** 57 * Empties the cache. 58 */ 59 public void clear(); 60 61 /** 62 * Data and metadata for an entry returned by the cache. 63 */ 64 public static class Entry { 65 /** The data returned from cache. */ 66 public byte[] data; 67 68 /** ETag for cache coherency. */ 69 public String etag; 70 71 /** Date of this response as reported by the server. */ 72 public long serverDate; 73 74 /** TTL for this record. */ 75 public long ttl; 76 77 /** Soft TTL for this record. */ 78 public long softTtl; 79 80 /** True if the entry is expired. */ 81 public boolean isExpired() { 82 return this.ttl < System.currentTimeMillis(); 83 } 84 85 /** True if a refresh is needed from the original data source. */ 86 public boolean refreshNeeded() { 87 return this.softTtl < System.currentTimeMillis(); 88 } 89 } 90 91} 92