Skip to content

Commit

Permalink
new file
Browse files Browse the repository at this point in the history
  • Loading branch information
mscroggs committed Apr 22, 2024
1 parent 778b089 commit 51288bb
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions ufl/core/caching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Caching.
Custom caching function to be used with class methods.
"""

__cache = {}


def cache(f):
"""Decorator for caching the result of a function."""

def cached_f(self, *args, **kwargs):
global __cache
key = f"{args} {kwargs}"
if self not in __cache[f.__name__]:
__cache[f.__name__][self] = {}
if key not in __cache[f.__name__][self]:
__cache[f.__name__][self][key] = f(self, *args, **kwargs)
return __cache[f.__name__][self][key]

return cached_f


def initialise_cache(function_name: str):
"""Initialise a cacge for a given function."""
global __cache
__cache[function_name] = {}


def empty_cache(function_name: str):
"""Remove the cache for a given function."""
global __cache
del __cache[function_name]

0 comments on commit 51288bb

Please sign in to comment.