invenio-cache 3.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- invenio_cache/__init__.py +86 -0
- invenio_cache/_compat.py +12 -0
- invenio_cache/bccache.py +21 -0
- invenio_cache/config.py +33 -0
- invenio_cache/decorators.py +98 -0
- invenio_cache/errors.py +41 -0
- invenio_cache/ext.py +52 -0
- invenio_cache/lock.py +220 -0
- invenio_cache/proxies.py +16 -0
- invenio_cache-3.0.1.dist-info/METADATA +48 -0
- invenio_cache-3.0.1.dist-info/RECORD +15 -0
- invenio_cache-3.0.1.dist-info/WHEEL +4 -0
- invenio_cache-3.0.1.dist-info/entry_points.txt +5 -0
- invenio_cache-3.0.1.dist-info/licenses/AUTHORS.rst +8 -0
- invenio_cache-3.0.1.dist-info/licenses/LICENSE +20 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2017-2018 CERN.
|
|
2
|
+
# SPDX-FileCopyrightText: 2024-2026 Graz University of Technology.
|
|
3
|
+
# SPDX-FileCopyrightText: 2026 TU Wien.
|
|
4
|
+
# SPDX-License-Identifier: MIT
|
|
5
|
+
|
|
6
|
+
"""Cache module for Invenio.
|
|
7
|
+
|
|
8
|
+
Initialization
|
|
9
|
+
--------------
|
|
10
|
+
|
|
11
|
+
Create a Flask application:
|
|
12
|
+
|
|
13
|
+
>>> from flask import Flask
|
|
14
|
+
>>> app = Flask('myapp')
|
|
15
|
+
>>> app.config['CACHE_TYPE'] = 'simple'
|
|
16
|
+
|
|
17
|
+
Initialize Invenio-Cache:
|
|
18
|
+
|
|
19
|
+
>>> from invenio_cache import InvenioCache
|
|
20
|
+
>>> ext = InvenioCache(app)
|
|
21
|
+
|
|
22
|
+
Jinja bytecode caching
|
|
23
|
+
----------------------
|
|
24
|
+
By default Jinja only supports filesystem and memcached backends for bytecode
|
|
25
|
+
caching. Invenio-Cache provides another backend, which will use the default
|
|
26
|
+
configured cache backend instead (and thus e.g. supports Redis). Bytecode
|
|
27
|
+
caching helps reduce template load time especially in a multi-process
|
|
28
|
+
environment where workers are recycled from time to time.
|
|
29
|
+
|
|
30
|
+
For more information about Jinja bytecode caching please see
|
|
31
|
+
http://jinja.pocoo.org/docs/2.9/api/#bytecode-cache
|
|
32
|
+
|
|
33
|
+
Enabling the bytecode cache is as simple as:
|
|
34
|
+
|
|
35
|
+
>>> from invenio_cache import BytecodeCache
|
|
36
|
+
>>> app.jinja_options = dict(
|
|
37
|
+
... app.jinja_options,
|
|
38
|
+
... cache_size=1000,
|
|
39
|
+
... bytecode_cache=BytecodeCache(app)
|
|
40
|
+
... )
|
|
41
|
+
|
|
42
|
+
Programmatic API
|
|
43
|
+
----------------
|
|
44
|
+
The programmatic cache API is very simple. First get your cache instance:
|
|
45
|
+
|
|
46
|
+
>>> cache = ext.cache
|
|
47
|
+
|
|
48
|
+
If you are in an Flask application context you can also use a handy proxy:
|
|
49
|
+
|
|
50
|
+
>>> app.app_context().push()
|
|
51
|
+
>>> from invenio_cache import current_cache
|
|
52
|
+
|
|
53
|
+
Now, simply set, get and delete cache values:
|
|
54
|
+
|
|
55
|
+
>>> current_cache.set('mykey', 'myvalue')
|
|
56
|
+
True
|
|
57
|
+
>>> current_cache.get('mykey')
|
|
58
|
+
'myvalue'
|
|
59
|
+
>>> current_cache.delete('mykey')
|
|
60
|
+
True
|
|
61
|
+
|
|
62
|
+
Further documentation
|
|
63
|
+
---------------------
|
|
64
|
+
`Flask-Caching
|
|
65
|
+
<http://pythonhosted.org/Flask-Caching/#configuring-flask-caching>`_ has a good
|
|
66
|
+
and extensive API documentation so please refer to that for other APIs such
|
|
67
|
+
as cache decorators for view, memoization of functions, Jinja snippet caching.
|
|
68
|
+
"""
|
|
69
|
+
|
|
70
|
+
from __future__ import absolute_import, print_function
|
|
71
|
+
|
|
72
|
+
from .bccache import BytecodeCache
|
|
73
|
+
from .decorators import cached_unless_authenticated
|
|
74
|
+
from .ext import InvenioCache
|
|
75
|
+
from .proxies import current_cache, current_cache_ext
|
|
76
|
+
|
|
77
|
+
__version__ = "3.0.1"
|
|
78
|
+
|
|
79
|
+
__all__ = (
|
|
80
|
+
"__version__",
|
|
81
|
+
"cached_unless_authenticated",
|
|
82
|
+
"current_cache_ext",
|
|
83
|
+
"current_cache",
|
|
84
|
+
"BytecodeCache",
|
|
85
|
+
"InvenioCache",
|
|
86
|
+
)
|
invenio_cache/_compat.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2017-2018 CERN.
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
"""Python 2/3 compatibility module."""
|
|
5
|
+
|
|
6
|
+
from __future__ import absolute_import, print_function
|
|
7
|
+
|
|
8
|
+
import sys
|
|
9
|
+
|
|
10
|
+
PY3 = sys.version_info[0] == 3
|
|
11
|
+
|
|
12
|
+
string_types = str if PY3 else basestring
|
invenio_cache/bccache.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2017-2018 CERN.
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
"""Jinja bytecode cache for Redis."""
|
|
5
|
+
|
|
6
|
+
from __future__ import absolute_import, print_function
|
|
7
|
+
|
|
8
|
+
from jinja2.bccache import MemcachedBytecodeCache
|
|
9
|
+
|
|
10
|
+
from .proxies import current_cache
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class BytecodeCache(MemcachedBytecodeCache):
|
|
14
|
+
"""A bytecode cache."""
|
|
15
|
+
|
|
16
|
+
def __init__(self, app):
|
|
17
|
+
"""Initialize `BytecodeCache`."""
|
|
18
|
+
prefix = "{0}jinja::".format(app.config.get("CACHE_KEY_PREFIX"))
|
|
19
|
+
super(self.__class__, self).__init__(
|
|
20
|
+
current_cache, prefix=prefix, timeout=None, ignore_memcache_errors=True
|
|
21
|
+
)
|
invenio_cache/config.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2017-2018 CERN.
|
|
2
|
+
# SPDX-FileCopyrightText: 2025 Graz University of Technology.
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
|
|
5
|
+
"""Configuration for Invenio-Cache module.
|
|
6
|
+
|
|
7
|
+
By default the module is configured to use a Redis database 0 on localhost.
|
|
8
|
+
The underlying Flask-Caching module however supports many other cahe backends.
|
|
9
|
+
|
|
10
|
+
For how to configure other cache backends please refer to the `Flask-Caching
|
|
11
|
+
<http://pythonhosted.org/Flask-Caching/#configuring-flask-caching>`_
|
|
12
|
+
documentation.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
CACHE_KEY_PREFIX = "cache::"
|
|
16
|
+
"""Cache key prefix."""
|
|
17
|
+
|
|
18
|
+
#: Sets the cache type.
|
|
19
|
+
CACHE_TYPE = "flask_caching.backends.redis"
|
|
20
|
+
"""Cache type.
|
|
21
|
+
|
|
22
|
+
Please refer to Flask-Caching documentation for other cache types.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
CACHE_REDIS_URL = "redis://localhost:6379/0"
|
|
26
|
+
"""Redis location and database."""
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
CACHE_IS_AUTHENTICATED_CALLBACK = None
|
|
30
|
+
"""Import path to callback.
|
|
31
|
+
|
|
32
|
+
Callback is executed to determine if request is authenticated.
|
|
33
|
+
"""
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2017-2023 CERN.
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
"""Decorators to help with caching."""
|
|
5
|
+
|
|
6
|
+
import hashlib
|
|
7
|
+
import threading
|
|
8
|
+
import time
|
|
9
|
+
from functools import wraps
|
|
10
|
+
|
|
11
|
+
from .proxies import current_cache, current_cache_ext
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def cached_unless_authenticated(timeout=50, key_prefix="default"):
|
|
15
|
+
"""Cache anonymous traffic."""
|
|
16
|
+
|
|
17
|
+
def caching(f):
|
|
18
|
+
@wraps(f)
|
|
19
|
+
def wrapper(*args, **kwargs):
|
|
20
|
+
cache_fun = current_cache.cached(
|
|
21
|
+
timeout=timeout,
|
|
22
|
+
key_prefix=key_prefix,
|
|
23
|
+
unless=lambda: current_cache_ext.is_authenticated_callback(),
|
|
24
|
+
)
|
|
25
|
+
return cache_fun(f)(*args, **kwargs)
|
|
26
|
+
|
|
27
|
+
return wrapper
|
|
28
|
+
|
|
29
|
+
return caching
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def cached_with_expiration(f):
|
|
33
|
+
"""In-process cache function results, with optional expiration and entropy.
|
|
34
|
+
|
|
35
|
+
This decorator caches function results in-process and not in a distributed
|
|
36
|
+
cache, allowing for optional expiration.
|
|
37
|
+
Entries are based on function arguments and can include entropy to prevent
|
|
38
|
+
multiple keys from expiring simultaneously.
|
|
39
|
+
This implementation is highly inspired by the native functools.lru_cache. This
|
|
40
|
+
func can be replaced if/when lru_cache will accept an expiration time.
|
|
41
|
+
|
|
42
|
+
To tune cache ttl and entropy, the decorated function should have the following
|
|
43
|
+
kwargs:
|
|
44
|
+
:param cache_ttl (int): Expiration time in seconds. Default is 3600 seconds.
|
|
45
|
+
:param cache_entropy (bool): Add entropy to cache expiration. Default is True.
|
|
46
|
+
"""
|
|
47
|
+
cache = {}
|
|
48
|
+
cache_lock = threading.Lock()
|
|
49
|
+
hits = misses = 0
|
|
50
|
+
|
|
51
|
+
@wraps(f)
|
|
52
|
+
def wrapper(*args, **kwargs):
|
|
53
|
+
"""Wrapper."""
|
|
54
|
+
nonlocal hits, misses
|
|
55
|
+
cache_ttl = kwargs.pop("cache_ttl", 3600)
|
|
56
|
+
with_entropy = kwargs.pop("cache_entropy", True)
|
|
57
|
+
|
|
58
|
+
# Create a hashable key that includes args and the sorted kwargs
|
|
59
|
+
key = (args) + tuple(sorted(kwargs.items()))
|
|
60
|
+
|
|
61
|
+
entropy = 0
|
|
62
|
+
if with_entropy:
|
|
63
|
+
# calculate entropy
|
|
64
|
+
key_str = str(key)
|
|
65
|
+
# calculate 2-digits int from args/kwargs to prevent keys created
|
|
66
|
+
# at the same time from expiring simultaneously
|
|
67
|
+
entropy = int(hashlib.md5(key_str.encode()).hexdigest(), 16) % 100
|
|
68
|
+
|
|
69
|
+
now = time.time()
|
|
70
|
+
with cache_lock:
|
|
71
|
+
cache_hit = (
|
|
72
|
+
key in cache and now - cache[key][1] < cache_ttl
|
|
73
|
+
) # exists and not expired
|
|
74
|
+
if cache_hit:
|
|
75
|
+
hits += 1
|
|
76
|
+
return cache[key][0]
|
|
77
|
+
else:
|
|
78
|
+
result = f(*args, **kwargs)
|
|
79
|
+
cache[key] = (result, now + entropy)
|
|
80
|
+
misses += 1
|
|
81
|
+
return result
|
|
82
|
+
|
|
83
|
+
def cache_info():
|
|
84
|
+
"""Report cache statistics."""
|
|
85
|
+
nonlocal hits, misses
|
|
86
|
+
with cache_lock:
|
|
87
|
+
return (hits, misses)
|
|
88
|
+
|
|
89
|
+
def cache_clear():
|
|
90
|
+
"""Clear the cache."""
|
|
91
|
+
nonlocal hits, misses
|
|
92
|
+
with cache_lock:
|
|
93
|
+
cache.clear()
|
|
94
|
+
hits = misses = 0
|
|
95
|
+
|
|
96
|
+
wrapper.cache_clear = cache_clear
|
|
97
|
+
wrapper.cache_info = cache_info
|
|
98
|
+
return wrapper
|
invenio_cache/errors.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2023 CERN.
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
"""Invenio cache errors."""
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class LockError(Exception):
|
|
7
|
+
"""Base error class for lock-related exceptions."""
|
|
8
|
+
|
|
9
|
+
_description = "Base lock error."
|
|
10
|
+
|
|
11
|
+
def __init__(self, lock, message=""):
|
|
12
|
+
"""Constructor, a lock can be passed to provide more details on the error."""
|
|
13
|
+
self.lock = lock
|
|
14
|
+
|
|
15
|
+
def __str__(self):
|
|
16
|
+
"""Return str(self)."""
|
|
17
|
+
return f"Error on lock: {self.lock}: {self._description}"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class LockedError(LockError):
|
|
21
|
+
"""Lock is locked error."""
|
|
22
|
+
|
|
23
|
+
_description = "Lock was not released yet."
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class LockReleaseFailed(LockError):
|
|
27
|
+
"""Error when releasing the lock."""
|
|
28
|
+
|
|
29
|
+
_description = "Lock failed to be released."
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class LockAcquireFailed(LockError):
|
|
33
|
+
"""Error when acquiring the lock."""
|
|
34
|
+
|
|
35
|
+
_description = "Lock failed to be acquired."
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class LockRenewPermissionDenied(LockError):
|
|
39
|
+
"""Error when the renewal failed due to permissions."""
|
|
40
|
+
|
|
41
|
+
_description = "Lock failed to be renewed due to lack of permissions."
|
invenio_cache/ext.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2017-2018 CERN.
|
|
2
|
+
# SPDX-FileCopyrightText: 2025 Graz University of Technology.
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
|
|
5
|
+
"""Cache module for Invenio."""
|
|
6
|
+
|
|
7
|
+
from __future__ import absolute_import, print_function
|
|
8
|
+
|
|
9
|
+
from flask_caching import Cache
|
|
10
|
+
from werkzeug.utils import import_string
|
|
11
|
+
|
|
12
|
+
from . import config
|
|
13
|
+
from ._compat import string_types
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class InvenioCache(object):
|
|
17
|
+
"""Invenio-Cache extension."""
|
|
18
|
+
|
|
19
|
+
def __init__(self, app=None):
|
|
20
|
+
"""Extension initialization."""
|
|
21
|
+
if app:
|
|
22
|
+
self.init_app(app)
|
|
23
|
+
|
|
24
|
+
def init_app(self, app):
|
|
25
|
+
"""Flask application initialization."""
|
|
26
|
+
self.init_config(app)
|
|
27
|
+
self.cache = Cache(app)
|
|
28
|
+
self.is_authenticated_callback = _callback_factory(
|
|
29
|
+
app.config["CACHE_IS_AUTHENTICATED_CALLBACK"]
|
|
30
|
+
)
|
|
31
|
+
app.extensions["invenio-cache"] = self
|
|
32
|
+
|
|
33
|
+
def init_config(self, app):
|
|
34
|
+
"""Initialize configuration."""
|
|
35
|
+
for k in dir(config):
|
|
36
|
+
if k.startswith("CACHE_"):
|
|
37
|
+
app.config.setdefault(k, getattr(config, k))
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _callback_factory(callback_imp):
|
|
41
|
+
"""Factory for creating a is authenticated callback."""
|
|
42
|
+
if callback_imp is None:
|
|
43
|
+
try:
|
|
44
|
+
from flask_login import current_user
|
|
45
|
+
|
|
46
|
+
return lambda: current_user.is_authenticated
|
|
47
|
+
except ImportError:
|
|
48
|
+
return lambda: False
|
|
49
|
+
elif isinstance(callback_imp, string_types):
|
|
50
|
+
return import_string(callback_imp)
|
|
51
|
+
else:
|
|
52
|
+
return callback_imp
|
invenio_cache/lock.py
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2023 CERN.
|
|
2
|
+
# SPDX-FileCopyrightText: 2025 Graz University of Technology.
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""Lock mechanisms."""
|
|
5
|
+
|
|
6
|
+
from datetime import datetime, timezone
|
|
7
|
+
|
|
8
|
+
from flask import current_app
|
|
9
|
+
|
|
10
|
+
from invenio_cache.errors import (
|
|
11
|
+
LockAcquireFailed,
|
|
12
|
+
LockReleaseFailed,
|
|
13
|
+
LockRenewPermissionDenied,
|
|
14
|
+
)
|
|
15
|
+
from invenio_cache.proxies import current_cache
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Lock:
|
|
19
|
+
"""Base Lock class."""
|
|
20
|
+
|
|
21
|
+
def __init__(self, lock_id):
|
|
22
|
+
"""Initialises the lock instance.
|
|
23
|
+
|
|
24
|
+
It does not grant access to the lock itself. To access the lock, call ``lock.acquire``.
|
|
25
|
+
The lock can be used in two ways:
|
|
26
|
+
|
|
27
|
+
1) inside a context:
|
|
28
|
+
|
|
29
|
+
.. code-block:: python
|
|
30
|
+
|
|
31
|
+
with lock(lock_id) as lock:
|
|
32
|
+
lock.acquire()
|
|
33
|
+
f()
|
|
34
|
+
|
|
35
|
+
2) manually calling each block / unblock mechanism:
|
|
36
|
+
|
|
37
|
+
.. code-block:: python
|
|
38
|
+
|
|
39
|
+
lock = lock(lock_id)
|
|
40
|
+
try:
|
|
41
|
+
lock.acquire()
|
|
42
|
+
f()
|
|
43
|
+
lock.release()
|
|
44
|
+
except LockAcquireFailed:
|
|
45
|
+
pass
|
|
46
|
+
|
|
47
|
+
:param lock_id: id of the lock.
|
|
48
|
+
:type lock_id: str
|
|
49
|
+
"""
|
|
50
|
+
self.lock_id = lock_id
|
|
51
|
+
self._created = datetime.now(timezone.utc)
|
|
52
|
+
|
|
53
|
+
def __enter__(self):
|
|
54
|
+
"""Entering the context."""
|
|
55
|
+
return self
|
|
56
|
+
|
|
57
|
+
def __exit__(self, exc_type, *args):
|
|
58
|
+
"""Releases the lock."""
|
|
59
|
+
# Release the lock only when acquired the lock
|
|
60
|
+
if exc_type is not LockAcquireFailed:
|
|
61
|
+
self.release()
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def created(self):
|
|
65
|
+
"""Lock creation date time."""
|
|
66
|
+
return self._created
|
|
67
|
+
|
|
68
|
+
def acquire(self, **kwargs):
|
|
69
|
+
"""Attempts to acquire the lock.
|
|
70
|
+
|
|
71
|
+
.. warning:
|
|
72
|
+
|
|
73
|
+
The method to adcquire the lock must be atomic.
|
|
74
|
+
|
|
75
|
+
"""
|
|
76
|
+
raise NotImplementedError
|
|
77
|
+
|
|
78
|
+
def release(self):
|
|
79
|
+
"""Attempts to release the lock."""
|
|
80
|
+
raise NotImplementedError
|
|
81
|
+
|
|
82
|
+
def exists(self):
|
|
83
|
+
"""Checks if the lock exists."""
|
|
84
|
+
raise NotImplementedError
|
|
85
|
+
|
|
86
|
+
def __repr__(self):
|
|
87
|
+
"""Lock string representation."""
|
|
88
|
+
return f"<Lock {self.lock_id}>"
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class CachedMutex(Lock):
|
|
92
|
+
"""Implements a Mutex using CacheLib API.
|
|
93
|
+
|
|
94
|
+
This lock will set a key in a cache when the lock is acquired, and delete the key when
|
|
95
|
+
the lock is to be deleted.
|
|
96
|
+
This mechanism is based on the principle that the desired cache's backend provides an atomic
|
|
97
|
+
write operation (e.g. "write if not exists").
|
|
98
|
+
"""
|
|
99
|
+
|
|
100
|
+
_cache = current_cache
|
|
101
|
+
|
|
102
|
+
def acquire(self, timeout):
|
|
103
|
+
"""Attempts to acquire the lock.
|
|
104
|
+
|
|
105
|
+
The method to adcquire the lock must be atomic.
|
|
106
|
+
|
|
107
|
+
If an error occurred with the backend, the exception is logged and re-raised.
|
|
108
|
+
If the lock was not released, a ``LockReleaseFailed`` exception is raised.
|
|
109
|
+
|
|
110
|
+
:returns: ``True`` if the lock was acquired, ``False`` otherwise .
|
|
111
|
+
:rtype: boolean
|
|
112
|
+
:param timeout: lock key timeout.
|
|
113
|
+
:type timeout: int
|
|
114
|
+
:raises: Exception, LockReleaseFailed
|
|
115
|
+
"""
|
|
116
|
+
success = False
|
|
117
|
+
try:
|
|
118
|
+
# Atomic operation to get the lock
|
|
119
|
+
success = self._cache.add(self.lock_id, True, timeout=timeout)
|
|
120
|
+
except:
|
|
121
|
+
# Unexpected error with the cache, we just log it and re-raise
|
|
122
|
+
current_app.logger.error(
|
|
123
|
+
f"Unexpected backend failure when acquiring lock {self.lock_id}."
|
|
124
|
+
)
|
|
125
|
+
raise
|
|
126
|
+
|
|
127
|
+
if not success:
|
|
128
|
+
raise LockAcquireFailed(self)
|
|
129
|
+
|
|
130
|
+
return success
|
|
131
|
+
|
|
132
|
+
def release(self):
|
|
133
|
+
"""Attempts to release the lock.
|
|
134
|
+
|
|
135
|
+
If an error occurred with the backend, the exception is logged and re-raised.
|
|
136
|
+
If the lock was not released, a ``LockReleaseFailed`` exception is raised.
|
|
137
|
+
|
|
138
|
+
:returns: ``True`` if the lock was released, ``False`` otherwise .
|
|
139
|
+
:rtype: boolean
|
|
140
|
+
:raises: Exception, LockReleaseFailed
|
|
141
|
+
"""
|
|
142
|
+
success = False
|
|
143
|
+
try:
|
|
144
|
+
success = self._cache.delete(self.lock_id)
|
|
145
|
+
except:
|
|
146
|
+
# Unexpected error with the cache, we just log it and re-raise
|
|
147
|
+
current_app.logger.error(
|
|
148
|
+
f"Unexpected backend failure when releasing lock {self.lock_id}."
|
|
149
|
+
)
|
|
150
|
+
raise
|
|
151
|
+
|
|
152
|
+
if not success:
|
|
153
|
+
raise LockReleaseFailed(self)
|
|
154
|
+
|
|
155
|
+
return success
|
|
156
|
+
|
|
157
|
+
def exists(self):
|
|
158
|
+
"""Checks if the lock exists.
|
|
159
|
+
|
|
160
|
+
:return: ``True``if the lock exists, ``False``otherwise.
|
|
161
|
+
:rtype: bool
|
|
162
|
+
:raises: Exception
|
|
163
|
+
"""
|
|
164
|
+
exists = False
|
|
165
|
+
try:
|
|
166
|
+
# ``has``is a cheaper operation than ``get``
|
|
167
|
+
exists = self._cache.has(self.lock_id)
|
|
168
|
+
except:
|
|
169
|
+
# Unexpected error with the cache, we just log it and re-raise
|
|
170
|
+
current_app.logger.error(
|
|
171
|
+
f"Unexpected backend failure when releasing lock {self.lock_id}."
|
|
172
|
+
)
|
|
173
|
+
raise
|
|
174
|
+
return exists
|
|
175
|
+
|
|
176
|
+
def _has_permission(self):
|
|
177
|
+
"""Whether the current lock instance has permission on the physical lock.
|
|
178
|
+
|
|
179
|
+
It is empty for now as it may be needed in the future when locks should be "shared" between contexts.
|
|
180
|
+
"""
|
|
181
|
+
return True
|
|
182
|
+
|
|
183
|
+
def acquire_or_renew(self, timeout):
|
|
184
|
+
"""Attempts to acquire the lock. If not possible, renews its timeout.
|
|
185
|
+
|
|
186
|
+
The lock's timeout is updated in the cache, effectively prolonging the lock's duration.
|
|
187
|
+
|
|
188
|
+
.. note::
|
|
189
|
+
|
|
190
|
+
This method requires that the caller has the necessary permission.
|
|
191
|
+
|
|
192
|
+
.. warning::
|
|
193
|
+
|
|
194
|
+
If the lock didn't exist before renewing, it will be created with the new timeout.
|
|
195
|
+
|
|
196
|
+
:param timeout: New timeout, in seconds.
|
|
197
|
+
:type timeout: int
|
|
198
|
+
:raises: Exception, LockAcquireFailed, LockRenewPermissionDenied
|
|
199
|
+
"""
|
|
200
|
+
success = False
|
|
201
|
+
if not self._has_permission():
|
|
202
|
+
raise LockRenewPermissionDenied(self)
|
|
203
|
+
|
|
204
|
+
# Overwrites previous timeout. If it didn't exist, the lock is created.
|
|
205
|
+
try:
|
|
206
|
+
success = self.acquire(timeout=timeout)
|
|
207
|
+
except LockAcquireFailed:
|
|
208
|
+
# Renew the lock if it already existed before
|
|
209
|
+
success = self._cache.set(self.lock_id, True, timeout)
|
|
210
|
+
except:
|
|
211
|
+
# Unexpected error with the cache, we just log it and re-raise
|
|
212
|
+
current_app.logger.error(
|
|
213
|
+
f"Unexpected backend failure when releasing lock {self.lock_id}."
|
|
214
|
+
)
|
|
215
|
+
raise
|
|
216
|
+
|
|
217
|
+
if not success:
|
|
218
|
+
raise LockAcquireFailed(self)
|
|
219
|
+
|
|
220
|
+
return success
|
invenio_cache/proxies.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2017-2018 CERN.
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
"""Helper proxies."""
|
|
5
|
+
|
|
6
|
+
from __future__ import absolute_import, print_function
|
|
7
|
+
|
|
8
|
+
from flask import current_app
|
|
9
|
+
from werkzeug.local import LocalProxy
|
|
10
|
+
|
|
11
|
+
current_cache_ext = LocalProxy(lambda: current_app.extensions["invenio-cache"])
|
|
12
|
+
"""Helper proxy to access cache extension object."""
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
current_cache = LocalProxy(lambda: current_app.extensions["invenio-cache"].cache)
|
|
16
|
+
"""Helper proxy to access cache object."""
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: invenio-cache
|
|
3
|
+
Version: 3.0.1
|
|
4
|
+
Summary: Cache module for Invenio.
|
|
5
|
+
Project-URL: Repository, https://github.com/inveniosoftware/invenio-cache
|
|
6
|
+
Author-email: CERN <info@inveniosoftware.org>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: AUTHORS.rst
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: cache,invenio
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Requires-Dist: flask-caching>=1.8.0
|
|
14
|
+
Requires-Dist: invenio-base<3.0.0,>=2.0.0
|
|
15
|
+
Provides-Extra: tests
|
|
16
|
+
Requires-Dist: flask-login>=0.6.3; extra == 'tests'
|
|
17
|
+
Requires-Dist: mock>=2.0.0; extra == 'tests'
|
|
18
|
+
Requires-Dist: pytest-black>=0.6.0; extra == 'tests'
|
|
19
|
+
Requires-Dist: pytest-invenio<5.0.0,>=4.0.0; extra == 'tests'
|
|
20
|
+
Requires-Dist: pytest-mock>=1.6.0; extra == 'tests'
|
|
21
|
+
Requires-Dist: redis>=2.10.5; extra == 'tests'
|
|
22
|
+
Requires-Dist: sphinx>=3; extra == 'tests'
|
|
23
|
+
Description-Content-Type: text/x-rst
|
|
24
|
+
|
|
25
|
+
..
|
|
26
|
+
SPDX-FileCopyrightText: 2017-2018 CERN.
|
|
27
|
+
SPDX-License-Identifier: MIT
|
|
28
|
+
|
|
29
|
+
===============
|
|
30
|
+
Invenio-Cache
|
|
31
|
+
===============
|
|
32
|
+
|
|
33
|
+
.. image:: https://img.shields.io/github/license/inveniosoftware/invenio-cache.svg
|
|
34
|
+
:target: https://github.com/inveniosoftware/invenio-cache/blob/master/LICENSE
|
|
35
|
+
|
|
36
|
+
.. image:: https://github.com/inveniosoftware/invenio-cache/workflows/CI/badge.svg
|
|
37
|
+
:target: https://github.com/inveniosoftware/invenio-cache/actions?query=workflow%3ACI
|
|
38
|
+
|
|
39
|
+
.. image:: https://img.shields.io/coveralls/inveniosoftware/invenio-cache.svg
|
|
40
|
+
:target: https://coveralls.io/r/inveniosoftware/invenio-cache
|
|
41
|
+
|
|
42
|
+
.. image:: https://img.shields.io/pypi/v/invenio-cache.svg
|
|
43
|
+
:target: https://pypi.org/pypi/invenio-cache
|
|
44
|
+
|
|
45
|
+
Cache module for Invenio.
|
|
46
|
+
|
|
47
|
+
Further documentation is available on
|
|
48
|
+
https://invenio-cache.readthedocs.io/
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
invenio_cache/__init__.py,sha256=YYpWrx42JnzOkO16mDVXTgsMMRYHtBx6Hx5QY53CRpw,2364
|
|
2
|
+
invenio_cache/_compat.py,sha256=p9vM33BfXEbw6SS7AO7WExSuRQd_zu24lLmmHTN7Xzk,256
|
|
3
|
+
invenio_cache/bccache.py,sha256=LdLkHlNZuyB5R6Rtbn1AL7gw0jy_ofeHX5vitG01pt4,615
|
|
4
|
+
invenio_cache/config.py,sha256=ygnmjnvZYBCbffKYP_N3Ef_tqXQEBgBAO_7GStx2V7E,929
|
|
5
|
+
invenio_cache/decorators.py,sha256=TAFvYijBkW0493NmJXkdgfYObPPtvbuls2zfDEbGjNw,3106
|
|
6
|
+
invenio_cache/errors.py,sha256=yUwb6NEeoBnTWwrZiMbBYO6GeloY24r6Lvghwq-96vQ,1050
|
|
7
|
+
invenio_cache/ext.py,sha256=FjTKOkjrXyZXoWPoaSg7B73nwAuDRA7ZsW-SG7BriQk,1498
|
|
8
|
+
invenio_cache/lock.py,sha256=KrPzY3Zj9kDR-yxI_QWvfX-w8NEaTs9Y8EJItYJoCAg,6567
|
|
9
|
+
invenio_cache/proxies.py,sha256=azcY9VcrMmnAViM_e_bYNOfxYhTX25VQ1UmnGUu4_s8,482
|
|
10
|
+
invenio_cache-3.0.1.dist-info/METADATA,sha256=aujmy7AN8YiqEP4Kbo93sLgEYopxgCpHzx6Rav6yJMo,1709
|
|
11
|
+
invenio_cache-3.0.1.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
|
|
12
|
+
invenio_cache-3.0.1.dist-info/entry_points.txt,sha256=KX9eJfXQN9CEtZ-CWYmrGNRBMRh2O8xKrJfklrAt0RM,131
|
|
13
|
+
invenio_cache-3.0.1.dist-info/licenses/AUTHORS.rst,sha256=cqTAHTkxSGRFSRsCTa-U1xSjGR5NQ0JVQF6_0OuVE-M,128
|
|
14
|
+
invenio_cache-3.0.1.dist-info/licenses/LICENSE,sha256=bi4ieq1faFKHWB4Id2Di1YzNKRiYyy2wu8dYtq9gbNc,1067
|
|
15
|
+
invenio_cache-3.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2017-2018 CERN.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|