Revise Computingrevisecomputing.co.uk
Free student revisionSee questionsFor schoolsSchool enquiry
Free revisionSee questionsFor schoolsSchool enquiry
Knowledge/A Level/H446/02 Algorithms and programming/Thinking ahead/The nature, benefits and drawbacks of caching

The nature, benefits and drawbacks of caching

The nature, benefits and drawbacks of caching.

2.1.2 Thinking ahead

Thinking ahead — Note 3 of 4

Candidates need to understand the purpose of caching in programming and how it can be used when writing a program. Candidates need to be able to apply caching to a scenario and understand the benefits and drawbacks of using it.

What you need to know

Caching stores data that has been used recently, or is likely to be needed soon, in a location that is faster to access. It is an example of thinking ahead, used in processors, web browsers and programs.

What to know

  • Examples: processor cache; web browsers caching pages and images; DNS caching; a program storing the results of an expensive calculation (memoisation).
  • Prefetching: loading data into the cache BEFORE it is requested, based on a prediction of what will be needed.
  • Benefits: faster access to data; less load on servers and networks; less bandwidth used.
  • Drawbacks: cached data can become out of date (stale); caches take up memory or storage; if the prediction is wrong, time is wasted (a cache miss); managing a cache adds complexity.

Key terms

Exam tipCommon mistake

Caching

Temporarily storing data or instructions that have been used, or are likely to be needed, so they can be accessed more quickly next time.

  • Exam tip: Apply caching to the scenario, e.g. 'the game could cache the map tiles around the player so they appear instantly as the player moves'.

Code examples

Caching the results of a slow calculation (memoisation)
python
cache = {}

def slow_square(n):
    if n in cache:              # cache hit: return the stored result
        return cache[n]
    result = n * n              # imagine this takes a long time
    cache[n] = result           # store for next time
    return result
← Previous noteDetermine the preconditions for devising a solution to a problem2.1.2 Thinking aheadNext note →The need for reusable program components2.1.2 Thinking ahead

Back to Thinking ahead

Thinking ahead

Note 3 of 4

  1. 1Identify the inputs and outputs for a given situation
  2. 2Determine the preconditions for devising a solution to a problem
  3. 3The nature, benefits and drawbacks of caching
  4. 4The need for reusable program components
Topic overview