Local Caching

Learn about local caching in Python.

Local caching has the advantage of being (very) fast, as it does not require access to a remote cache over the network. Usually, caching works by storing cached data under a key that identifies it. This technique makes the Python dictionary the most obvious data structure for implementing a caching mechanism.

A simple cache using Python dictionary

Press + to interact
cache = {}
cache['key'] = 'value'
cache = {}
def compute_length_or_read_in_cache(s):
try:
return cache[s]
except KeyError:
cache[s] = len(s)
return cache[s]
print("Foobar: ", compute_length_or_read_in_cache("foobar"))
print('Cache: ', cache)
print("Babaz: ", compute_length_or_read_in_cache("babaz"))
print('Cache: ', cache)

Obviously, such a simple cache has a few drawbacks. First, its size is unbound, which means it can grow to a substantial size that can fill up the entire system memory. That would result in the death of either the process, or even the whole ...