WEBVTT 1 00:00:00.400 --> 00:00:02.130 Hi guys and welcome back. 2 00:00:02.130 --> 00:00:05.290 In this video we're going to be doing TTL caching 3 00:00:05.290 --> 00:00:07.620 or time to live caching 4 00:00:07.620 --> 00:00:10.470 using another package called cachetools. 5 00:00:10.470 --> 00:00:12.700 Let's go over to our Interpreter settings 6 00:00:12.700 --> 00:00:16.200 and instal the cachetools package. 7 00:00:16.200 --> 00:00:18.910 So it's right here, just go ahead and instal that. 8 00:00:18.910 --> 00:00:22.400 And we're gonna be using one of the things in this package 9 00:00:22.400 --> 00:00:25.110 but it's actually got a few different caching functions 10 00:00:25.110 --> 00:00:27.400 that you can look at in the official documentation 11 00:00:27.400 --> 00:00:30.760 linked in the resources section of this lecture. 12 00:00:30.760 --> 00:00:33.180 All you have to do is, instead of importing functools, 13 00:00:33.180 --> 00:00:38.180 we're gonna say from cachetools import cached, TTLCache. 14 00:00:38.360 --> 00:00:42.220 Now instead of functools.lru_cache, we wanna say @cached 15 00:00:43.230 --> 00:00:47.410 and we're gonna pass in a cache which is TTLCache, 16 00:00:47.410 --> 00:00:50.480 and the max size which is gonna be two 17 00:00:50.480 --> 00:00:53.390 and the TTL which is gonna be 900, 18 00:00:53.390 --> 00:00:56.020 so that is a quarter of an hour. 19 00:00:56.020 --> 00:00:57.920 If you wanted to make this longer, you can do 20 00:00:57.920 --> 00:00:59.630 but I'm just doing 900 21 00:00:59.630 --> 00:01:03.400 so that it doesn't necessarily wait an entire hour 22 00:01:03.400 --> 00:01:05.433 for an update if there is one earlier. 23 00:01:06.730 --> 00:01:08.470 By doing this, you'll see that the app 24 00:01:08.470 --> 00:01:11.552 actually runs almost identically fast. 25 00:01:11.552 --> 00:01:16.250 But every 900 seconds, it will run slowly again 26 00:01:16.250 --> 00:01:19.370 because it will actually interact with the server again. 27 00:01:19.370 --> 00:01:21.750 This stuff here can be really useful 28 00:01:21.750 --> 00:01:23.820 especially when you're interacting with APIs 29 00:01:23.820 --> 00:01:26.030 or when you're doing some sort of web scraping. 30 00:01:26.030 --> 00:01:28.860 For example, imagine you have a programme 31 00:01:28.860 --> 00:01:30.900 that scrapes a website 32 00:01:30.900 --> 00:01:33.460 and gives the user some information back. 33 00:01:33.460 --> 00:01:36.520 If you have a lot of users all accessing the same programme, 34 00:01:36.520 --> 00:01:39.720 you may be scraping the same site over and over again. 35 00:01:39.720 --> 00:01:40.670 Instead what you could do, 36 00:01:40.670 --> 00:01:42.890 is you could wrap the whole scraping 37 00:01:42.890 --> 00:01:46.960 in a cached from cachetools and only scrape it 38 00:01:46.960 --> 00:01:50.270 every 600 seconds, for example, every 10 minutes. 39 00:01:50.270 --> 00:01:53.530 That could make your programme almost infinitely faster 40 00:01:53.530 --> 00:01:56.193 while not hurting your users at all. 41 00:01:57.400 --> 00:01:59.870 So hopefully you've learned something in this video. 42 00:01:59.870 --> 00:02:01.320 I'll see you in the next one.