Comments (6)
@AIGeneratedUsername Thanks for you input, I on the same side with you. And you mentioned case where prefer to do not cache None, and I thought that is is more safer behavior. And now I think that I was wrong.
I'll fix it
from cashews.
Thanks !
So there is problem with lack of documentation.
By default cache only store some real
value that is not None
, but you can customize it with
:param condition: callable object that determines whether the result will be saved or not
@cache(ttl="10m", condition=lambda *_, **__: True)
async def get(*args, **kwargs):
or
@cache(ttl="10m", condition=any)
async def get(*args, **kwargs):
With this option you can store None too
from cashews.
I see that 'aiocache' has the same behavior aio-libs/aiocache#452. The problem with 'aiocache' is that it skips caching None
silently, the same as 'cashews' do know. IMHO, it is safer to make this option to be opt-in (not opt-out). Some functions do expensive calculations or/and expensive/slow network calls. Without return
at all or return None
in some cases if no result. Is the any good reason to skip caching by default when a function has no return
or return None
?
So, from one side we have 'aiocache'. Who are on the opposite side?
- Python's
functools.cache
(functools.lru_cache
). It caches functions by default withoutreturn
or whenNone
is returned. - 'diskcache' package has the same behavior as
functools.cache
:
from diskcache import Cache
cache = Cache()
@cache.memoize(expire=10, tag='fib')
def bar():
print("Hello")
bar()
bar() # Will be called only once
cache.clear()
So, there are two options:
- Keep the code as is, but document the behavior more explicitly. Put a huge warning in the documentation.
- Change the behavior to store
None
by default. Make cache ignoring optional.
from cashews.
I think hard to imagine a real situation when someone uses a @cache
decorator, but does not want to cache function if a result is None
. Here is a possible scenario:
@cache(cache_none=False)
def sometimes_expensive_function():
try:
response = request_url("https://")
except NetworkError:
return
Perhaps such default behavior (do not cache None
) makes some sense.
In general, I see functions like
@cache(cache_none=True)
def expensive_function(foo):
# Do very expensive calculation
# If no result or something bad happened, then `return` or `return None`
# Maybe we always have nothing to return
from cashews.
@Krukov By the way, could you please enable GitHub Discussions to avoid issues cluttering is someone will want to ask a question? From my side, I can subscribe to notifications then, and I will try to help if someone will create a new topic.
from cashews.
Thanks - I enabled it, I am not familiar with it -let's try
from cashews.
Related Issues (20)
- Update README with migration notes: simple decorator usage is broken after `NotConfiguredError` introducing HOT 4
- How to cache StreamingResponse/Generator? HOT 26
- Alias/tags for cache keys for invalidation
- Transaction for cache changes
- auto strategy to cache HOT 1
- Disabling cache doesnโt work reliably in tests HOT 3
- Cache `bytes` more efficient HOT 1
- lock performance HOT 4
- Invalid register tag with optional arguments HOT 2
- Not support redis unix socket HOT 2
- Error when delete tags the cache before caching itself HOT 2
- TTL type annotation
- Key template doesnt work correctly with class methods HOT 2
- Get keys number
- Allow cache exceptions aka. "white exceptions" HOT 6
- Current implementation of `thunder_protection` may cause "Future exception was never retrieved" warning HOT 1
- Add More contrib modules
- Pass cache result into ttl function HOT 5
- Timeout and Cancelled errors HOT 6
- Improve basic api with external exceptions in case of any Backend errors
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cashews.