This episode is for members only

Sign up to access "Build an E-Commerce Platform" right now.

Get started
Already a member? Sign in to continue
Playing
21. Caching the cart instance

Episodes

0%
Your progress
  • Total: 8h 42m
  • Played: 0m
  • Remaining: 8h 42m
Join or sign in to track your progress
01. Introduction and demo
9m 42s
0%
02. Installing Laravel, Breeze and Livewire
4m 47s
0%
03. Creating categories
9m 4s
0%
04. Recursively displaying categories
8m 21s
0%
05. Product model and migration
2m 50s
0%
06. Showing a product
7m 50s
0%
07. Product variation setup
10m 26s
0%
08. Creating the product selector
10m 34s
0%
09. Loading child variation dropdowns
4m 28s
0%
10. Faking adding the final variation
9m 33s
0%
11. Setting up product stock
4m 34s
0%
12. Calculating variation stock levels
9m 1s
0%
13. Adding product images with MediaLibrary
8m 50s
0%
14. Creating the product gallery
7m 28s
0%
15. Providing a fallback image
2m 56s
0%
16. Adding media to product variations
3m 37s
0%
17. Creating the cart model
3m 37s
0%
18. Registering the cart service
6m 34s
0%
19. Creating a cart session
11m 4s
0%
20. Showing the cart in the navigation
9m 19s
0%
21. Caching the cart instance
3m 2s
0%
22. Adding items to the cart
14m 1s
0%
23. Creating the notification component
8m 5s
0%
24. Showing the user's cart
6m 50s
0%
25. Outputting cart items
4m 50s
0%
26. Showing variation specifics
8m 8s
0%
27. Updating item quantity
8m 2s
0%
28. Removing an item from the cart
6m 1s
0%
29. Calculating the cart summary
8m 7s
0%
30. Showing the category products page
5m 1s
0%
31. Indexing products in Meilisearch
8m 32s
0%
32. Hooking up products to categories
4m 15s
0%
33. Building the product browser
13m 32s
0%
34. Showing child categories
1m 51s
0%
35. Indexing product variations for filtering
8m
0%
36. Outputting variations for filtering
12m 52s
0%
37. Hooking up product filters with Livewire
7m 48s
0%
38. Filtering products
12m 24s
0%
39. Filtering by price
9m 50s
0%
40. Adding global navigation search
7m 50s
0%
41. Handling products that are not live
3m 22s
0%
42. Price range category fix
1m 18s
0%
43. Scaffolding the checkout page
8m 6s
0%
44. Listing shipping options
9m 3s
0%
45. Calculating the cart totals
2m 52s
0%
46. Validating the account form
9m 10s
0%
47. Validating the shipping form
5m 47s
0%
48. Saving the shipping address
8m 4s
0%
49. Selecting a saved shipping address
6m 39s
0%
50. Fix shipping address error for non authenticated users
1m 7s
0%
51. Redirecting if the cart is empty
2m 42s
0%
52. Checking for quantity changes
7m 11s
0%
53. Syncing if quantities have changed
11m 59s
0%
54. Flashing a message when quantities have changed
5m 48s
0%
55. Setting up for orders
5m 17s
0%
56. Creating an order
13m 24s
0%
57. Attaching variations to order
6m 13s
0%
58. Reducing stock after ordering
2m 56s
0%
59. Meilisearch filter query fix
1m 33s
0%
60. Showing the order confirmation page
7m 39s
0%
61. Attaching orders for registering guest users
5m 17s
0%
62. Scaffolding the orders page
5m 30s
0%
63. Filling in order variation details
5m 24s
0%
64. Returning the order status
4m 39s
0%
65. Detecting order status changes
10m 49s
0%
66. Sending the order status change email
5m 12s
0%
67. Sending an order confirmation email
2m 47s
0%
68. Handling deleted cart records
4m 44s
0%
69. Transferring the guest cart
2m 44s
0%
70. Creating a presenter for the order status
4m 31s
0%
71. Setting up Stripe
3m 43s
0%
72. Creating and updating a PaymentIntent
16m 21s
0%
73. The Stripe card form
3m 35s
0%
74. Validating before payment
5m 34s
0%
75. Submitting a payment
6m 40s
0%
76. Checking for a successful payment
5m 47s
0%
77. Handling Stripe client errors
3m 11s
0%
78. Entangling Stripe customer data
2m 18s
0%

Transcript

00:00
Okay, we're going to need to tweak this along the way with things like ego loading inside of our cart class, but for now, I'm going to go ahead and open up Laravel Debug Bar and check out the additional queries that we're making when we refresh this page.
00:13
Now, at the moment, it's actually not too bad. We have no duplicates. But let's say in several other locations on this page, we were reading cart data, which is highly likely. Let's just simulate this now by just copying and pasting this cart contents countdown.
00:29
A few times, of course, that's not what we want to do, but this is going to give us a really good idea as to what's happening. Now, just by duplicating that a few times, we've now added a huge amount of duplicate queries. Let's go all the way down and have a look at these. And you can see that pretty much every time we call that method,
00:46
we are re-grabbing out the cart from the database by the session UUID and all the variations as well. So this really isn't going to work. The more we use our cart on the same page, the more queries we are going to create. Now, the solution for this is to cache the cart instance that we have created here.
01:07
And by that, I don't mean cache this in some sort of store like Redis or memcached. I mean, just cache this inside of this class. Now, if we cast our mind back to when we created our cart service provider, we created this as a singleton. So what this means is we could just add this to a property inside of this class
01:28
and then check just here if we have that property filled with a cart model already and then return that instead of redoing the database lookup. So this is just a very simple way to cache something within a class. So to do this, at the same time as returning this,
01:45
we're also going to go ahead and set a property on this overall class called instance. So let's go up to the top and just add that in there. So protected instance, because we don't need to be able to grab the outside of here. And then down here, what we're going to do is add a check in to say,
02:01
well, if that instance already exists, this instance, then return, and that's not a method, then return this instance. So what's happening here is the first time around, this is going to be false. So we're not going to return this instance. That will be null. And the second time around, the instance would have been set as well as being returned.
02:25
So next time we make a call to, for example, contents count, and we access the instance here, that will return to us the cached version directly within this class. Let's go over and see the difference with this very simple change. So I'm going to give that a refresh. And there we go. We are back to eight queries.
02:44
We're only doing that lookup once now. We can use this as many times as we want, and that value will always be cached inside of here. Let's finish up by just going over to our navigation, getting rid of all of these. And that's a very simple way to cache a model we've already looked up.
78 episodes8 hrs 42 mins

Overview

Build a robust e-commerce platform with a Laravel and Livewire. Features products with unlimited and flexible variations, a product browser with filters and price range slider, global product search, guest checkout, shipping and payment implementation, order status tracking and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.