Created
May 4, 2021 06:51
-
-
Save jojurajan/6860e4af906a9736b039ba245f79d976 to your computer and use it in GitHub Desktop.
Custom implementation to add a key_prefix while keeping the existing flow in place.
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
def get_key(key, env, encoding='utf-8'): | |
""" | |
This method allows adding a prefix to provided key based on the env value. | |
""" | |
is_str = isinstance(key, str) | |
if is_str: | |
return 'custom_{}_{}'.format(env, key) | |
key = key.decode(encoding) | |
return 'custom_{}_{}'.format(env, key).encode(encoding) | |
def custom_compose_key(func, key_encoding=None, key_refactor=None, env='dev'): | |
""" | |
Custom implementation for the `compose_key` method in | |
https://github.com/youknowone/ring/blob/0.7.3/ring/func/base.py#L648 to | |
allow addition of runtime prefix for cache keys. | |
""" | |
_callable = func.callable | |
_ignorable_keys = func.compose_key.ignorable_keys | |
_key_prefix = get_key(suggest_key_prefix(_callable, None), env) | |
in_memory_storage = hasattr(func.storage, 'in_memory_storage') | |
_compose_key = create_key_builder( | |
_callable, _key_prefix, _ignorable_keys, | |
encoding=key_encoding, key_refactor=key_refactor, in_memory_storage=in_memory_storage | |
) | |
_compose_key.ignorable_keys = _ignorable_keys | |
return _compose_key | |
# Patching the compose_key method with the custom method | |
if hasattr(_method_obj, 'compose_key'): | |
_method_obj.compose_key = custom_compose_key(_method_obj, env=env) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment