Community resourceWorksheet
OCR H446 2.1.2 Program caching and reusable components
Part 2 of 2 · H446 2.1.2 · Thinking ahead
Caching and reusable components are the two thinking-ahead mechanisms named in OCR H446 2.1.2, and both fail when validity or the interface is left vague. Students trace hits and misses in a live-event app, judge a 24 hour cache policy against changing closures, and write a reusable duration formatter in Python.
Students will:
- trace a cache hit and a cache miss and say what is computed or stored in each
- separate a program cache designed in application code from hardware processor cache
- explain how a stale cached result arises and recommend a better policy
- implement a small reusable function that honours a stated precondition
- justify reusing a component and state one check to make before adopting it
Inside: 8 explanation cells, 1 multiple-choice question, 1 fill-in-the-blanks cell, 2 written answers and 2 Python tasks. 19 marks, about 25 to 35 minutes.
Series: H446 2.1.2 · Thinking ahead, part 2 of 2.
Shared by Coding PathwayVerified teacher
- 14 cells
- About 30 minutes
- CC BY-SA 4.0
- Shared 31 Aug 2026
- Updated 3 Sept 2026
Preview
The whole resource, exactly as a class sees it. Answers and marking are held back.
Program caching and reusable components
A live-event app repeatedly requests route summaries and schedule labels. Thinking ahead can avoid repeated work, but only if stored results remain valid and components have clear interfaces.
By the end, you will be able to
- trace cache hits, misses, storage and invalidation;
- explain speed/response, memory and stale-data trade-offs;
- distinguish program caching from CPU cache;
- design and test a reusable program component.
Reactivate: a dictionary maps a key to a value; a function receives parameters and may return a result.
Cache decision flow
A cache stores a result so a later request with the same key can reuse it. A hit returns a valid stored result. A miss requires retrieval or computation before storing the result. An entry must be invalidated or expire when the underlying data can change.
route_cache = {}
route_computations = 0
def route_summary(route_id):
global route_computations
if route_id in route_cache:
print("cache hit")
return route_cache[route_id]
print("cache miss")
route_computations += 1
result = "Summary for " + route_id
route_cache[route_id] = result
return result
print(route_summary("R7"))
print(route_summary("R7"))
print("computations:", route_computations)Worked trace
The first R7 request is a miss: the function computes and stores the summary. The second is a hit: it returns the stored summary. route_computations is 1, not 2.
Benefit chain: repeated request → avoided computation/retrieval → lower response time and reduced workload.
Drawback chain: route changes but entry remains → old summary returned → attendee may receive an incorrect route. A validity policy is part of the design, not an optional afterthought.
Guided practice
Predict hit or miss for requests R7, R8, R7 in that order. Then identify which entries must be invalidated if only route R7 changes.
Program cache, not processor cache
This worksheet concerns a cache deliberately designed in a program: application code chooses a key, stores a computed or retrieved result, and decides when it is invalid. A CPU cache is hardware-managed fast memory used to reduce processor access time to main memory. They share the broad idea of retaining data for faster reuse, but their contents, control and level of operation differ.
Which statement about program caching is accurate?
- AA cached result is always current.
- BProgram caching is exactly the same mechanism as CPU cache.
- CEvery request becomes a cache hit.
- DCaching can reduce repeated work but uses memory and needs a validity policy.
The app caches route summaries for 24 hours, but closures can change during the day. Explain one benefit and one drawback of this policy, then recommend an improvement.
Use request pattern, data volatility and consequence.
Students type their answer here.
Reusable components need contracts
A reusable component might be a function or procedure, a module or library, or a larger tested service used through an application programming interface (API). Reuse can avoid writing the same algorithm again, reduce development and repeated testing work, promote consistency and let a team use specialist expertise.
A function called format_schedule(event_name, start_minutes) is more reusable than print_jazz_event_at_630(). Parameters separate the general behaviour from one event. Returning a value lets web, mobile and print views reuse the result without forcing one output device.
Reuse does not guarantee that a component is suitable or error-free. Check its interface, preconditions, documentation, test evidence, maintenance and security before depending on it.
Component checklist: one coherent responsibility → meaningful parameters → documented assumptions → useful returned result → tests at boundaries.
Independent transfer: build a reusable formatter
Implement format_duration(total_minutes). It returns “Hh Mm” using whole hours and remaining minutes. It must work for 0 and values above 60. The precondition is a non-negative integer.
def format_duration(total_minutes):
# Return text such as '2h 5m'.
passExplain two reasons why format_duration can be reused in different parts of an event app, and state one check before another program adopts it.
Refer to its interface, separation from one display context and a suitability check.
Students type their answer here.
Checkpoint
Complete both thinking-ahead mechanisms from memory. There is no answer bank, and correctness is withheld until teacher review.
Review your understanding
Before submitting, check that you can explain the main distinction in your own words, apply it in an unfamiliar context and justify the resulting behaviour or consequence.