imgcache.js icon indicating copy to clipboard operation
imgcache.js copied to clipboard

Implement some form of cache strategies

Open vincentjames501 opened this issue 10 years ago • 2 comments

I'd like to start out by saying very nice library! The code is also very well written and incredibly easy to follow and I appreciate that a ton!

Right now, the library takes a naive approach to managing a cache size limit. When the cache is full, the next time init is called the entire cache is simply cleared. It would be nice to do something like:

// Purges the least recently used items until the cache is no longer full
ImgCache.options.cacheStrategy = ImgCacheCacheStrategy.LEAST_RECENTLY_USED;

// Purges the entire cache when full
ImgCache.options.cacheStrategy = ImgCacheCacheStrategy.PURGE_WHEN_FULL;

// Purges the entire cache each time init and a new file is loaded
ImgCache.options.cacheStrategy = ImgCacheCacheStrategy.ALWAYS;

// Performs no caching
ImgCache.options.cacheStrategy = ImgCacheCacheStrategy.NONE;

I think that after each file is saved to the cached directory, we should run a cleanUpCache method of some kind that adheres to the aforementioned cache strategy and only deletes enough files in the cache such that the current cache size is less than the quota.

vincentjames501 avatar Apr 21 '14 21:04 vincentjames501

This cache size management is indeed limited, actually it's been broken for some time because of a bug in Cordova - see KNOWN_ISSUES. By the way : does it work for you? If that's the case what version of the File Plugin are you using?

You can currently achieve:

  • PURGE_WHEN_FULL using options.cacheClearSize
  • ALWAYS by calling clearCache() after init()

For the others:

  • NONE: the purpose of the library being to cache files.. what would this strategy achieve?
  • LEAST_RECENTLY_USED: this strategy makes most sense for a cache strategy. Though it might be quite an overhead to keep track of the history of files and deleting files one by one might take some time (clearCache is pretty simple as it overwrites the root directory). The definition of 'used' would have to be explicit: does it mean 'added to the cache' (cacheFile) or 'shown' (useCachedFile).

I agree that setting strategies the way you suggest would be much clearer than what it is now. I'll add it when there are more than 2 strategies available.

chrisben avatar May 10 '14 16:05 chrisben

a cache option of NONE is nice because it allows a user to use a familiar api throughout their application that gives them freebies like downloading a file to local storage, monitoring progress, etc and with a cordova app one may want to do other things like image manipulation, etc. It would be nice if a cache option of NONE saved to TEMPORARY storage instead of PERMANENT storage. However, the LRU cache strategy is definitely the one most people are going to be looking for.

vincentjames501 avatar May 12 '14 13:05 vincentjames501