GithubHelp home page GithubHelp logo

Comments (6)

Krukov avatar Krukov commented on May 27, 2024 1

@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.

Krukov avatar Krukov commented on May 27, 2024

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.

AIGeneratedUsername avatar AIGeneratedUsername commented on May 27, 2024

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?

  1. Python's functools.cache (functools.lru_cache). It caches functions by default without return or when None is returned.
  2. '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:

  1. Keep the code as is, but document the behavior more explicitly. Put a huge warning in the documentation.
  2. Change the behavior to store None by default. Make cache ignoring optional.

from cashews.

AIGeneratedUsername avatar AIGeneratedUsername commented on May 27, 2024

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.

AIGeneratedUsername avatar AIGeneratedUsername commented on May 27, 2024

@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.

Krukov avatar Krukov commented on May 27, 2024

Thanks - I enabled it, I am not familiar with it -let's try

from cashews.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.