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

Allow memcacheexpire to be a callable #938

Merged
merged 3 commits into from
Jul 25, 2024
Merged
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ci:
autoupdate_schedule: quarterly
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.5.3"
rev: "v0.5.4"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to this library are documented in this file.
### New Features

- Add `_check_dueling_tropics` VTEC check looking for TR+HU overlap (#930).
- Allow `iemapp(memcachexpire)` to be a callable, called back with environ.
- Cross check WCNs against watch database storage for PDS status (#925).
- For `iemapp` if the `memcachekey=` callable returns `None`, the check of
memcache is short circuited.
Expand Down
7 changes: 5 additions & 2 deletions src/pyiem/webutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,9 @@ def _mcall(func, environ, start_response, memcachekey, expire, content_type):
res = mc.get(key)
if not res:
res = func(environ, start_response)
mc.set(key, res, expire)
mc.set(
key, res, expire if isinstance(expire, int) else expire(environ)
)
else:
# since our function never got called, we need to start_response
ct = (
Expand Down Expand Up @@ -396,7 +398,8 @@ def iemapp(**kwargs):
- schema (BaseModel): A Pydantic model to parse the form with.
- memcachekey (str or callable): A memcache key to use for caching
the response. If the callable returns `None`, no caching is done.
- memcacheexpire (int): The number of seconds to cache the response.
- memcacheexpire (int or callable): The number of seconds to cache
the response, defaults to 3600.
- content_type (str or callable): The content type to use for the
response.

Expand Down
Binary file modified tests/baseline/test_windrose_hads_wind.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/test_webutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
def test_memcachekey_is_none():
"""Test that we can handle a None memcachekey."""

@iemapp(memcachekey=lambda _e: None)
@iemapp(memcachekey=lambda _e: None, memcacheexpire=lambda _e: 60)
def application(_environ, _start_response):
"""Test."""
return f"aa{random.random()}".encode("ascii")
Expand Down
6 changes: 3 additions & 3 deletions tests/test_windrose_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ def test_windrose_hads_wind():
"""Test the database filtering with actual database data."""
# Faked from iem-database repo store_test_data
fig = windrose(
"XXXX",
"EOKI4",
database="hads",
months=[4, 5, 6],
sts=utc(2020, 1, 5),
ets=utc(2020, 9, 5),
sts=utc(2024, 1, 5),
ets=utc(2024, 9, 5),
tzname="America/Chicago",
nogenerated=True,
)
Expand Down
Loading