2018-01-23 00:40:00 +00:00
|
|
|
/*
|
2017-09-03 06:04:18 +00:00
|
|
|
* Copyright 2017 Mauricio Colli <mauriciocolli@outlook.com>
|
|
|
|
* InfoCache.java is part of NewPipe
|
|
|
|
*
|
|
|
|
* License: GPL-3.0+
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.schabi.newpipe.util;
|
|
|
|
|
2019-10-04 12:59:08 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.collection.LruCache;
|
2017-09-03 06:04:18 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import org.schabi.newpipe.MainActivity;
|
|
|
|
import org.schabi.newpipe.extractor.Info;
|
2018-09-23 01:32:19 +00:00
|
|
|
import org.schabi.newpipe.extractor.InfoItem;
|
2017-09-03 06:04:18 +00:00
|
|
|
|
2017-11-14 05:13:52 +00:00
|
|
|
import java.util.Map;
|
2018-03-03 19:42:23 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
|
|
|
|
public final class InfoCache {
|
|
|
|
private static final boolean DEBUG = MainActivity.DEBUG;
|
|
|
|
private final String TAG = getClass().getSimpleName();
|
|
|
|
|
|
|
|
private static final InfoCache instance = new InfoCache();
|
|
|
|
private static final int MAX_ITEMS_ON_CACHE = 60;
|
|
|
|
/**
|
|
|
|
* Trim the cache to this size
|
|
|
|
*/
|
|
|
|
private static final int TRIM_CACHE_TO = 30;
|
|
|
|
|
2017-11-14 05:13:52 +00:00
|
|
|
private static final LruCache<String, CacheData> lruCache = new LruCache<>(MAX_ITEMS_ON_CACHE);
|
2017-09-03 06:04:18 +00:00
|
|
|
|
|
|
|
private InfoCache() {
|
|
|
|
//no instance
|
|
|
|
}
|
|
|
|
|
|
|
|
public static InfoCache getInstance() {
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2018-03-03 19:42:23 +00:00
|
|
|
@Nullable
|
2018-09-23 01:32:19 +00:00
|
|
|
public Info getFromKey(int serviceId, @NonNull String url, @NonNull InfoItem.InfoType infoType) {
|
2017-09-03 06:04:18 +00:00
|
|
|
if (DEBUG) Log.d(TAG, "getFromKey() called with: serviceId = [" + serviceId + "], url = [" + url + "]");
|
|
|
|
synchronized (lruCache) {
|
2018-09-29 10:16:47 +00:00
|
|
|
return getInfo(keyOf(serviceId, url, infoType));
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 01:32:19 +00:00
|
|
|
public void putInfo(int serviceId, @NonNull String url, @NonNull Info info, @NonNull InfoItem.InfoType infoType) {
|
2017-09-03 06:04:18 +00:00
|
|
|
if (DEBUG) Log.d(TAG, "putInfo() called with: info = [" + info + "]");
|
2018-03-03 19:42:23 +00:00
|
|
|
|
2018-03-14 03:25:22 +00:00
|
|
|
final long expirationMillis = ServiceHelper.getCacheExpirationMillis(info.getServiceId());
|
2017-09-03 06:04:18 +00:00
|
|
|
synchronized (lruCache) {
|
2018-03-03 19:42:23 +00:00
|
|
|
final CacheData data = new CacheData(info, expirationMillis);
|
2018-09-23 01:32:19 +00:00
|
|
|
lruCache.put(keyOf(serviceId, url, infoType), data);
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 01:32:19 +00:00
|
|
|
public void removeInfo(int serviceId, @NonNull String url, @NonNull InfoItem.InfoType infoType) {
|
2017-09-03 06:04:18 +00:00
|
|
|
if (DEBUG) Log.d(TAG, "removeInfo() called with: serviceId = [" + serviceId + "], url = [" + url + "]");
|
|
|
|
synchronized (lruCache) {
|
2018-09-23 01:32:19 +00:00
|
|
|
lruCache.remove(keyOf(serviceId, url, infoType));
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearCache() {
|
|
|
|
if (DEBUG) Log.d(TAG, "clearCache() called");
|
|
|
|
synchronized (lruCache) {
|
|
|
|
lruCache.evictAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void trimCache() {
|
|
|
|
if (DEBUG) Log.d(TAG, "trimCache() called");
|
|
|
|
synchronized (lruCache) {
|
2018-08-28 18:02:25 +00:00
|
|
|
removeStaleCache();
|
2017-09-03 06:04:18 +00:00
|
|
|
lruCache.trimToSize(TRIM_CACHE_TO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getSize() {
|
|
|
|
synchronized (lruCache) {
|
|
|
|
return lruCache.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 19:42:23 +00:00
|
|
|
@NonNull
|
2018-09-23 01:32:19 +00:00
|
|
|
private static String keyOf(final int serviceId, @NonNull final String url, @NonNull InfoItem.InfoType infoType) {
|
|
|
|
return serviceId + url + infoType.toString();
|
2017-11-14 05:13:52 +00:00
|
|
|
}
|
|
|
|
|
2018-08-28 18:02:25 +00:00
|
|
|
private static void removeStaleCache() {
|
|
|
|
for (Map.Entry<String, CacheData> entry : InfoCache.lruCache.snapshot().entrySet()) {
|
2017-11-14 05:13:52 +00:00
|
|
|
final CacheData data = entry.getValue();
|
|
|
|
if (data != null && data.isExpired()) {
|
2018-08-28 18:02:25 +00:00
|
|
|
InfoCache.lruCache.remove(entry.getKey());
|
2017-11-14 05:13:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 19:42:23 +00:00
|
|
|
@Nullable
|
2018-08-28 18:02:25 +00:00
|
|
|
private static Info getInfo(@NonNull final String key) {
|
|
|
|
final CacheData data = InfoCache.lruCache.get(key);
|
2017-11-14 05:13:52 +00:00
|
|
|
if (data == null) return null;
|
|
|
|
|
|
|
|
if (data.isExpired()) {
|
2018-08-28 18:02:25 +00:00
|
|
|
InfoCache.lruCache.remove(key);
|
2017-11-14 05:13:52 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.info;
|
|
|
|
}
|
|
|
|
|
|
|
|
final private static class CacheData {
|
|
|
|
final private long expireTimestamp;
|
|
|
|
final private Info info;
|
|
|
|
|
2018-03-03 19:42:23 +00:00
|
|
|
private CacheData(@NonNull final Info info, final long timeoutMillis) {
|
|
|
|
this.expireTimestamp = System.currentTimeMillis() + timeoutMillis;
|
2017-11-14 05:13:52 +00:00
|
|
|
this.info = info;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isExpired() {
|
|
|
|
return System.currentTimeMillis() > expireTimestamp;
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|