Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lru_cache as decorator #2904

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions python/nav/alertengine/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ def check_alerts(debug=False):
@transaction.atomic()
def handle_new_alerts(new_alerts):
"""Handles new alerts on the queue"""
memoized_check_alert = lru_cache()(check_alert_against_filtergroupcontents)
_logger = logging.getLogger('nav.alertengine.handle_new_alerts')
accounts = []

Expand Down Expand Up @@ -232,14 +231,13 @@ def subscription_sort_key(subscription):
alertsubscriptions,
dupemap,
_logger,
memoized_check_alert,
check_alert_against_filtergroupcontents,
permissions,
)
del alert
del account
del permissions

del memoized_check_alert
del new_alerts
gc.collect()

Expand Down Expand Up @@ -599,6 +597,7 @@ def alert_should_be_ignored(queued_alert, subscription, now):
)


@lru_cache
def check_alert_against_filtergroupcontents(alert, filtergroupcontents, atype):
"""Checks a given alert against an array of filtergroupcontents"""

Expand Down
7 changes: 2 additions & 5 deletions python/nav/metrics/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
_reverse_handlers = []


def _lookup(metric):
@lru_cache(maxsize=200)
def lookup(metric):
"""
Looks up a NAV object from a metric path.

Expand All @@ -42,10 +43,6 @@ def _lookup(metric):
return func(**match.groupdict())


# pylint: disable=C0103
lookup = lru_cache(maxsize=200)(_lookup)


def reverses(pattern):
"""Decorator to map regex patterns to reverse lookup functions"""
try:
Expand Down
6 changes: 2 additions & 4 deletions python/nav/web/ipdevinfo/host_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def reverse_lookup(addresses):
yield {'addr': addr, 'name': name}


def _get_host_info(host):
@lru_cache
def get_host_info(host):
"""Returns a dictionary containing DNS information about the host"""
if is_valid_ip(host, strict=True):
addresses = list(reverse_lookup([host]))
Expand All @@ -61,6 +62,3 @@ def _get_host_info(host):
addresses = list(reverse_lookup(addresses))

return {'host': host, 'addresses': addresses}


get_host_info = lru_cache()(_get_host_info)
Loading