我应该在 Guava 中将字符串键设置为软键吗?
我应该在 Guava 中将字符串键设置为软键吗?
我将它用于简单的位图缓存。
private final ConcurrentMap<String, Bitmap> imageCache = new MapMaker()
.softValues()
.expireAfterAccess(IMAGE_EXPIRATION_TIMEOUT, TimeUnit.SECONDS)
.makeComputingMap(new Function<String, Bitmap>() {
public Bitmap apply(String key) {
Log.d(TAG, "Image loaded");
Bitmap bm = null;
try {
URL aURL = new URL(key);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (Exception e) {
Log.w(TAG, e);
}
return bm;
}
});
Should I make string key to be softkey in Guava?
Im using it for simple bitmap cache.
private final ConcurrentMap<String, Bitmap> imageCache = new MapMaker()
.softValues()
.expireAfterAccess(IMAGE_EXPIRATION_TIMEOUT, TimeUnit.SECONDS)
.makeComputingMap(new Function<String, Bitmap>() {
public Bitmap apply(String key) {
Log.d(TAG, "Image loaded");
Bitmap bm = null;
try {
URL aURL = new URL(key);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (Exception e) {
Log.w(TAG, e);
}
return bm;
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,您不应该使用软键做任何事情……它在最新版本中已被弃用。有关原因的更多信息,请参阅此答案。
另请注意,在最新版本中 缓存和CacheBuilder 应优先使用
MapMaker
创建计算地图。No, you shouldn't use soft keys for anything... it's deprecated in the latest release. See this answer for more on why.
Also note that in the latest release Cache and CacheBuilder should be used in preference to creating computing maps with
MapMaker
.