A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.md 3.5KB

4 vuotta sitten
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. title: Cache-limiting in Service Workers
  2. url: https://adactio.com/journal/9844
  3. hash_url: dfe747a193bf51d2fa8680797ab29d7f
  4. <p>When I was documenting <a href="https://adactio.com/journal/9775">my first Service Worker</a> I mentioned that every time a user requests a page, I store that page in a cache for later (offline) use:</p>
  5. <blockquote>
  6. <p>Right now I’m stashing any HTML pages the user visits into the cache. I don’t think that will get out of control—I imagine most people only ever visit just a handful of pages on my site. But there’s the chance that the cache could get quite bloated. Ideally I’d have some way of keeping the cache nice and lean.</p>
  7. <p>I was thinking: maybe I should have a separate cache for HTML pages, and limit the number in that cache to, say, 20 or 30 items. Every time I push something new into that cache, I could pop the oldest item out.</p>
  8. <p>I could imagine doing something similar for images: keeping a cache of just the most recent 10 or 20.</p>
  9. </blockquote>
  10. <p>Well I&#8217;ve done that now. Here&#8217;s <a href="https://gist.github.com/adactio/4d588bb8a65fa11a3ea3">the updated Service Worker code</a>.</p>
  11. <p>I&#8217;ve got a function in there called <code>stashInCache</code> that takes a few arguments: which cache to use, the maximum number of items that should be in there, the request (URL), and the response:</p>
  12. <pre><code>var stashInCache = function(cacheName, maxItems, request, response) {
  13. caches.open(cacheName)
  14. .then(function (cache) {
  15. cache.keys()
  16. .then(function (keys) {
  17. if (keys.length &lt; maxItems) {
  18. cache.put(request, response);
  19. } else {
  20. cache.delete(keys[0])
  21. .then(function() {
  22. cache.put(request, response);
  23. });
  24. }
  25. })
  26. });
  27. };
  28. </code></pre>
  29. <p>It looks to see if the current number of items in the cache is less than the specified maximum:</p>
  30. <pre><code>if (keys.length &lt; maxItems)
  31. </code></pre>
  32. <p>If so, go ahead and cache the item:</p>
  33. <pre><code>cache.put(request, response);
  34. </code></pre>
  35. <p>Otherwise, delete the first item from the cache and <em>then</em> put the item in the cache:</p>
  36. <pre><code>cache.delete(keys[0])
  37. .then(function() {
  38. cache.put(request, response);
  39. });
  40. </code></pre>
  41. <p>For HTML requests, I limit the cache to 35 items:</p>
  42. <pre><code>var copy = response.clone();
  43. var cacheName = version + pagesCacheName;
  44. var maxItems = 35;
  45. stashInCache(cacheName, maxItems, request, copy);
  46. return response;
  47. </code></pre>
  48. <p>For images, I&#8217;m limiting the cache to 20 items:</p>
  49. <pre><code>var copy = response.clone();
  50. var cacheName = version + imagesCacheName;
  51. var maxItems = 20;
  52. stashInCache(cacheName, maxItems, request, copy);
  53. return response;
  54. </code></pre>
  55. <p>Here&#8217;s <a href="https://adactio.com/serviceworker.js">my updated Service Worker</a>.</p>
  56. <p>The cache-limited seems to be working for pages. But for some reason the images cache has blown past its allotted maximum of 20 (you can see the items in the caches under the &#8220;Resources&#8221; tab in Chrome under &#8220;Cache Storage&#8221;).</p>
  57. <p>This is almost certainly because I&#8217;m doing something wrong or have completely misunderstood how the caching works. If you can spot what I&#8217;m doing wrong, <a href="https://adactio.com/contact">please let me know</a>.</p>