aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/java/com/android/volley/AsyncCache.java
blob: 8b2dbccc872af9b3976b12ce5915edc5fcb56ef3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.volley;

import androidx.annotation.Nullable;

/**
 * Asynchronous equivalent to the {@link Cache} interface.
 *
 * <p><b>WARNING</b>: This API is experimental and subject to breaking changes. Please see
 * https://github.com/google/volley/wiki/Asynchronous-Volley for more details.
 */
public abstract class AsyncCache {

    public interface OnGetCompleteCallback {
        /**
         * Invoked when the read from the cache is complete.
         *
         * @param entry The entry read from the cache, or null if the read failed or the key did not
         *     exist in the cache.
         */
        void onGetComplete(@Nullable Cache.Entry entry);
    }

    /**
     * Retrieves an entry from the cache and sends it back through the {@link
     * OnGetCompleteCallback#onGetComplete} function
     *
     * @param key Cache key
     * @param callback Callback that will be notified when the information has been retrieved
     */
    public abstract void get(String key, OnGetCompleteCallback callback);

    public interface OnWriteCompleteCallback {
        /** Invoked when the cache operation is complete */
        void onWriteComplete();
    }

    /**
     * Writes a {@link Cache.Entry} to the cache, and calls {@link
     * OnWriteCompleteCallback#onWriteComplete} after the operation is finished.
     *
     * @param key Cache key
     * @param entry The entry to be written to the cache
     * @param callback Callback that will be notified when the information has been written
     */
    public abstract void put(String key, Cache.Entry entry, OnWriteCompleteCallback callback);

    /**
     * Clears the cache. Deletes all cached files from disk. Calls {@link
     * OnWriteCompleteCallback#onWriteComplete} after the operation is finished.
     */
    public abstract void clear(OnWriteCompleteCallback callback);

    /**
     * Initializes the cache and calls {@link OnWriteCompleteCallback#onWriteComplete} after the
     * operation is finished.
     */
    public abstract void initialize(OnWriteCompleteCallback callback);

    /**
     * Invalidates an entry in the cache and calls {@link OnWriteCompleteCallback#onWriteComplete}
     * after the operation is finished.
     *
     * @param key Cache key
     * @param fullExpire True to fully expire the entry, false to soft expire
     * @param callback Callback that's invoked once the entry has been invalidated
     */
    public abstract void invalidate(
            String key, boolean fullExpire, OnWriteCompleteCallback callback);

    /**
     * Removes a {@link Cache.Entry} from the cache, and calls {@link
     * OnWriteCompleteCallback#onWriteComplete} after the operation is finished.
     *
     * @param key Cache key
     * @param callback Callback that's invoked once the entry has been removed
     */
    public abstract void remove(String key, OnWriteCompleteCallback callback);
}