1 /* 2 * Copyright (C) 2022 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 17 package com.android.adservices.service.common.cache; 18 19 import java.net.URL; 20 import java.util.List; 21 import java.util.Map; 22 23 /** Interface for generic cache */ 24 public interface HttpCache { 25 26 /** Gets a cached entry */ get(URL url)27 DBCacheEntry get(URL url); 28 29 /** 30 * Saves a cache entry 31 * 32 * @param url for which the request was made 33 * @param body response for the url 34 * @param requestPropertiesMap original connection's properties 35 */ put( URL url, String body, Map<String, List<String>> requestPropertiesMap, Map<String, List<String>> responseHeaders)36 void put( 37 URL url, 38 String body, 39 Map<String, List<String>> requestPropertiesMap, 40 Map<String, List<String>> responseHeaders); 41 42 /** @return no of entries cached */ getCachedEntriesCount()43 long getCachedEntriesCount(); 44 45 /** @return no of entries taken from cache, saving network call */ getHitCount()46 long getHitCount(); 47 48 /** @return no of cache look-ups attempts made */ getRequestCount()49 long getRequestCount(); 50 51 /** Delete the cache */ delete()52 void delete(); 53 54 /** Clean up the cache */ cleanUp()55 void cleanUp(); 56 57 /** Possible observable events for Cache */ 58 enum CacheEventType { 59 GET, 60 PUT, 61 DELETE, 62 CLEANUP 63 } 64 65 /** 66 * Add observers to observe this cache's events 67 * 68 * @param observer that gets updated on events 69 */ addObserver(CacheObserver observer)70 void addObserver(CacheObserver observer); 71 72 /** 73 * Notify all the registered observers 74 * 75 * @param cacheEvent type of caching event 76 */ notifyObservers(CacheEventType cacheEvent)77 void notifyObservers(CacheEventType cacheEvent); 78 79 /** An observer that can help get updates from inside the cache events */ 80 interface CacheObserver { update(CacheEventType cacheEvent)81 void update(CacheEventType cacheEvent); 82 } 83 } 84