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, 2 fill-in-the-blanks cells, 2 written answers and 2 Python tasks. 23 marks, about 30 to 40 minutes.
Series: H446 2.1.2 · Thinking ahead, part 2 of 2.
Shared by Coding PathwayVerified teacher
- 15 cells
- About 30 minutes
- CC BY-SA 4.0
- Shared 31 Aug 2026
- Updated 15 Sept 2026
Preview
The whole resource, exactly as a class sees it. Answers and marking are held back.
Thinking ahead: caching and reusable components
A program may be asked for the same result many times. It can sometimes save time by storing a result and reusing it. A programmer can also save development work by reusing a tested component instead of writing the same solution again. Both choices require planning: old cached data may become incorrect, and a reused component may not suit the new program.
By the end, you will be able to
- trace cache hits, misses and invalidation;
- explain the benefits and drawbacks of program caching;
- distinguish a cache designed in a program from CPU cache memory;
- explain why reusable components are useful and test a reusable function.
Remember: a dictionary maps a key to a value. A function receives data through parameters and can return a result.
Follow a request through a program cache
A cache stores a result so that the program can reuse it later. A request is identified by a key, such as a route ID.
- A cache hit occurs when the key has a valid stored result. The program can return that result without repeating the original work.
- A cache miss occurs when no valid result is stored. The program must retrieve or calculate the result, store it and then return it.
- Invalidation removes or marks an entry as invalid when its source data changes. An expiry time can also stop an old result from being used forever.
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 request for R7 is a cache miss, so the function creates and stores the summary. The second request for R7 is a cache hit, so the stored summary is returned. The value of route_computations is therefore 1 rather than 2.
This improves performance because the repeated request avoids another calculation or retrieval. The response can be produced sooner and the program performs less repeated work.
Caching also creates a risk. If route R7 changes but its cached summary remains, the program will return old information. An attendee could then receive an incorrect route. The programmer must therefore decide when an entry expires or is invalidated.
Caching is most useful when requests repeat and the original result is costly to calculate or retrieve. If results rarely repeat or change very often, the extra memory and validity checks may outweigh the benefit.
- hit
- miss
- R7
- R8
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.
Explain why reuse can help, why a 24-hour-old result could be unsafe, and how your change reduces that risk.
Students type their answer here.
Why programs reuse components
A reusable program component is a piece of code designed to solve a useful, repeatable task in more than one place. It might be a function, procedure, module or library. Reusing a suitable component can reduce repeated development and unit-testing work, improve consistency and allow programmers to use another developer's expertise. The new program must still test that the component works correctly when it is integrated.
For example, format_schedule(event_name, start_minutes) is more reusable than print_jazz_event_at_630(). Its parameters allow different event names and times to be supplied. Returning a value also allows a web page, mobile app or printed schedule to use the result.
A reused component is not automatically suitable or error-free. Before using it, check its interface: what inputs it expects, what it returns and what must already be true. Test it at important boundaries before relying on it.
Extension: teams may also need to check documentation, licensing, security and whether the component will continue to be maintained.
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):
hours = total_minutes // 60
# Calculate the minutes left after the whole hours.
minutes = 0
# Return text such as '2h 5m'.
return ""Explain two reasons why format_duration can be reused in different parts of an event app, and state one check before another program adopts it.
For each reason, identify a feature of the function and explain how it allows reuse. Then state one check another programmer should make before using it.
Students type their answer here.
Checkpoint
Complete the caching and reuse explanation from memory. Use the precise terms you have learned; your teacher will review your answers.
Review your understanding
Before submitting, check that you can trace cache hits and misses, explain one caching benefit and drawback, and explain why a reusable component must still be checked before use.