Source code for pmesh.cache

"""
    module for caching objects

    MPI Communicator is a finite resource. Plans and Procmeshs uses
    MPI Communicator, and there fore are finite resources too.

    Using a cache for them allows us to reduce the number of usages
"""


[docs]class Cache(dict): def __init__(self): dict.__init__(self) def _hash(self, *keys): return '-'.join(str(i) for i in keys)
[docs] def get(self, create, *keys): """ create() is a function to create the object if it is not there """ hash = self._hash(*keys) if hash not in self: obj = create() self[hash] = (obj, 0) obj, i = self[hash] i = i + 1 self[hash] = (obj, i) return obj
[docs] def expire(self, *keys): hash = self._hash(*keys) if hash not in self: return obj, i = self[hash] i = i - 1 if i == 0: print('expiring', hash) del self[hash]