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

Feature/add cache decorator #313

Merged
merged 45 commits into from
Sep 27, 2024
Merged

Feature/add cache decorator #313

merged 45 commits into from
Sep 27, 2024

Conversation

AMontagu
Copy link
Collaborator

@AMontagu AMontagu commented Jul 26, 2024

  • Test on real app
  • Clean code
  • Write forgotten test about change default settings value for cache
  • Test automatique wrapper around django decorator
  • Implement an auto clean cache decorator from django signals
  • Write documentation*
  • Make changelog. Put inside the inheritance of django request/response. The headers proxy, the http_to_grpc_decorator, cache feature and the fact that metadata in test should match real grpc convention, drop support of django version and python versions

@AMontagu AMontagu force-pushed the feature/addCacheDecorator branch 6 times, most recently from bc50bac to 5ae15cb Compare August 1, 2024 13:39
@AMontagu AMontagu requested a review from legau August 1, 2024 14:42
@AMontagu AMontagu force-pushed the feature/addCacheDecorator branch from eb8d4d6 to fd1aeb3 Compare August 2, 2024 10:44
@AMontagu AMontagu marked this pull request as ready for review August 2, 2024 11:35
django_socio_grpc/decorators.py Outdated Show resolved Hide resolved
django_socio_grpc/decorators.py Outdated Show resolved Hide resolved
django_socio_grpc/decorators.py Outdated Show resolved Hide resolved
django_socio_grpc/decorators.py Show resolved Hide resolved
:param senders: The senders to listen to the signal
:param invalidator_signals: The django signals to listen to delete the cache
"""
if not key_prefix:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key_prefix has no default value so this case is only when the user puts ""

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it is to avoid user puts "" by mistake

return grpc_request_metadata

def get_query_params(self, grpc_request: Message):
def get_query_params(self, grpc_request: Message) -> dict:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def get_query_params(self, grpc_request: Message) -> dict:
def get_query_params(self, grpc_request: Message) -> dict[str, str]:

:param invalidator_signals: The django signals to listen to delete the cache
"""
if not key_prefix:
logger.warning(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why without a prefix does it not delete only this route from cache ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because if not using Redis there is no way to clean multiple key from cache without knowing them exactly.
So we clear all the cach: https://github.com/socotecio/django-socio-grpc/pull/313/files#diff-e320866935eaa353b47eb8ff96e0bbcb20aa7fa3b0ae29b08681b63604816cb6R308

@@ -362,6 +405,10 @@ async def wrapped(*args, **kwargs):
)
return self

async def with_call(self, request=None, metadata=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't seem to exist in async

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch. I changed it

django_socio_grpc/decorators.py Outdated Show resolved Hide resolved
@AMontagu AMontagu force-pushed the feature/addCacheDecorator branch from 117e15f to dbb6458 Compare August 12, 2024 14:13
@@ -123,6 +124,11 @@ def __set_name__(self, owner, name):
if "_decorated_grpc_action_registry" not in owner.__dict__:
owner._decorated_grpc_action_registry = {}
owner._decorated_grpc_action_registry.update({name: self.get_action_params()})
# INFO - AM - 22/08/2024 - Send a signal to notify that a grpc action has been created. Used for now in cache deleter
grpc_action_set.send(sender=self, owner=owner, name=name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this you access only the parent class which defines the action, this should be in the register method

# INFO - AM - 22/08/2024 - http_to_grpc is a decorator but as it is returned from a decorator (to be able to access func) we need to call it directly to have the actual decorator
return http_to_grpc(
method_decorator(
cache_page(timeout, key_prefix=key_prefix, cache=cache), name="cache_page"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the nonlocal will make this work, the functions are already resolved and returned. This could be done by modifying action.function = http_to_grpc(...(action.function)) when the signal is triggered

django_socio_grpc/decorators.py Outdated Show resolved Hide resolved
django_socio_grpc/decorators.py Outdated Show resolved Hide resolved
django_socio_grpc/decorators.py Outdated Show resolved Hide resolved
django_socio_grpc/decorators.py Outdated Show resolved Hide resolved
docs/how-to/use-django-decorators-in-dsg.rst Outdated Show resolved Hide resolved
@legau legau merged commit 954fb56 into master Sep 27, 2024
6 checks passed
@legau legau deleted the feature/addCacheDecorator branch September 27, 2024 07:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants