1
00:00:01,140 --> 00:00:03,083
Instructor: Our next technique is the caching.

2
00:00:03,953 --> 00:00:05,880
Caching is used to store

3
00:00:05,880 --> 00:00:09,150
frequently accessed data close to the API.

4
00:00:09,150 --> 00:00:12,900
So if we have a data that the API access frequently

5
00:00:12,900 --> 00:00:16,650
then it might be a good idea to store it somewhere close

6
00:00:16,650 --> 00:00:19,260
to the API code instead of a database

7
00:00:19,260 --> 00:00:22,020
that is usually stored on a different server

8
00:00:22,020 --> 00:00:24,990
or sometimes even in a different data center.

9
00:00:24,990 --> 00:00:28,950
Now, cache engines usually store the data in-memory

10
00:00:28,950 --> 00:00:30,570
so the retrieval of the data

11
00:00:30,570 --> 00:00:33,150
from the cache is extremely fast.

12
00:00:33,150 --> 00:00:36,150
There is no IO operation, there is no network.

13
00:00:36,150 --> 00:00:40,320
It simply goes to the memory and retrieve the data.

14
00:00:40,320 --> 00:00:42,300
Now, when working with caching,

15
00:00:42,300 --> 00:00:45,720
always make sure to set expiration and invalidation

16
00:00:45,720 --> 00:00:49,710
so that the data in the cache will automatically expire

17
00:00:49,710 --> 00:00:51,840
after a predefined interval.

18
00:00:51,840 --> 00:00:55,800
And also the data will be invalidated,

19
00:00:55,800 --> 00:00:59,940
which means deleted from the cache when something happened.

20
00:00:59,940 --> 00:01:02,100
For example, if you store in the cache

21
00:01:02,100 --> 00:01:04,230
the list of users of the system

22
00:01:04,230 --> 00:01:06,330
then when this list is modified

23
00:01:06,330 --> 00:01:08,130
and a new user is added,

24
00:01:08,130 --> 00:01:10,830
then the data in the cache should be invalidated

25
00:01:10,830 --> 00:01:13,020
because it's not relevant anymore.

26
00:01:13,020 --> 00:01:15,720
This data is called stale data,

27
00:01:15,720 --> 00:01:17,940
and you should always try your best

28
00:01:17,940 --> 00:01:20,373
to avoid having stale data in the cache.

29
00:01:21,780 --> 00:01:23,220
In this code example,

30
00:01:23,220 --> 00:01:27,750
you see an implementation of ResponseCache in ASP.NET core.

31
00:01:27,750 --> 00:01:32,280
And as you can see, this cache has a duration of 30 seconds.

32
00:01:32,280 --> 00:01:34,710
Now, what this means is that the response

33
00:01:34,710 --> 00:01:38,250
from this web API, the item web API,

34
00:01:38,250 --> 00:01:41,430
is cached on the web server for 30 seconds.

35
00:01:41,430 --> 00:01:43,410
So after the first request,

36
00:01:43,410 --> 00:01:47,580
any subsequent request for 30 seconds will get the response

37
00:01:47,580 --> 00:01:50,400
from the cache and not from the code.

38
00:01:50,400 --> 00:01:53,220
And this way, the time of executing the code

39
00:01:53,220 --> 00:01:55,140
and probably going to the database,

40
00:01:55,140 --> 00:01:58,203
is saved and the performance vastly improve.

