PWA Tutorial for Beginners #21 - Limiting Cache Size

Net Ninja · Beginner ·🌐 Frontend Engineering ·7y ago

Key Takeaways

Limits cache size in a Progressive Web App

Full Transcript

okay so right now when we visit pages and resources online we're caching every new dynamic page or resource that we come across that's not already cached now that's okay but it means at some point that our cache is going to become bloated and be a drain on resources because we're just caching everything that we request now it would be nice to be able to limit our different caches to a specific size so that they could only hold X number of items for example 15 or 20 and when that number of items is reached then we can remove the oldest item and so forth so if we currently have for example 20 items then add in a new item so it has 21 in total at that point we could go through the cache and delete the oldest item so the cache size would go back down to 20 and it would never be exceeded so let's try this out in the code so what I'm going to do then is now make a function to limit the size of our cache so first of all let's make this at the top do a little comment to say cache size limit function and then underneath it will make a constant and we'll call this function limit cache size set it equal to an error function this error function is going to take in two arguments the name and the size now the name is going to be the name of the cache that we want to limit the size off so we're going to pass that into the function and that could either be this static cache name or the dynamic cache name and this is going to be the maximum size that we want our cache it to be so the maximum number of items inside that cache okay so let's open up this function now and inside what do we want to do well the first thing we want to do is open up whichever cache we pass in so let's say caches dots open and then the name now this is asynchronous it returns a promise so we can tack on dot then and when this opens it returns towards the cache that it opens now what we want to do is get the keys of this cache which are basically these things right here or if they're the dynamic ones it would be the resources for the dynamic cache so these things right here so what I'm going to do is say cat dr. keys which is a function to get us those keys this again is asynchronous it returns to us a promise so we tack on dot then and then we take these keys which is an array okay so now we have an array of essentially all the different things inside the cache now what we want to do is check is that array over this size because if it is over this size then what we want to do is start deleting items from the start of that array the oldest ones so then let's do a little live check I'm going to say if and then keys dot length is going to be over the size which is whatever we pass in here then we want to do something now what do we want to do we want to delete something from the cache so we'll say cache delete and then what do we want to delete the first item in the keys array the oldest thing so let's say keys and then 0 which is the first item in that array so this goes out and it deletes the first item in that array so the first item from the cache and then what we're going to do is tack on a dot there method because this returns a promise it's asynchronous and in the dot then method what I'd like to do is then call this function again because if it's 22 currently that we have in there and we want it to be no more than 20 then we're going to have to delete two items so we want to recall this function to recheck either the keys that length is still over the size that we pass in which could be 20 and if it is delete another item okay so we'll call this again right here limit cache size again and we're going to pass in the name again and the size and again the name is just going to be what we first pass in and the size is going to be what we first pass in as well so I hope that makes sense we're basically recalling this function over and over again until this is no longer true so if we get to the point where the length of the keys is not greater than the size then we don't need to call this again and we don't need to continue with this cycle and carry on deleting things from the cache okay so then we have our function the next thing to do is actually call the function when we want to limit the size of the cash now where do we want to do that well I think we should do it down here inside the Flex event whenever we try to put something new in to the dynamic cash at that point we want to do a check because if we keep on putting stuff in and not check in then it's going to get bloated and very big so after we put something in we're going to do a check and see if the cash is over a certain size and then if it is we're going to do the items so we'll call this function from here so its limits cash size and what we want to do is pass in the cash name that is dynamic cash name oops spell is correctly and then we want to say what is the maximum number of items we want to be in this cash for now I'm just gonna set this to 3 it's gonna be more later but I want to demo this and since we don't have many things inside our dynamic cash I want this number to be small for now so we're saying here every time we reach out and add something new to our dynamic cash we want to then limit the size of our cash do a check do we have more than three items in it if we do delete the oldest one so it can never get more than three items that's what we're doing now every time we add something to the dynamic cash so let me save this now and if we go over to the application first of all go to serviceworkers and then go to skip waiting make sure you're on line and you've not got this offline mode checked then what we need to do is go to the home page I'm gonna check site dynamic and notice we have three items if I now go to the about page which is not inside the cash then it should cash this but it deletes the oldest one so I'm going to go back to the home page and navigate around a little bit and it's stayin as three now so that's fine so this is working if I change this to two then save it and go back over here I'm going to go to the service worker skip waiting and update over here then I'm going to go to the cash next I'm going to go to the about page watch these items and we can see now we only have this about in here okay so now this is our working now I'm not going to stick this at two I'm going to keep this at about 15 so now I'm saying we can have no more than 15 items in our dynamic cache at any one time then it's going to start to delete older items so there we go my friends now we are deleting our older cache items to make sure that our cache never gets too big

Original Description

Hey gang, in this PWA tutorial we'll talk about how to limit our cache size so that it doesn't ever get too big. ---------------------------------------- 🐱‍💻 🐱‍💻 Course Links: + Course files - https://github.com/iamshaunjp/pwa-tutorial + Modern JavaScript Tutorial - https://www.thenetninja.co.uk/udemy/modern-javascript 🐱‍💻 🐱‍💻 Other Related Courses: + Firebase Firestore Playlist - https://www.youtube.com/watch?v=4d-gIPGzmK4&list=PL4cUxeGkcC9itfjle0ji1xOZ2cjRGY_WB Firebase Authentication Tutorial - https://www.youtube.com/watch?v=aN1LnNq4z54&list=PL4cUxeGkcC9jUPIes_B8vRjn1_GaplOPQ
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

📰
The Enter key that submits your form while a Japanese user is still typing
Learn how to prevent the Enter key from submitting a form while a Japanese user is still typing, and why it matters for user experience
Dev.to · greymoth
📰
The two-Reacts bug: when packages aren't singletons
Learn to fix the two-Reacts bug by understanding how to handle non-singleton packages in React applications
Dev.to · r9v
📰
🚀 Introducing Prism Guard — An Open Source Frontend Architecture Intelligence Platform
Learn about Prism Guard, an open-source frontend architecture intelligence platform, and how it can help improve codebase quality
Dev.to · Ritumoni Sarma
📰
The Death of the Heavy Hydration Layer
Learn why plain HTML is the new developer flex and how to simplify web development by ditching heavy hydration layers
Dev.to · Amodit Jha
Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →