GithubHelp home page GithubHelp logo

Minor code clean up about cashews HOT 7 CLOSED

krukov avatar krukov commented on June 7, 2024
Minor code clean up

from cashews.

Comments (7)

Krukov avatar Krukov commented on June 7, 2024 1

Thanks for all your suggestions and contribution

We need to check repr because we it will raise an error if structure of object have been changed. As we use pickle we can have some strange errors with cache - for example we have structure with some fields and we store it in cache

@dataclasses.dataclass
class Schema:
    name: str


async def func():
    await cache.set("key", Schema("test"))

Than we change the schema

@dataclasses.dataclass
class Schema:
    first_name: str
    last_name: str

And without checking repl we will have an error

async def func():
    a = await cache.get("key")
    print(a.first_name)
    
>>> AttributeError: 'Schema' object has no attribute 'first_name'

https://github.com/Krukov/cashews#cache-invalidation-on-code-change

Regarding mget , I prefer to return immutable structures. I think a list comprehension in this case looks too heavy and hard to maintain and troubleshoot.

from cashews.

Krukov avatar Krukov commented on June 7, 2024 1

@AIGeneratedUsername Can I ask you for review this PR ?

from cashews.

Krukov avatar Krukov commented on June 7, 2024 1

Yep, thank to point it. I hope that is no doubts about socket_keepalive - the same setting we have in our browsers in it set to true.
To be honest there is no reason why socket_timeout is exactly 0.1 by default. I just want to set it with a small amount based on my experience. We had an issues with cache when it stacked and we got the whole app down by the end just because timeout had a big value or didn't set. I don't think that we can count the value for socket timeout, but for cache it should be extremely small just because of the purpose of cache.

I saw some redis benchmarks and they said that average response time is 200 us ( 0.0002 sec) ! and 100 percentile took less then 3 ms ( 0.003 sec) (https://redis.io/topics/benchmarks) !

Do you have some suggestions for that value?

from cashews.

AIGeneratedUsername avatar AIGeneratedUsername commented on June 7, 2024

@Krukov Could you please clarify the purpose of checking __repr__ of unpickled data?

        if self._check_repr:
            repr(value)

It will be great to have a comment in the code for such not obvious things.


Why do you convert to tuple? This seems to be redundant because mget returns a list.

    async def get_many(self, *keys):
        values = []
        for key, value in zip(keys, await super().get_many(*keys) or [None] * len(keys)):
            values.append(await self._get_value(value, key))

        return tuple(values)

By the way, we can write a list comprehension instead:

return [await self._get_value(value, key) for key, value in zip(keys, await super().get_many(*keys) or [None] * len(keys))]

from cashews.

Krukov avatar Krukov commented on June 7, 2024

Regarding the code changes:

  1. we support aioredis 1.3.x and it have this strange mget signature https://aioredis.readthedocs.io/en/v1.3.0/mixins.html#aioredis.commands.StringCommandsMixin.mget
  2. good point
  3. I don't think that is bad , we just hide converting from seconds to millisecond because you can't use floating seconds with ex arg of set cmd with redis (https://redis.io/commands/set)
await cache.set("key", "value", expire=10)  # will set ttl to 10 seconds with redis cmd "SET key value EX 10"
await cache.set("key", "value", expire=10.0)  # will also set ttl to 10 seconds "SET key value PX 10000"
  1. I don't remember why I did it but you right
  2. Thanks

from cashews.

AIGeneratedUsername avatar AIGeneratedUsername commented on June 7, 2024

By the way, it may be great to find how to make type hinting to be IDE-friendly.

2022-02-08_09-53

Perhaps the only way is to add type hints everywhere, but it is too much work to do from the scratch for the whole package.

from cashews.

AIGeneratedUsername avatar AIGeneratedUsername commented on June 7, 2024

@Krukov Could you please let know in a few words what is the background behind:

            kwargs.setdefault("socket_keepalive", True)
            kwargs.setdefault("socket_timeout", 0.1)

Why are exactly these values used? Why timeout is 0.1 and not 0.2, or 0.3, etc.
So if someone will decide to contribute, everybody will be on the same page about specific decisions under the hood.

I know the settings definition, but I am not sure about choosing the best values. I do not mean that the current values are bad or wrong.

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.