Skip to content

Commit

Permalink
Merge pull request #934 from akrherz/no_mckey
Browse files Browse the repository at this point in the history
Allow bypass of memcache check in iemapp()
  • Loading branch information
akrherz authored Jul 17, 2024
2 parents 8349d3b + 02f4ee3 commit c01511e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
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.1"
rev: "v0.5.2"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ All notable changes to this library are documented in this file.

- Add `_check_dueling_tropics` VTEC check looking for TR+HU overlap (#930).
- 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.

### Bug Fixes

Expand Down
5 changes: 4 additions & 1 deletion src/pyiem/webutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ def _mcall(func, environ, start_response, memcachekey, expire, content_type):
if memcachekey is None:
return func(environ, start_response)
key = memcachekey if isinstance(memcachekey, str) else memcachekey(environ)
if key is None:
# An appside short circuit when we programatically do not want cache
return func(environ, start_response)
mc = Client("iem-memcached:11211")
res = mc.get(key)
if not res:
Expand Down Expand Up @@ -392,7 +395,7 @@ def iemapp(**kwargs):
single cursor name with `iemdb_cursorname=<name>`.
- schema (BaseModel): A Pydantic model to parse the form with.
- memcachekey (str or callable): A memcache key to use for caching
the response.
the response. If the callable returns `None`, no caching is done.
- memcacheexpire (int): The number of seconds to cache the response.
- content_type (str or callable): The content type to use for the
response.
Expand Down
17 changes: 17 additions & 0 deletions tests/test_webutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@
)


def test_memcachekey_is_none():
"""Test that we can handle a None memcachekey."""

@iemapp(memcachekey=lambda _e: None)
def application(_environ, _start_response):
"""Test."""
return f"aa{random.random()}".encode("ascii")

env = {
"wsgi.input": mock.MagicMock(),
"QUERY_STRING": "",
}
sr = mock.MagicMock()
res1 = application(env, sr)[0]
assert res1.startswith(b"aa")


def test_iemapp_memcache_keychanged():
"""Test the memcache option."""

Expand Down

0 comments on commit c01511e

Please sign in to comment.