[ServiceWorker] consider exposing size for cache entries (#587)

(I think this is a known feature request, but didn't see an issue for it.)

Consider how you would build a media application like a music player or photo gallery.  While you probably couldn't store all media offline, it would be nice to save some amount of the most frequently used files.

Currently the Cache API lets us build an LRU cache based on count:

```
var cache;
cache.open('foo').then(function(foo) {
  cache = foo;
  return cache.match(request);
}).then(function(response) {
  if (response) {
    // update order of entries in keys()
    cache.put(request, response.clone());
    return response;
  }

  var maxItems = 100;
  return addToLRU(cache, maxItems, request);
});

function addToLRU(cache, maxItems, request) {
  return cache.keys().then(function(keys) {
    if (keys.length < maxItems) {
      return cache.add(request);
    }

    return cache.delete(keys[0]).then(function() {
      return cache.add(request);
    });
  });
}
```

It would be nice, however, to be able to store items up until a certain size limit is reached.  I was thinking an API like:

```
  Promise<unsigned long> sizeOf(Request or USVString request, optional QueryParams params);
  Promise<unsigned long> sizeOfAll(Request or USVString request, optional QueryParams params);
```

These would function like match() and matchAll(), but return a size value instead of the responses.

It would then be necessary to define what "size" means.  This I am less sure of:

* Size of on-disk or in-memory?  I assume on-disk would be preferable.
* Size of just the body or including all fields?  The body will typically dominate for most Responses.
* Require approximate or exact measurements from the browser?  Given compression, databases, and de-duplication, I think approximate would be better.

Thoughts?

---
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/587

Received on Wednesday, 10 December 2014 21:52:21 UTC