Giter VIP home page Giter VIP logo

Comments (14)

miroslavign avatar miroslavign commented on August 24, 2024

Yes, I totally agree with this suggestion. This could be the most useful new feature.

from disklrucache.

lexs avatar lexs commented on August 24, 2024

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.

JakeWharton avatar JakeWharton commented on August 24, 2024

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.

fraggle222 avatar fraggle222 commented on August 24, 2024

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.

JakeWharton avatar JakeWharton commented on August 24, 2024

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.

fraggle222 avatar fraggle222 commented on August 24, 2024

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.

JakeWharton avatar JakeWharton commented on August 24, 2024

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.

fraggle222 avatar fraggle222 commented on August 24, 2024

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.

JakeWharton avatar JakeWharton commented on August 24, 2024

The time is stored as a key on the entry, not read from the underlying file.

from disklrucache.

fraggle222 avatar fraggle222 commented on August 24, 2024

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.

DHuckaby avatar DHuckaby commented on August 24, 2024

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.

JakeWharton avatar JakeWharton commented on August 24, 2024

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.

DHuckaby avatar DHuckaby commented on August 24, 2024

@JakeWharton's answer has a better solution.

from disklrucache.

fraggle222 avatar fraggle222 commented on August 24, 2024

@JakeWharton , ok thanks, I'll try that approach, much appreciated.

from disklrucache.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.