PWA Tutorial for Beginners #21 - Limiting Cache Size
Skills:
Backend Performance80%
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: Backend Performance
View skill →Related Reads
📰
📰
📰
📰
The Enter key that submits your form while a Japanese user is still typing
Dev.to · greymoth
The two-Reacts bug: when packages aren't singletons
Dev.to · r9v
🚀 Introducing Prism Guard — An Open Source Frontend Architecture Intelligence Platform
Dev.to · Ritumoni Sarma
The Death of the Heavy Hydration Layer
Dev.to · Amodit Jha
🎓
Tutor Explanation
DeepCamp AI