Skip to content

Commit 04bdc1c

Browse files
committed
Base cache key on info type instead of item type
It didn't really made sense to consider two cache keys as equal based on the type of items contained within that list.
1 parent 1d8850d commit 04bdc1c

3 files changed

Lines changed: 68 additions & 40 deletions

File tree

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
import org.schabi.newpipe.error.ReCaptchaActivity;
7373
import org.schabi.newpipe.error.UserAction;
7474
import org.schabi.newpipe.extractor.Image;
75-
import org.schabi.newpipe.extractor.InfoItem;
7675
import org.schabi.newpipe.extractor.NewPipe;
7776
import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
7877
import org.schabi.newpipe.extractor.exceptions.ContentNotSupportedException;
@@ -107,16 +106,17 @@
107106
import org.schabi.newpipe.util.Constants;
108107
import org.schabi.newpipe.util.DeviceUtils;
109108
import org.schabi.newpipe.util.ExtractorHelper;
109+
import org.schabi.newpipe.util.InfoCache;
110110
import org.schabi.newpipe.util.ListHelper;
111111
import org.schabi.newpipe.util.Localization;
112112
import org.schabi.newpipe.util.NavigationHelper;
113113
import org.schabi.newpipe.util.PermissionHelper;
114-
import org.schabi.newpipe.util.image.PicassoHelper;
114+
import org.schabi.newpipe.util.PlayButtonHelper;
115115
import org.schabi.newpipe.util.StreamTypeUtil;
116116
import org.schabi.newpipe.util.ThemeHelper;
117117
import org.schabi.newpipe.util.external_communication.KoreUtils;
118118
import org.schabi.newpipe.util.external_communication.ShareUtils;
119-
import org.schabi.newpipe.util.PlayButtonHelper;
119+
import org.schabi.newpipe.util.image.PicassoHelper;
120120

121121
import java.util.ArrayList;
122122
import java.util.Iterator;
@@ -1445,7 +1445,7 @@ public void showLoading() {
14451445
super.showLoading();
14461446

14471447
//if data is already cached, transition from VISIBLE -> INVISIBLE -> VISIBLE is not required
1448-
if (!ExtractorHelper.isCached(serviceId, url, InfoItem.InfoType.STREAM)) {
1448+
if (!ExtractorHelper.isCached(serviceId, url, InfoCache.Type.STREAM)) {
14491449
binding.detailContentRootHiding.setVisibility(View.INVISIBLE);
14501450
}
14511451

app/src/main/java/org/schabi/newpipe/util/ExtractorHelper.java

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import android.view.View;
2828
import android.widget.TextView;
2929

30+
import androidx.annotation.NonNull;
3031
import androidx.annotation.Nullable;
3132
import androidx.core.text.HtmlCompat;
3233
import androidx.preference.PreferenceManager;
@@ -113,14 +114,14 @@ public static Single<List<String>> suggestionsFor(final int serviceId, final Str
113114
public static Single<StreamInfo> getStreamInfo(final int serviceId, final String url,
114115
final boolean forceLoad) {
115116
checkServiceId(serviceId);
116-
return checkCache(forceLoad, serviceId, url, InfoItem.InfoType.STREAM,
117+
return checkCache(forceLoad, serviceId, url, InfoCache.Type.STREAM,
117118
Single.fromCallable(() -> StreamInfo.getInfo(NewPipe.getService(serviceId), url)));
118119
}
119120

120121
public static Single<ChannelInfo> getChannelInfo(final int serviceId, final String url,
121122
final boolean forceLoad) {
122123
checkServiceId(serviceId);
123-
return checkCache(forceLoad, serviceId, url, InfoItem.InfoType.CHANNEL,
124+
return checkCache(forceLoad, serviceId, url, InfoCache.Type.CHANNEL,
124125
Single.fromCallable(() ->
125126
ChannelInfo.getInfo(NewPipe.getService(serviceId), url)));
126127
}
@@ -130,7 +131,7 @@ public static Single<ChannelTabInfo> getChannelTab(final int serviceId,
130131
final boolean forceLoad) {
131132
checkServiceId(serviceId);
132133
return checkCache(forceLoad, serviceId,
133-
listLinkHandler.getUrl(), InfoItem.InfoType.CHANNEL,
134+
listLinkHandler.getUrl(), InfoCache.Type.CHANNEL_TAB,
134135
Single.fromCallable(() ->
135136
ChannelTabInfo.getInfo(NewPipe.getService(serviceId), listLinkHandler)));
136137
}
@@ -145,10 +146,11 @@ public static Single<InfoItemsPage<InfoItem>> getMoreChannelTabItems(
145146
listLinkHandler, nextPage));
146147
}
147148

148-
public static Single<CommentsInfo> getCommentsInfo(final int serviceId, final String url,
149+
public static Single<CommentsInfo> getCommentsInfo(final int serviceId,
150+
final String url,
149151
final boolean forceLoad) {
150152
checkServiceId(serviceId);
151-
return checkCache(forceLoad, serviceId, url, InfoItem.InfoType.COMMENT,
153+
return checkCache(forceLoad, serviceId, url, InfoCache.Type.COMMENTS,
152154
Single.fromCallable(() ->
153155
CommentsInfo.getInfo(NewPipe.getService(serviceId), url)));
154156
}
@@ -175,7 +177,7 @@ public static Single<PlaylistInfo> getPlaylistInfo(final int serviceId,
175177
final String url,
176178
final boolean forceLoad) {
177179
checkServiceId(serviceId);
178-
return checkCache(forceLoad, serviceId, url, InfoItem.InfoType.PLAYLIST,
180+
return checkCache(forceLoad, serviceId, url, InfoCache.Type.PLAYLIST,
179181
Single.fromCallable(() ->
180182
PlaylistInfo.getInfo(NewPipe.getService(serviceId), url)));
181183
}
@@ -188,9 +190,10 @@ public static Single<InfoItemsPage<StreamInfoItem>> getMorePlaylistItems(final i
188190
PlaylistInfo.getMoreItems(NewPipe.getService(serviceId), url, nextPage));
189191
}
190192

191-
public static Single<KioskInfo> getKioskInfo(final int serviceId, final String url,
193+
public static Single<KioskInfo> getKioskInfo(final int serviceId,
194+
final String url,
192195
final boolean forceLoad) {
193-
return checkCache(forceLoad, serviceId, url, InfoItem.InfoType.PLAYLIST,
196+
return checkCache(forceLoad, serviceId, url, InfoCache.Type.KIOSK,
194197
Single.fromCallable(() -> KioskInfo.getInfo(NewPipe.getService(serviceId), url)));
195198
}
196199

@@ -202,7 +205,7 @@ public static Single<InfoItemsPage<StreamInfoItem>> getMoreKioskItems(final int
202205
}
203206

204207
/*//////////////////////////////////////////////////////////////////////////
205-
// Utils
208+
// Cache
206209
//////////////////////////////////////////////////////////////////////////*/
207210

208211
/**
@@ -214,24 +217,25 @@ public static Single<InfoItemsPage<StreamInfoItem>> getMoreKioskItems(final int
214217
* @param forceLoad whether to force loading from the network instead of from the cache
215218
* @param serviceId the service to load from
216219
* @param url the URL to load
217-
* @param infoType the {@link InfoItem.InfoType} of the item
220+
* @param cacheType the {@link InfoCache.Type} of the item
218221
* @param loadFromNetwork the {@link Single} to load the item from the network
219222
* @return a {@link Single} that loads the item
220223
*/
221224
private static <I extends Info> Single<I> checkCache(final boolean forceLoad,
222-
final int serviceId, final String url,
223-
final InfoItem.InfoType infoType,
224-
final Single<I> loadFromNetwork) {
225+
final int serviceId,
226+
@NonNull final String url,
227+
@NonNull final InfoCache.Type cacheType,
228+
@NonNull final Single<I> loadFromNetwork) {
225229
checkServiceId(serviceId);
226230
final Single<I> actualLoadFromNetwork = loadFromNetwork
227-
.doOnSuccess(info -> CACHE.putInfo(serviceId, url, info, infoType));
231+
.doOnSuccess(info -> CACHE.putInfo(serviceId, url, info, cacheType));
228232

229233
final Single<I> load;
230234
if (forceLoad) {
231-
CACHE.removeInfo(serviceId, url, infoType);
235+
CACHE.removeInfo(serviceId, url, cacheType);
232236
load = actualLoadFromNetwork;
233237
} else {
234-
load = Maybe.concat(ExtractorHelper.loadFromCache(serviceId, url, infoType),
238+
load = Maybe.concat(ExtractorHelper.loadFromCache(serviceId, url, cacheType),
235239
actualLoadFromNetwork.toMaybe())
236240
.firstElement() // Take the first valid
237241
.toSingle();
@@ -246,15 +250,17 @@ private static <I extends Info> Single<I> checkCache(final boolean forceLoad,
246250
* @param <I> the item type's class that extends {@link Info}
247251
* @param serviceId the service to load from
248252
* @param url the URL to load
249-
* @param infoType the {@link InfoItem.InfoType} of the item
253+
* @param cacheType the {@link InfoCache.Type} of the item
250254
* @return a {@link Single} that loads the item
251255
*/
252-
private static <I extends Info> Maybe<I> loadFromCache(final int serviceId, final String url,
253-
final InfoItem.InfoType infoType) {
256+
private static <I extends Info> Maybe<I> loadFromCache(
257+
final int serviceId,
258+
@NonNull final String url,
259+
@NonNull final InfoCache.Type cacheType) {
254260
checkServiceId(serviceId);
255261
return Maybe.defer(() -> {
256262
//noinspection unchecked
257-
final I info = (I) CACHE.getFromKey(serviceId, url, infoType);
263+
final I info = (I) CACHE.getFromKey(serviceId, url, cacheType);
258264
if (MainActivity.DEBUG) {
259265
Log.d(TAG, "loadFromCache() called, info > " + info);
260266
}
@@ -268,11 +274,17 @@ private static <I extends Info> Maybe<I> loadFromCache(final int serviceId, fina
268274
});
269275
}
270276

271-
public static boolean isCached(final int serviceId, final String url,
272-
final InfoItem.InfoType infoType) {
273-
return null != loadFromCache(serviceId, url, infoType).blockingGet();
277+
public static boolean isCached(final int serviceId,
278+
@NonNull final String url,
279+
@NonNull final InfoCache.Type cacheType) {
280+
return null != loadFromCache(serviceId, url, cacheType).blockingGet();
274281
}
275282

283+
284+
/*//////////////////////////////////////////////////////////////////////////
285+
// Utils
286+
//////////////////////////////////////////////////////////////////////////*/
287+
276288
/**
277289
* Formats the text contained in the meta info list as HTML and puts it into the text view,
278290
* while also making the separator visible. If the list is null or empty, or the user chose not

app/src/main/java/org/schabi/newpipe/util/InfoCache.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import org.schabi.newpipe.MainActivity;
2929
import org.schabi.newpipe.extractor.Info;
30-
import org.schabi.newpipe.extractor.InfoItem;
3130

3231
import java.util.Map;
3332

@@ -48,14 +47,27 @@ private InfoCache() {
4847
// no instance
4948
}
5049

50+
/**
51+
* Identifies the type of {@link Info} to put into the cache.
52+
*/
53+
public enum Type {
54+
STREAM,
55+
CHANNEL,
56+
CHANNEL_TAB,
57+
COMMENTS,
58+
PLAYLIST,
59+
KIOSK,
60+
}
61+
5162
public static InfoCache getInstance() {
5263
return INSTANCE;
5364
}
5465

5566
@NonNull
56-
private static String keyOf(final int serviceId, @NonNull final String url,
57-
@NonNull final InfoItem.InfoType infoType) {
58-
return serviceId + url + infoType.toString();
67+
private static String keyOf(final int serviceId,
68+
@NonNull final String url,
69+
@NonNull final Type cacheType) {
70+
return serviceId + ":" + cacheType.ordinal() + ":" + url;
5971
}
6072

6173
private static void removeStaleCache() {
@@ -83,38 +95,42 @@ private static Info getInfo(@NonNull final String key) {
8395
}
8496

8597
@Nullable
86-
public Info getFromKey(final int serviceId, @NonNull final String url,
87-
@NonNull final InfoItem.InfoType infoType) {
98+
public Info getFromKey(final int serviceId,
99+
@NonNull final String url,
100+
@NonNull final Type cacheType) {
88101
if (DEBUG) {
89102
Log.d(TAG, "getFromKey() called with: "
90103
+ "serviceId = [" + serviceId + "], url = [" + url + "]");
91104
}
92105
synchronized (LRU_CACHE) {
93-
return getInfo(keyOf(serviceId, url, infoType));
106+
return getInfo(keyOf(serviceId, url, cacheType));
94107
}
95108
}
96109

97-
public void putInfo(final int serviceId, @NonNull final String url, @NonNull final Info info,
98-
@NonNull final InfoItem.InfoType infoType) {
110+
public void putInfo(final int serviceId,
111+
@NonNull final String url,
112+
@NonNull final Info info,
113+
@NonNull final Type cacheType) {
99114
if (DEBUG) {
100115
Log.d(TAG, "putInfo() called with: info = [" + info + "]");
101116
}
102117

103118
final long expirationMillis = ServiceHelper.getCacheExpirationMillis(info.getServiceId());
104119
synchronized (LRU_CACHE) {
105120
final CacheData data = new CacheData(info, expirationMillis);
106-
LRU_CACHE.put(keyOf(serviceId, url, infoType), data);
121+
LRU_CACHE.put(keyOf(serviceId, url, cacheType), data);
107122
}
108123
}
109124

110-
public void removeInfo(final int serviceId, @NonNull final String url,
111-
@NonNull final InfoItem.InfoType infoType) {
125+
public void removeInfo(final int serviceId,
126+
@NonNull final String url,
127+
@NonNull final Type cacheType) {
112128
if (DEBUG) {
113129
Log.d(TAG, "removeInfo() called with: "
114130
+ "serviceId = [" + serviceId + "], url = [" + url + "]");
115131
}
116132
synchronized (LRU_CACHE) {
117-
LRU_CACHE.remove(keyOf(serviceId, url, infoType));
133+
LRU_CACHE.remove(keyOf(serviceId, url, cacheType));
118134
}
119135
}
120136

0 commit comments

Comments
 (0)