-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from gwww/new-radar
Improve radar CPU usage
- Loading branch information
Showing
5 changed files
with
212 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
.PHONY: install install-core clean isort lint test | ||
|
||
clean: | ||
find . -name '*.pyc' -exec rm -f {} + | ||
find . -name '*.pyo' -exec rm -f {} + | ||
find . -name '__pycache__' -exec rm -rf {} + | ||
find . -name '.cache' -exec rm -rf {} + | ||
find . -name '.pytest_cache' -exec rm -rf {} + | ||
rm -rf build dist *.egg-info .venv | ||
|
||
install: | ||
ifndef VIRTUAL_ENV | ||
$(error Create venv (python -m venv .venv) and activate virtual env first.) | ||
else | ||
pip install -e . | ||
pip install pytest | ||
pip install pylint | ||
pip install ruff | ||
endif | ||
|
||
install-core: | ||
pip install -e . | ||
|
||
isort: | ||
sh -c "isort --skip-glob=.tox ." | ||
|
||
lint: | ||
pylint --msg-template='{msg_id}({symbol}):{line:3d},{column}: {obj}: {msg}' elkm1_lib | ||
ruff check | ||
|
||
test: | ||
pytest tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,25 @@ | ||
from aiohttp import ClientSession | ||
from datetime import datetime, timedelta | ||
from datetime import datetime | ||
|
||
from .constants import USER_AGENT | ||
|
||
CACHE_EXPIRE_TIME = timedelta(minutes=200) # Time is tuned for 3h radar image | ||
|
||
class Cache: | ||
_cache = {} | ||
|
||
class CacheClientSession(ClientSession): | ||
"""Shim to cache ClientSession requests.""" | ||
@classmethod | ||
def add(cls, cache_key, item, cache_time): | ||
"""Add an entry to the cache.""" | ||
|
||
_cache = {} | ||
cls._cache[cache_key] = (datetime.now() + cache_time, item) | ||
return item # Returning item useful for chaining calls | ||
|
||
def _flush_cache(self): | ||
"""Flush expired cache entries.""" | ||
@classmethod | ||
def get(cls, cache_key): | ||
"""Get an entry from the cache.""" | ||
|
||
# Delete expired entries at start so we don't use expired entries | ||
now = datetime.now() | ||
expired = [key for key, value in self._cache.items() if value[0] < now] | ||
expired = [key for key, value in cls._cache.items() if value[0] < now] | ||
for key in expired: | ||
del self._cache[key] | ||
|
||
async def get(self, url, params, cache_time=CACHE_EXPIRE_TIME): | ||
"""Thin wrapper around ClientSession.get to cache responses.""" | ||
|
||
self._flush_cache() # Flush at start so we don't use expired entries | ||
|
||
cache_key = (url, tuple(sorted(params.items()))) | ||
result = self._cache.get(cache_key) | ||
if not result: | ||
result = ( | ||
datetime.now() + cache_time, | ||
await super().get( | ||
url=url, params=params, headers={"User-Agent": USER_AGENT} | ||
), | ||
) | ||
self._cache[cache_key] = result | ||
del cls._cache[key] | ||
|
||
return result[1] | ||
result = cls._cache.get(cache_key) | ||
return result[1] if result else None |
Oops, something went wrong.