-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19. Cache Memory.py
36 lines (30 loc) · 1.5 KB
/
19. Cache Memory.py
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
# Sure! Here's an example Python code for a simple cache implementation using a dictionary:
cache = {}
def get_data(key):
# Check if the data is already in the cache
if key in cache:
print(f"Retrieving data for key {key} from cache")
return cache[key]
else:
# If the data is not in the cache, retrieve it from a data source
data = retrieve_data_from_source(key)
# Store the data in the cache
cache[key] = data
print(f"Retrieving data for key {key} from data source")
return data
def retrieve_data_from_source(key):
# Code to retrieve data from a data source
return f"Data for key {key}"
# Test the cache
print(get_data(1))
print(get_data(2))
print(get_data(1))
# In this example, the cache dictionary stores the cached data, and the get_data function retrieves the data either from the cache or a data source if the data is not in the cache. When the data is retrieved from the data source, it is stored in the cache for future use. The retrieve_data_from_source function simulates retrieving data from a data source.
# When the code is run, the output will be:
Retrieving data for key 1 from data source
Data for key 1
Retrieving data for key 2 from data source
Data for key 2
Retrieving data for key 1 from cache
Data for key 1
# As you can see, the first time get_data is called for each key, the data is retrieved from the data source and stored in the cache. The second time get_data is called for key 1, the data is retrieved from the cache.