comparison sat/tools/common/async_utils.py @ 3725:9b45f0f168cf

tools (common): new `async_utils` module with an async version of `lru_cache`
author Goffi <goffi@goffi.org>
date Tue, 25 Jan 2022 17:25:10 +0100
parents
children ebe45ea2df3b
comparison
equal deleted inserted replaced
3724:a0c08fcfe11e 3725:9b45f0f168cf
1 #!/usr/bin/env python3
2
3
4 # Libervia: an XMPP client
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 """tools to launch process in a async way (using Twisted)"""
21
22 from collections import OrderedDict
23 from typing import Optional, Callable, Awaitable
24 from sat.core.log import getLogger
25
26
27 log = getLogger(__name__)
28
29
30 def async_lru(maxsize: Optional[int] = None) -> Callable:
31 """Decorator to cache async function results using LRU algorithm"""
32 def decorator(func: Callable) -> Callable:
33 cache = OrderedDict()
34 async def wrapper(*args) -> Awaitable:
35 if args in cache:
36 log.debug(f"using result in cache for {args}")
37 cache.move_to_end(args)
38 result = cache[args]
39 return result
40 log.debug(f"caching result for {args}")
41 result = await func(*args)
42 cache[args] = result
43 if maxsize is not None and len(cache) > maxsize:
44 value = cache.popitem(False)
45 log.debug(f"Removing LRU value: {value}")
46 return result
47 return wrapper
48 return decorator