我正在制作一个使用谷歌云端点作为后端的Android应用程序。所以我正在从应用程序请求。我想缓存这些请求在内存中的响应以及存储。如何在谷歌云端点缓存响应?
我想缓存在手机上的响应,这样我就不必做不必要的重复的网络请求。
我在网上搜索了一些内置的解决方案,但在google提供的api中找不到类似的东西。
我总共需要缓存大约2MB的数据。这些数据分布在20个端点请求上。
实现这种缓存的最佳选择是什么?
我正在制作一个使用谷歌云端点作为后端的Android应用程序。所以我正在从应用程序请求。我想缓存这些请求在内存中的响应以及存储。如何在谷歌云端点缓存响应?
我想缓存在手机上的响应,这样我就不必做不必要的重复的网络请求。
我在网上搜索了一些内置的解决方案,但在google提供的api中找不到类似的东西。
我总共需要缓存大约2MB的数据。这些数据分布在20个端点请求上。
实现这种缓存的最佳选择是什么?
我要回答我自己的问题,以便它可以帮助某人,直到有更清洁的解决方案可用。
我使用这个库缓存回应:https://github.com/vincentbrison/android-easy-cache
GenericJson
。使用此序列化响应SerializationUtil使用此代码创建DualCache库样板。 dualCacheByteArray
用于高速缓存响应和dualCacheDate
保持的time_to_live_for_response
public static final int APP_CACHE_VERSION = 1;
public static final String CACHE_ID = "cache_id_string";
public static final String CACHE_ID_DATE = "cache_id_date";
public static final int RAM_CACHE_SIZE = 5 * 1024 * 1024; // 5 mb
public static final int DISK_CACHE_SIZE = 15 * 1024 * 1024; //15 mb
public static final int RAM_CACHE_SIZE_DATE = 1 * 1024 * 1024; // 5 mb
public static final int DISK_CACHE_SIZE_DATE = 3 * 1024 * 1024; //15 mb
private DualCache<byte[]> dualCacheByteArray;
private DualCache<Date> dualCacheDate;
public DualCache<byte[]> getDualCacheByteArray() {
if (dualCacheByteArray == null) {
dualCacheByteArray = new DualCacheBuilder<byte[]>(Constants.CACHE_ID, Constants.APP_CACHE_VERSION, byte[].class)
.useReferenceInRam(Constants.RAM_CACHE_SIZE, new SizeOf<byte[]>() {
@Override
public int sizeOf(byte[] object) {
return object.length;
}
})
.useDefaultSerializerInDisk(Constants.DISK_CACHE_SIZE, true);
}
return dualCacheByteArray;
}
public DualCache<Date> getDualCacheDate() {
if (dualCache == null) {
dualCacheDate = new DualCacheBuilder<Date>(Constants.CACHE_ID_DATE, Constants.APP_CACHE_VERSION, Date.class)
.useReferenceInRam(Constants.RAM_CACHE_SIZE_DATE, new SizeOf<Date>() {
@Override
public int sizeOf(Date date) {
byte[] b = new byte[0];
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(date);
oos.flush();
byte[] buf = baos.toByteArray();
return buf.length;
} catch (IOException e) {
Log.e("some prob", "error in calculating date size for caching", e);
}
return sizeOf(date);
}
})
.useDefaultSerializerInDisk(Constants.DISK_CACHE_SIZE_DATE, true);
}
return dualCacheDate;
}
轨道现在使用上述DualCache
s到缓存你的回应。
getDualCacheByteArray().put(YOUR_RESPONSE_CACHE_KEY, serializedProduct);
getDualCacheDate().put(YOUR_RESPONSE_CACHE_KEY, new Date());
之前制作使用谷歌云终端新的请求,你应该检查在双缓存如果旧响应在高速缓存中
public byte[] getCachedGenericJsonByteArray(String key, int cacheExpireTimeInMinutes) {
Date cachingDate = getDualCacheDate().get(key);
if(cachingDate!=null) {
long expirationTime = TimeUnit.MILLISECONDS.convert(cacheExpireTimeInMinutes, TimeUnit.MINUTES);
long timeElapsedAfterCaching = new Date().getTime() - cachingDate.getTime();
if (timeElapsedAfterCaching >= expirationTime) {
//the cached data has expired
return null;
} else {
byte[] cachedGenericJsonByteArray = getDualCacheByteArray().get(key);
return cachedGenericJsonByteArray;
}
} else {
//result for this key was never cached or is cleared
return null;
}
}
已经存在,如果缓存字节数组不null,然后使用SerializationUtil对其进行反序列化并将其用作缓存响应,否则从Google云端点发出新请求
EDI牛逼:使用序列UTIL在其他的答案中指出SANKET伯德可能不会在所有情况下是必要的
可以实现惊人的库,称为android-easy-cache
样品实现我的回答显示here
我不清楚,如果你想缓存API后端或应用程序的数据,请澄清! – saiyr
我想缓存应用程序中的数据(更新了问题) – penduDev