Comments (14)
Yes, I totally agree with this suggestion. This could be the most useful new feature.
from disklrucache.
I don't this should be implemented in the library itself. Instead write to a separate index when the entry expires. Then when you read back you can compare this, and if it has expired clear it.
This would also enable you to do conditional get and then update only the expires timestamp.
from disklrucache.
I'm going to side with @lexs on this and deem it as out-of-scope. There's a few ways to do this with a wrapper around DiskLruCache
's classes or a separate index.
from disklrucache.
Ok, I don't get it. Vote to re-open or please explain: Are there really no expirations of any kind in this code? Most cache's have a way to expire content, that's why they are a cache, not permanent storage. Maybe i'm missing it, if so sorry for the noob comment.
from disklrucache.
The cache expires by a fixed size and by ordering by recently accessed
items. If you don't access entry A while accessing and writing a bunch of
others (B, C, D, E, etc.) then eventually A will be bubbled out of the
cache. You can read more about LRU on wikipedia:
https://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used
Jake Wharton
http://about.me/jakewharton
On Wed, Oct 30, 2013 at 7:54 AM, fraggle222 [email protected]:
Ok, I don't get it. Vote to re-open or please explain: Are there really no
expirations of any kind in this code? Most cache's have a way to expire
content, that's why they are a cache, not permanent storage. Maybe i'm
missing it, if so sorry for the noob comment.—
Reply to this email directly or view it on GitHubhttps://github.com//issues/3#issuecomment-27395838
.
from disklrucache.
Thanks for responding. I get LRU. But that leaves the general problem of item expiration unsolved:
Use case: you are utilizing a service that lets you cache images for at most 3 days. After that you are required to refresh the image.
Or anytime you want to have a max age on a particular file or even for the entire cache in general.
Just seems like a common issue and one that I have right now: need to save some images, but I want to force a refresh every x number of days. Images are saved at different times, so not one global timeout in this case.
Seems like I can't use your cache library for this without independently writing the code to check the file last mod times, and that just seems weird to do when I'm using a caching library to control this stuff.
Would prefer DiskLruCache.get(String key, long maxAge); and have it return null if expired or better DiskLruCache.isExpired(String key, long maxAge);
from disklrucache.
Max age is an application level constraint. You can look at OkHttp which
uses this library to implement the HTTP caching semantics as an example.
On Oct 30, 2013 8:48 AM, "fraggle222" [email protected] wrote:
Thanks for responding. I get LRU. But that leaves the general problem of
item expiration unsolved:Use case: you are utilizing a service that lets you cache images for at
most 3 days. After that you are required to refresh the image.Or anytime you want to have a max age on a particular file or even for the
entire cache in general.Just seems like a common issue and one that I have right now: need to save
some images, but I want to force a refresh every x number of days. Images
are saved at different times, so not one global timeout in this case.Seems like I can't use your cache library for this without independently
writing the code to check the file last mod times, and that just seems
weird to do when I'm using a caching library to control this stuff.Would prefer DiskLruCache.get(String key, long maxAge); and have it return
null if expired or better DiskLruCache.isExpired(String key, long maxAge);—
Reply to this email directly or view it on GitHubhttps://github.com//issues/3#issuecomment-27402224
.
from disklrucache.
Ok, strange I'll have to take a look at that.
Seems like your library doesn't give any access to the underlying File objects right, just gives access to InputStreams? So I'm not sure how any code can determine a cached files created time or modified time without independently checking the file. This requires knowing the file structure and naming in the cache, which external code shouldn't need to do since that is stuff that is essentially private to your library.
An alternate request then would be to give access to a File object for cached files, or if that is too concrete then to give access to the last time the entry was put into the cache.
{edited at 12:42pm edt}
from disklrucache.
The time is stored as a key on the entry, not read from the underlying file.
from disklrucache.
In DiskLruCache? Not seeing that. I'll do some more digging. No timestamps in the journal. No access to Entry from the Editor for a snapshot, no time info in the Entry class anyway, getCleanFile is private. Maybe I'm missing it somewhere. I see no time info anywhere in DiskLruCache, nor any way to get to the underlying file info.
My hack so far:
public long getAge(String key) {
File file=new File(mDiskCache.getDirectory(),key+".0"); //hack
return file.lastModified();
}
from disklrucache.
Store it separately, DiskLruCache isn't meant to do everything. It is a highly specific datastore. For example, just store the last modified time in SharedPreferences with the same key as your DiskLruCache entry. Update it when you update your entry.
from disklrucache.
Store the time in the entry of the cache!
DiskLruCache.Editor e = cache.edit("somekey");
e.set(0, "DATE"); // Modification date, or expiration date, or creation date
e.set(1, "DATA");
e.commit();
from disklrucache.
@JakeWharton's answer has a better solution.
from disklrucache.
@JakeWharton , ok thanks, I'll try that approach, much appreciated.
from disklrucache.
Related Issues (20)
- Clear Cache HOT 1
- Copyright (C) 2011 The Android Open Source Project Copyright 2012 Jake Wharton Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
- executorService is not shut down
- Can you support the OpenHarmony JavaScript version? HOT 1
- Does getCacheDir() supported? HOT 3
- Simplify interface and make lib truly asynchronous HOT 4
- Multi-thread: Infinite loop in trimToSize HOT 5
- Closing the correct resource ? HOT 1
- Validate key Issue HOT 1
- max size doesn't take in consideration minimum 4kb block size. HOT 2
- Files disappear after a while HOT 1
- Old Image is always shown HOT 1
- Basic usage and code samples? HOT 1
- [Question] Where is the performence report of DiskLruCache? HOT 1
- what can cause cache is closed
- Hey! 61 tests 10 failed I have... HOT 5
- Request for new release 2.0.3 HOT 1
- Get entry count/iterator
- while loop in StrictLineReader.java HOT 2
- Key length is 64 for dependencies resolved via maven/jcenter HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from disklrucache.