-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13-class-attribute-cache-system.py
More file actions
429 lines (313 loc) · 11.3 KB
/
13-class-attribute-cache-system.py
File metadata and controls
429 lines (313 loc) · 11.3 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
"""Question: Define a class Cache with a private class attribute _cache
to store cached values. Use class methods to add, retrieve, and clear cache entries.
"""
# LEARNING CHALLENGE
#
# Before looking at any solution below, please try to solve this yourself first!
#
# Tips for success:
# - Read the question carefully
# - Think about what classes and methods you need
# - Start with a simple implementation
# - Test your code step by step
# - Don't worry if it's not perfect - learning is a process!
#
# Remember: The best way to learn programming is by doing, not by reading solutions!
#
# Take your time, experiment, and enjoy the learning process!
# Try to implement your solution here:
# (Write your code below this line)
# HINT SECTION (Only look if you're really stuck!)
#
# Think about:
# - What is a cache? (temporary storage for frequently accessed data)
# - Why use class methods for cache operations? (shared across all instances)
# - What data structure should store cache entries? (dictionary)
# - How do you handle missing keys? (return None or default value)
#
# Remember: Start simple and build up complexity gradually!
# ===============================================================================
# STEP-BY-STEP SOLUTION
# ===============================================================================
#
# CLASSROOM-STYLE WALKTHROUGH
#
# Let's solve this problem step by step, just like in a programming class!
# Each step builds upon the previous one, so you can follow along and understand
# the complete thought process.
#
# ===============================================================================
# Step 1: Define the Cache class with private class attribute
# ===============================================================================
# Explanation:
# Let's start by creating our Cache class with a private class attribute to store cached data.
# Using a class attribute means the cache is shared across all instances.
class Cache:
_cache = {}
# What we accomplished in this step:
# - Created Cache class with private class attribute _cache
# Step 2: Add class method to add cache entries
# ===============================================================================
# Explanation:
# Class methods take 'cls' as the first parameter and can access class attributes.
# The add method stores key-value pairs in our cache.
class Cache:
_cache = {}
@classmethod
def add(cls, key, value):
cls._cache[key] = value
# What we accomplished in this step:
# - Added class method to add entries to the cache
# Step 3: Add class method to retrieve cache entries
# ===============================================================================
# Explanation:
# The get method retrieves values from the cache. We use dict.get() to safely
# handle missing keys by returning None instead of raising an exception.
class Cache:
_cache = {}
@classmethod
def add(cls, key, value):
cls._cache[key] = value
@classmethod
def get(cls, key):
return cls._cache.get(key, None)
# What we accomplished in this step:
# - Added class method to retrieve cache entries safely
# Step 4: Add class method to clear cache
# ===============================================================================
# Explanation:
# The clear method removes all entries from the cache. This is useful for
# cache invalidation or memory management.
class Cache:
_cache = {}
@classmethod
def add(cls, key, value):
cls._cache[key] = value
@classmethod
def get(cls, key):
return cls._cache.get(key, None)
@classmethod
def clear(cls):
cls._cache.clear()
# What we accomplished in this step:
# - Added class method to clear all cache entries
# Step 5: Add additional cache management methods
# ===============================================================================
# Explanation:
# Let's add more functionality to make our cache more complete, including
# methods to check existence, remove specific entries, and get cache statistics.
class Cache:
_cache = {}
@classmethod
def add(cls, key, value):
cls._cache[key] = value
@classmethod
def get(cls, key, default=None):
return cls._cache.get(key, default)
@classmethod
def remove(cls, key):
if key in cls._cache:
del cls._cache[key]
return True
return False
@classmethod
def exists(cls, key):
return key in cls._cache
@classmethod
def clear(cls):
cls._cache.clear()
@classmethod
def size(cls):
return len(cls._cache)
@classmethod
def keys(cls):
return list(cls._cache.keys())
@classmethod
def values(cls):
return list(cls._cache.values())
@classmethod
def items(cls):
return list(cls._cache.items())
# What we accomplished in this step:
# - Enhanced get method with default parameter
# - Added remove method for specific key deletion
# - Added exists method to check key presence
# - Added size, keys, values, and items methods for cache inspection
# Step 6: Add cache statistics and advanced features
# ===============================================================================
# Explanation:
# Let's add some advanced features like cache statistics and expiration handling
# to make our cache more production-ready.
import time
class Cache:
_cache = {}
_stats = {'hits': 0, 'misses': 0, 'adds': 0, 'removes': 0}
@classmethod
def add(cls, key, value, ttl=None):
"""Add a value to cache with optional time-to-live"""
if ttl:
expiry_time = time.time() + ttl
cls._cache[key] = {'value': value, 'expiry': expiry_time}
else:
cls._cache[key] = {'value': value, 'expiry': None}
cls._stats['adds'] += 1
@classmethod
def get(cls, key, default=None):
"""Get a value from cache, handling expiration"""
if key in cls._cache:
entry = cls._cache[key]
# Check if entry has expired
if entry['expiry'] and time.time() > entry['expiry']:
del cls._cache[key]
cls._stats['misses'] += 1
return default
cls._stats['hits'] += 1
return entry['value']
cls._stats['misses'] += 1
return default
@classmethod
def remove(cls, key):
if key in cls._cache:
del cls._cache[key]
cls._stats['removes'] += 1
return True
return False
@classmethod
def exists(cls, key):
if key in cls._cache:
entry = cls._cache[key]
# Check if entry has expired
if entry['expiry'] and time.time() > entry['expiry']:
del cls._cache[key]
return False
return True
return False
@classmethod
def clear(cls):
cls._cache.clear()
@classmethod
def size(cls):
return len(cls._cache)
@classmethod
def get_stats(cls):
return cls._stats.copy()
@classmethod
def reset_stats(cls):
cls._stats = {'hits': 0, 'misses': 0, 'adds': 0, 'removes': 0}
# What we accomplished in this step:
# - Added TTL (time-to-live) support for cache entries
# - Added cache statistics tracking
# - Added automatic expiration handling
# Step 7: Test the cache system
# ===============================================================================
# Explanation:
# Finally, let's test our complete cache system to make sure all functionality
# works correctly, including TTL and statistics.
import time
class Cache:
_cache = {}
_stats = {'hits': 0, 'misses': 0, 'adds': 0, 'removes': 0}
@classmethod
def add(cls, key, value, ttl=None):
if ttl:
expiry_time = time.time() + ttl
cls._cache[key] = {'value': value, 'expiry': expiry_time}
else:
cls._cache[key] = {'value': value, 'expiry': None}
cls._stats['adds'] += 1
@classmethod
def get(cls, key, default=None):
if key in cls._cache:
entry = cls._cache[key]
if entry['expiry'] and time.time() > entry['expiry']:
del cls._cache[key]
cls._stats['misses'] += 1
return default
cls._stats['hits'] += 1
return entry['value']
cls._stats['misses'] += 1
return default
@classmethod
def remove(cls, key):
if key in cls._cache:
del cls._cache[key]
cls._stats['removes'] += 1
return True
return False
@classmethod
def exists(cls, key):
if key in cls._cache:
entry = cls._cache[key]
if entry['expiry'] and time.time() > entry['expiry']:
del cls._cache[key]
return False
return True
return False
@classmethod
def clear(cls):
cls._cache.clear()
@classmethod
def size(cls):
return len(cls._cache)
@classmethod
def get_stats(cls):
return cls._stats.copy()
@classmethod
def reset_stats(cls):
cls._stats = {'hits': 0, 'misses': 0, 'adds': 0, 'removes': 0}
# Test our cache system:
print("Testing Cache System:")
# Test basic operations
Cache.add("user:1", {"name": "Alice", "age": 30})
Cache.add("user:2", {"name": "Bob", "age": 25})
Cache.add("config", {"theme": "dark", "language": "en"})
print(f"Cache size: {Cache.size()}")
print(f"User 1: {Cache.get('user:1')}")
print(f"User 2: {Cache.get('user:2')}")
print(f"Config: {Cache.get('config')}")
# Test missing key
print(f"Missing key: {Cache.get('user:3', 'Not found')}")
# Test existence check
print(f"User 1 exists: {Cache.exists('user:1')}")
print(f"User 3 exists: {Cache.exists('user:3')}")
# Test TTL functionality
print(f"\nTesting TTL (Time-To-Live):")
Cache.add("temp_data", "This will expire", ttl=2) # 2 seconds TTL
print(f"Temp data (immediately): {Cache.get('temp_data')}")
print("Waiting 3 seconds for expiration...")
time.sleep(3)
print(f"Temp data (after expiration): {Cache.get('temp_data', 'Expired')}")
# Test statistics
print(f"\nCache statistics: {Cache.get_stats()}")
# Test removal
Cache.remove("user:2")
print(f"After removing user:2, size: {Cache.size()}")
print(f"User 2 after removal: {Cache.get('user:2', 'Not found')}")
# Test clear
Cache.clear()
print(f"After clearing cache, size: {Cache.size()}")
print(f"Final statistics: {Cache.get_stats()}")
# What we accomplished in this step:
# - Created and tested our complete cache system
# - Demonstrated basic operations, TTL, and statistics
# ===============================================================================
# CONGRATULATIONS!
#
# You've successfully completed the step-by-step solution!
#
# Key concepts learned:
# - Class-level caching with shared state
# - Class methods for cache operations
# - TTL (time-to-live) implementation
# - Cache statistics and monitoring
# - Automatic expiration handling
# - Production-ready cache features
#
# Try it yourself:
# 1. Start with Step 1 and code along
# 2. Test each step before moving to the next
# 3. Understand WHY each step is necessary
# 4. Experiment with modifications (try adding LRU eviction or size limits!)
#
# Remember: The best way to learn is by doing!
# ===============================================================================