dulwich.lru_cache module

A simple least-recently-used (LRU) cache.

class dulwich.lru_cache.LRUCache(max_cache=100, after_cleanup_count=None)

Bases: object

A class which manages a cache of entries, removing unused ones.

add(key, value, cleanup=None)

Add a new value to the cache.

Also, if the entry is ever removed from the cache, call cleanup(key, value).

Parameters
  • key – The key to store it under

  • value – The object to store

  • cleanup – None or a function taking (key, value) to indicate ‘value’ should be cleaned up.

cache_size()

Get the number of entries we will cache.

cleanup()

Clear the cache until it shrinks to the requested size.

This does not completely wipe the cache, just makes sure it is under the after_cleanup_count.

clear()

Clear out all of the cache.

get(key, default=None)
items()

Get the key:value pairs as a dict.

keys()

Get the list of keys currently cached.

Note that values returned here may not be available by the time you request them later. This is simply meant as a peak into the current state.

Returns: An unordered list of keys that are currently cached.

resize(max_cache, after_cleanup_count=None)

Change the number of entries that will be cached.

class dulwich.lru_cache.LRUSizeCache(max_size=1048576, after_cleanup_size=None, compute_size=None)

Bases: LRUCache

An LRUCache that removes things based on the size of the values.

This differs in that it doesn’t care how many actual items there are, it just restricts the cache to be cleaned up after so much data is stored.

The size of items added will be computed using compute_size(value), which defaults to len() if not supplied.

Create a new LRUSizeCache.

Parameters
  • max_size – The max number of bytes to store before we start clearing out entries.

  • after_cleanup_size – After cleaning up, shrink everything to this size.

  • compute_size – A function to compute the size of the values. We use a function here, so that you can pass ‘len’ if you are just using simple strings, or a more complex function if you are using something like a list of strings, or even a custom object. The function should take the form “compute_size(value) => integer”. If not supplied, it defaults to ‘len()’

add(key, value, cleanup=None)

Add a new value to the cache.

Also, if the entry is ever removed from the cache, call cleanup(key, value).

Parameters
  • key – The key to store it under

  • value – The object to store

  • cleanup – None or a function taking (key, value) to indicate ‘value’ should be cleaned up.

cleanup()

Clear the cache until it shrinks to the requested size.

This does not completely wipe the cache, just makes sure it is under the after_cleanup_size.

resize(max_size, after_cleanup_size=None)

Change the number of bytes that will be cached.