Anvil: Web Apps with Nothing but Python Transcripts
Chapter: Databases: Storing and querying data
Lecture: Improving database performance
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
So our app is working pretty well. Let's actually clear this out and click around a little bit. When I go_compare, notice there's a little spinner
0:10
and Ajax busy type of thing, and I go_home and it spins, and I go here, and it spins. And I go there, and it spins.
0:19
What's happening? Well, every interaction we're going and getting the user object from the Anvil server.
0:26
In this Home one, we're actually getting the user and we're getting all the measurements, every time. That seems a little silly.
0:34
Maybe we could get it once. We had some location, some centralized place where we could do that, that would be great.
0:40
We do, of course, that's why we created this data_access thing. So let's work on measurements first here.
0:46
Let's create a private variable here called __measurements and we'll start it out being empty like that. Say global, that thing, and here we'll say
0:57
if there are measurements, return __measurements. Otherwise, the __measurements equal to this you know, this is like a cursor thing into the database
1:05
so let's convert this to a local list and then return Measurements. Just doing that one simple thing should make our app work a little bit better.
1:13
Let's do some clean up, and then run it. Are we ready? Notice that we had a little spinner, we go to compare that's the user spinner, we go home.
1:24
That's quicker, quicker, there was more spinner action going on before, but we're still getting a little spinner here. So what's that for?
1:32
Well that's for accessing the user. So let's have a function here called def the_user. We're just going to use that whenever we want
1:44
to access the user. You may be noticing a pattern.
2:04
Okay, so now we have this other function called the_user and we just need to make sure that wherever we were
2:09
working with the user before, so in this part right there let's just change this to data_access which we have not imported in this place
2:24
the_user, something like that. Okay? And let's just cruise around and see if there's anywhere else we're doing this, maybe in navigation.
2:38
Over in require_accounts, actually we're going to need to import data_access here, just to make sure that works.
2:47
Again, we put data_access user, and if it's there we return it, otherwise we're going to log in.
2:53
All right, this is looking pretty good for the user there. Let's see if we're doing anything about the user in these other places.
3:05
Not there, but I think we are compare. Okay, we're probably ready to go. Let's do a quick clean up and re-run it. See if this is working.
3:15
So it did the little spinny thing when it was starting up and we got that output. That's looking good. Let's go here. Wait a minute, that was faster.
3:25
That was a lot faster. Let's go to compare. Let's go home. Oh, so there's still something happening on home. But notice, this is much, much quicker.
3:34
Let's go see what we're missing on the home bit here. Yeah, I'm not sure we're missing anything. I think it's pretty good.
3:41
There, maybe, maybe I'm missing it, but I don't see it. Let's do one final thing. Let's just do a little print statement
3:47
where we show that we're returning the cached measurements for that, and maybe also, the same thing for the user.
3:54
Something like print using cached user I think it's email, let's double check. Email, perfect. And when it first runs, we output our three measurements
4:11
but there's no comment on caching. But as we work around now, as we click around notice we're using the cached user.
4:19
We're not going back to the database. We go home, we're using our cached measurements. No more data access, and look how incredibly quick that is.
4:28
I mean maybe, not sure how well it comes across in the video, but it feels nearly instant. This one right here, a tiny bit of spinning
4:35
as it's loading up stuff, but it's much, much better and that's on a really fast gigabit connection. If we were on crummy 3G or Edge Cellular
4:45
you would really appreciate the fact that this app goes super fast now. And that's it. That's how we use our data access bit to cache things.
4:54
Now there's a few other things about, say logging out and whatnot, we got to be a little bit careful about, so, we're going to have a function
5:01
called logout, and when we do that, we want to just clear out the measurements here. So we'll say user equals none.
5:10
The other thing we want to be careful about is once we add a measurement, how does this get updated?
5:16
Remember, this function was going to get more interesting? So down here we're going to say, that's equal to an empty list as well.
5:26
If we're going to change the measurements we're going to clear the ones we've got and so the next time someone asks for them
5:31
we go back to the database. And let's just make sure that that actually works. And everything should be good.
5:37
Oh, we're not calling the, one other thing. We want to make sure we're call logout down here somewhere. link_logout so let's say data_access.logout.
5:49
That should actually log out the user and I think we'll be good this time. Let's give it a shot. So we're over here, let's try to logout.
5:57
Yeah, everything worked on that. Log back in, that looks like that works and yeah, we're actually getting the measurements logged in again.
6:07
Now notice we're using the cached measurements. Let's add one more, go back in time. Add go, 181, say 73, 74, something like that.
6:18
Notice there's a little spin right there as it took just a moment to add that measurement and now how many do we have?
6:24
We have four. Right, 'cause when we went to the Home page we had to go back and reload it but again now, we're using our four cached measurements.
6:33
I think we have this data access and caching thing nailed.