You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, i've been using this library and i find it very useful and simple at the same time. the @cache decorator is a charm.
i know that probably is not the way i'm actually using the @cache is not the way its meant to be used, but i think that this can be useful for anyone...
In my application the api routers are just api end-points, the logic behind each api call is inside a "service" actually those service are objects with methods to solve any task.
Think as every api group has it's own router, it's own service - object, and a method for every end-point.
At First i've started to use the @cache decorator to decorate the router functions, but than i decided to try to decorate the service-methods... This choice came since some of those methods use other methods to make calcs or handling a dataset, and there are lot of cases where i have to call the same method several times with same parameters producing the same results, so caching that result is proven to be much faster than recalculate it every time.
I've been able to use @cache decorator that way , i create a key with a custom key-builder, the key is composed with the object - method name and the passed parameters.
At this point everything seamed to work fine, but i've noticed a couple of things that maybe can be addressed and improved.
When retrieving a result from the cache, the result is a python dict, but when the result it's calculated by the method in my case it's a pydantic object ... i've solved it by checking the result type and in case of a dict i parse it with .parse_obj to obtain the pydantic obj.
Is it possible to force the cache to return the pydantic object in any case?
An other issue i've noticed ( and i think this came from the improper use of the @cache ), sometimes the kwargs are missing during the method calls.
i'll try to describe the scenario
A1 api uses methodA1.
Inside methodA1, there's a call to methodB1 ( wich is an other service)
methodB1 is called with its parameters
Inside methodB1 there are 2 other calls for methodB2, and methodB3. these are separate tasks, which give they own results.
methodB1 params are passed to methodB2 and methodB3.
methodB1 B2 B3 are @cache decorated so they result are saved into cache with their keys.
The parameters are passed to the methods as expected, but B2 and B3 keys don't have any parameters, as if no parameters are passed.
I think that this problem can occur also if for example we have a normal router end-point decorated with @cache, which calls other apis, to fetch some data, and those apis call other apis aswell...
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello, i've been using this library and i find it very useful and simple at the same time. the @cache decorator is a charm.
i know that probably is not the way i'm actually using the @cache is not the way its meant to be used, but i think that this can be useful for anyone...
In my application the api routers are just api end-points, the logic behind each api call is inside a "service" actually those service are objects with methods to solve any task.
Think as every api group has it's own router, it's own service - object, and a method for every end-point.
At First i've started to use the @cache decorator to decorate the router functions, but than i decided to try to decorate the service-methods... This choice came since some of those methods use other methods to make calcs or handling a dataset, and there are lot of cases where i have to call the same method several times with same parameters producing the same results, so caching that result is proven to be much faster than recalculate it every time.
I've been able to use @cache decorator that way , i create a key with a custom key-builder, the key is composed with the object - method name and the passed parameters.
At this point everything seamed to work fine, but i've noticed a couple of things that maybe can be addressed and improved.
When retrieving a result from the cache, the result is a python dict, but when the result it's calculated by the method in my case it's a pydantic object ... i've solved it by checking the result type and in case of a dict i parse it with .parse_obj to obtain the pydantic obj.
Is it possible to force the cache to return the pydantic object in any case?
An other issue i've noticed ( and i think this came from the improper use of the @cache ), sometimes the kwargs are missing during the method calls.
i'll try to describe the scenario
A1 api uses methodA1.
Inside methodA1, there's a call to methodB1 ( wich is an other service)
methodB1 is called with its parameters
Inside methodB1 there are 2 other calls for methodB2, and methodB3. these are separate tasks, which give they own results.
methodB1 params are passed to methodB2 and methodB3.
methodB1 B2 B3 are @cache decorated so they result are saved into cache with their keys.
The parameters are passed to the methods as expected, but B2 and B3 keys don't have any parameters, as if no parameters are passed.
I think that this problem can occur also if for example we have a normal router end-point decorated with @cache, which calls other apis, to fetch some data, and those apis call other apis aswell...
Beta Was this translation helpful? Give feedback.
All reactions