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

API sugar for common depend_on_row patterns #3

Open
lenaqr opened this issue Nov 13, 2015 · 4 comments
Open

API sugar for common depend_on_row patterns #3

lenaqr opened this issue Nov 13, 2015 · 4 comments

Comments

@lenaqr
Copy link
Owner

lenaqr commented Nov 13, 2015

The idea would be to reduce the amount of lambdas you have to write. Currently there already exists

depend_on_row(Model, 'selector')
->
get_or_create_token(('selector',))
depend_on_row(Model, lambda instance: {'selector': instance})

A useful one would be:

depend_on_row(Model, {'arg1': 'attr1', 'arg2': 'attr2'})
->
get_or_create_token(('arg1', 'arg2'))
depend_on_row(Model, lambda instance: {'arg1': instance.attr1, 'arg2': instance.attr2})

Another one that might be slightly trickier is depend_on_cache:

depend_on_cache(cache, {'arg1': 'arg2', 'arg3': 'arg4'})
->
get_or_create_token(('arg1', 'arg3'))
depend_on_cache(cache, lambda arg2=wildcard, arg4=wildcard, **kwargs: {'arg1': arg2, 'arg3': arg4})

More generally, could also support using lambdas as per-key selectors:

depend_on_row(Model, {'arg1': f1, 'arg2': f2})
->
get_or_create_token(('arg1', 'arg2'))
depend_on_row(Model, lambda instance: {'arg1': f1(instance), 'arg2': f2(instance)})

This would be useful in the case where the per-key selectors are simple functions, like operator.methodcaller('something'), and also has the benefit of being able to create the tokens automatically ahead of time.

@lenaqr lenaqr changed the title Syntactic sugar for common depend_on_row patterns API sugar for common depend_on_row patterns Nov 14, 2015
@benjaminjkraft
Copy link

Another nice one would be to have something for "expire this cache for all arguments if the model matches this predicate". This could probably replace a lot of unnecessarily broad uses of depend_on_model -- e.g. a lot of the uses of depend_on_model(Tag) in the ESP website actually only depend on tags with a specific name.

@lenaqr
Copy link
Owner Author

lenaqr commented Nov 25, 2015

That actually exists, it's the filter parameter to depend_on_row and friends.

@lenaqr
Copy link
Owner Author

lenaqr commented Nov 25, 2015

(But it could also be useful to have shortcuts for defining those without lambdas too -- maybe passing in a QuerySet-like object.)

@benjaminjkraft
Copy link

Ah, TIL.

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

No branches or pull requests

2 participants