Skip to content

Commit

Permalink
Refine @cached decorator from #3258
Browse files Browse the repository at this point in the history
Don't restrict to Python 2 precisely.
  • Loading branch information
sampsyo committed May 9, 2019
1 parent d236e1e commit ff1d43d
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions beets/util/functemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,12 +554,15 @@ def _parse(template):
return Expression(parts)


# Decorator that enables lru_cache on py3, and no caching on py2.
def cached(func):
if six.PY2:
# Sorry python2 users, no caching for you :(
"""Like the `functools.lru_cache` decorator, but works (as a no-op)
on Python < 3.2.
"""
if hasattr(functools, 'lru_cache'):
return functools.lru_cache(maxsize=128)(func)
else:
# Do nothing when lru_cache is not available.
return func
return functools.lru_cache(maxsize=128)(func)


@cached
Expand Down

0 comments on commit ff1d43d

Please sign in to comment.