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 androidx.room.Dao;
20 import androidx.room.Insert;
21 import androidx.room.OnConflictStrategy;
22 import androidx.room.Query;
23 
24 import java.time.Instant;
25 
26 /** A Dao for handling the queries related to {@link CacheDatabase} */
27 @Dao
28 public interface CacheEntryDao {
29 
30     /**
31      * @param cacheEntry an entry that needs to cached
32      * @return the count of entries persisted, ideally 1 if succeeded
33      */
34     @Insert(onConflict = OnConflictStrategy.REPLACE)
persistCacheEntry(DBCacheEntry cacheEntry)35     long persistCacheEntry(DBCacheEntry cacheEntry);
36 
37     /**
38      * Should only return entries that are fresh
39      *
40      * @param url which was use do cache an entry in the persistence layer
41      * @param curTime the current clock time
42      * @return the cached entry corresponding to the url key
43      */
44     @Query(
45             "SELECT http_cache.cache_url AS cache_url, http_cache.response_body as response_body,"
46                     + " http_cache.response_headers as response_headers, http_cache"
47                     + ".creation_timestamp as creation_timestamp, http_cache.max_age as"
48                     + " max_age FROM http_cache WHERE (max_age * 1000) + creation_timestamp > "
49                     + " :curTime AND cache_url = :url")
getCacheEntry(String url, Instant curTime)50     DBCacheEntry getCacheEntry(String url, Instant curTime);
51 
52     /**
53      * Deletes the rows in the persistence layer which are expired or are older than the cache's
54      * max-age.
55      *
56      * @param defaultMaxAgeSeconds cache enforced max age for which entries should be considered
57      *     fresh
58      * @param curTime the current clock time
59      */
60     @Query(
61             "DELETE FROM http_cache WHERE (max_age * 1000) + creation_timestamp < :curTime"
62                     + " OR (:defaultMaxAgeSeconds * 1000) + creation_timestamp < :curTime")
deleteStaleRows(long defaultMaxAgeSeconds, Instant curTime)63     void deleteStaleRows(long defaultMaxAgeSeconds, Instant curTime);
64 
65     /** @return num of entries in the DB */
66     @Query("SELECT COUNT(cache_url) FROM http_cache")
getDBEntriesCount()67     long getDBEntriesCount();
68 
69     /** Deletes all the entries from cache */
70     @Query("DELETE FROM http_cache")
deleteAll()71     void deleteAll();
72 
73     /**
74      * Prunes the cache so that it does not increase beyond a max permissible size. Uses FIFO
75      * strategy to delete the oldest records first.
76      *
77      * @param maxCacheEntries max allowed size of cache, eventually imposed after pruning
78      */
79     @Query(
80             "DELETE FROM http_cache WHERE cache_url IN (SELECT cache_url FROM http_cache ORDER BY"
81                     + " creation_timestamp DESC LIMIT -1 OFFSET :maxCacheEntries)")
prune(long maxCacheEntries)82     void prune(long maxCacheEntries);
83 }
84