Libraryless. Click here for Pure Java version (11661L/67K).
transient sclass ThumbnailCache { settable WidthAndHeightImpl thumbnailSize = new(100); // max bytes in memory settable long maxBytes = oneMegabyte(); // estimated bytes in memory gettable long usedBytes; Q q = startQ(); new TheMap map; transient Lock lock = lock(); class TheMap extends LinkedHashMap<File, BufferedImage> { @Override protected bool removeEldestEntry(Map.Entry<File, BufferedImage> eldest) { if (usedBytes > maxBytes) { usedBytes -= guessImageBytes(eldest.getValue()); true; } false; } } int guessImageBytes(BufferedImage image) { // 24 is a random guess for the overhead of an image ret 24+guessImageSizeInMemory(image); } BufferedImage makeThumbnail(File file) { var fullSize = loadImage2(file); ret scaleBufferedImageToMaxWidthOrHeight(thumbnailSize, fullSize); } selfType thumbnailSize(int wAndH) { ret thumbnailSize(new WidthAndHeightImpl(wAndH)); } void get(File imageFile, IVF1<BufferedImage> onLoad) { lock lock; if (map.containsKey(imageFile)) onLoad.get(map.get(imageFile)); else q.add(-> { BufferedImage img = null; pcall { img = makeThumbnail(imageFile); } { lock lock; usedBytes += guessImageBytes(img); map.put(imageFile, img); } onLoad.get(img); }); } }
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): elmgxqgtpvxh, mqqgnosmbjvj, wnsclhtenguj
No comments. add comment
Snippet ID: | #1035949 |
Snippet name: | ThumbnailCache |
Eternal ID of this version: | #1035949/14 |
Text MD5: | acb9dd604e9998eb74ac5cf73c1df9e2 |
Transpilation MD5: | ef5a9db2c6916fefa0c87fe02fe8c288 |
Author: | stefan |
Category: | javax / io |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-08-17 21:51:28 |
Source code size: | 1493 bytes / 59 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 145 / 251 |
Version history: | 13 change(s) |
Referenced in: | [show references] |