Skip to content

Commit

Permalink
Add cache decorator to util and use it for template._get_docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
yaph committed Oct 27, 2020
1 parent 46066bc commit cdf9efb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion logya/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from jinja2 import Environment, FileSystemLoader, escape

from logya.util import cache


env = Environment(
loader=FileSystemLoader('templates'),
Expand Down Expand Up @@ -59,7 +61,7 @@ def _filesource(root: Path, name: str, lines: int = None, raw: bool = False) ->
return text
return escape(text)


@cache
def _get_docs(L, url: str, sort_attr: str = 'created', sort_order: str = 'descending') -> list:
docs = []
# A collection index will only exist at the given URL if there is no content document with the same URL.
Expand Down
12 changes: 12 additions & 0 deletions logya/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@
Paths = namedtuple('Paths', ['root', 'content', 'public', 'static', 'templates'])


def cache(func):
"""Decorator for caching function calls."""

mapping = {}
def f(*args, **kwargs):
key = str(args) + str(kwargs)
if key not in mapping:
mapping[key] = func(*args, **kwargs)
return mapping[key]
return f


# FIXME function never called, except in tests
def deduplicate(li: list, attr: str) -> list:
"""Return a list without duplicates, based on value of given attribute."""
Expand Down

0 comments on commit cdf9efb

Please sign in to comment.