Skip to content

Commit

Permalink
general: better cached calls
Browse files Browse the repository at this point in the history
* Adds parameters and comments to `cached` wrapper.

Co-Authored-by: Peter Weber <[email protected]>
  • Loading branch information
rerowep committed Nov 13, 2024
1 parent 1fdc0fb commit 36a72dc
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 30 deletions.
4 changes: 3 additions & 1 deletion rero_ils/modules/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ def add_org_lib_doc(item, doc_pid="dummy"):
return item


@cached()
@cached(
timeout=2 * 60 * 60, key_refix="doc_item_lofi_schemas", query_string=True
) # 2 hour timeout
def get_doc_item_lofi_schemas():
"""Get document, item, local field schemas."""
# document schema
Expand Down
4 changes: 2 additions & 2 deletions rero_ils/modules/documents/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@


@api_blueprint.route("/cover/<isbn>")
@cached(timeout=300, query_string=True)
@cached(timeout=5 * 60, query_string=True) # 5 minutes timeout
def cover(isbn):
"""Document cover service."""
return jsonify(get_remote_cover(isbn))
Expand All @@ -51,7 +51,7 @@ def document_availability(pid):


@api_blueprint.route("/advanced-search-config")
@cached(timeout=300, query_string=True)
@cached(timeout=5 * 60, query_string=True) # 5 minutes timeout
@check_logged_as_librarian
def advanced_search_config():
"""Advanced search config."""
Expand Down
6 changes: 4 additions & 2 deletions rero_ils/modules/documents/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
from werkzeug.local import LocalProxy

from ...utils import get_i18n_supported_languages
from ..utils import get_schema_for_resource, memoize
from ..utils import cached, get_schema_for_resource

_records_state = LocalProxy(lambda: current_app.extensions["invenio-records"])


@memoize(timeout=3600)
@cached(
timeout=60 * 60, key_prefix="document_types_from_schema", query_string=True
) # 1 hour timeout
def get_document_types_from_schema(schema="doc"):
"""Create document type definition from schema."""
path = current_jsonschemas.url_to_path(get_schema_for_resource(schema))
Expand Down
21 changes: 0 additions & 21 deletions rero_ils/modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,27 +108,6 @@ def wrapper(*args, **kwargs):
return caching


def memoize(timeout=50):
"""Memoize functions.
Use this to cache the result of a function, taking its arguments into
account in the cache key.
:param timeout: Default 50. If set to an integer, will cache for that
amount of time. Unit of time is in seconds.
"""

def memoize(f):
@wraps(f)
def wrapper(*args, **kwargs):
memoize_fun = current_cache.memoize(timeout=timeout)
return memoize_fun(f)(*args, **kwargs)

return wrapper

return memoize


def strtotime(strtime):
"""String to datetime."""
splittime = strtime.split(":")
Expand Down
2 changes: 1 addition & 1 deletion rero_ils/modules/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

@api_blueprint.route("/permissions/<route_name>", methods=["GET"])
@api_blueprint.route("/permissions/<route_name>/<record_pid>", methods=["GET"])
@cached(timeout=10, query_string=True)
@cached(timeout=10, query_string=True) # 10 seconds timeout
@check_authentication
def permissions(route_name, record_pid=None):
"""HTTP GET request for record permissions.
Expand Down
4 changes: 2 additions & 2 deletions rero_ils/theme/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ def error():


@blueprint.route("/robots.txt")
@cached()
def robots(timeout=60 * 60): # 1 hour timeout
@cached(timeout=60 * 60, query_string=True) # 1 hour timeout
def robots():
"""Robots.txt generate response."""
response = current_app.config["RERO_ILS_ROBOTS"]
response = Response(response=response, status=200, mimetype="text/plain")
Expand Down
1 change: 0 additions & 1 deletion tests/unit/test_cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def test_cli_validate_documents_items_lofi(app, loc_public_martigny):
file_name = join(dirname(__file__), "../data/documents_items_lofi.json")

res = runner.invoke(validate_documents_with_items_lofis_cli, [file_name, "-v"])
print(">>>>", res)
assert res.output.strip().split("\n")[1:] == [
"1 document: dummy_1 errors: 1",
" documents: 'type' is a required property",
Expand Down

0 comments on commit 36a72dc

Please sign in to comment.