Skip to content

Commit

Permalink
Use lru_cache directly as decorator
Browse files Browse the repository at this point in the history
This was added in Python 3.8
  • Loading branch information
johannaengland committed Aug 8, 2024
1 parent 0a90566 commit 09bf5c3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
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)

0 comments on commit 09bf5c3

Please sign in to comment.