GithubHelp home page GithubHelp logo

Implement set_many function about cashews HOT 10 CLOSED

krukov avatar krukov commented on June 6, 2024
Implement set_many function

from cashews.

Comments (10)

AIGeneratedUsername avatar AIGeneratedUsername commented on June 6, 2024 2

I am +1 on this.
I think it makes sense to add such a function, because now in my code I need to inherit from cashews and define missing functionality on my side:

   async def multi_set(self, pairs: Mapping[AnyKeyT, EncodableT], *, ttl: int | None = None) -> None:
        async with self._client.pipeline(transaction=True) as pipe:
            pairs = {self._build_key(k): v for k, v in pairs.items()}  # type: ignore[misc]
            await pipe.mset(pairs)  # type: ignore[arg-type]
            if ttl:
                for key in pairs.keys():
                    await pipe.expire(key, ttl)
            await pipe.execute()

from cashews.

Krukov avatar Krukov commented on June 6, 2024

Hi there.

Yep, totally agree with set many, but I have some doubts about the decorator.
A first call it will set multi keys. It is easy, but what will happens if I call decorated function many times? Will It return cached results?

from cashews.

yashh avatar yashh commented on June 6, 2024

Thats right. The first call with set the multi keys while the subsequent would return from cache. The decorator would compute which of the keys are a hit, then compute the missing keys and then call the inner fn with the rest, set the cache and return.

Anyway set_many would be sufficient.

from cashews.

Krukov avatar Krukov commented on June 6, 2024

Got it.
You are welcome to make a PR even with the decorator. I will be glad

from cashews.

yashh avatar yashh commented on June 6, 2024

master...yashh:cashews:master

Could you guide me a bit here. I have a WIP set_many working. Since I am not using the self._with_middlewares function set_many is not created backend._client attribute yet. If I call get / get_many that will initiate the connection everything works as expected. Am I on the right path?

from cashews.

Krukov avatar Krukov commented on June 6, 2024

You are right about with_middlewares, but you are not on a right way. The problem that Wrapper - it a place to work with many backends and execute middewares - it is a some kid of proxy class for others "backends". And there are a few Backends: redis, memory and disk - they all are implementation of Backend interface. So I think that you should define a new method set_many as a part of the backend interface . And all magic with a pipeline should be only in the Redis backend

from cashews.

yashh avatar yashh commented on June 6, 2024

Take #2.
master...yashh:cashews:master

So I implemented set_many in redis, disk & memory backends. I see that redis-cli monitor is getting the right commands. However when I read the keys back with get_many right after set_many I get (None, None). I know redis is properly returning the right data. However

            _values = await self._with_middlewares("get_many", _keys[0])(*_keys, default=default)
            result.update(dict(zip(_keys, _values)))```

_values is (None, None). Any idea what it could be?

from cashews.

Krukov avatar Krukov commented on June 6, 2024

@yashh
You on right way, LGFM. I think that the problem with Nones related to a pickle serialization.To solve it you need to implement set_many in PickleSerializerMixin and PickleSerializerMixin shouldn't know about Redis ). And to be honest I don't now how exactly you can implement it.

from cashews.

yashh avatar yashh commented on June 6, 2024
    async def set_many(self, pairs: Dict[str, Any], *args: Any, **kwargs: Any):
        data = {}
        for key, value in pairs.items():
            transformed_value = None
            if isinstance(value, int) and not isinstance(value, bool):
                transformed_value = pairs[key]
            else:
                transformed_value = self._pickler.dumps(value)
            data[key] = self._prepend_sign_to_value(key, transformed_value)
        return await super().set_many(data, *args, **kwargs)

Great. I implemented the set_many on the PickleSerializer and now get_many works! I was able to store strings, objects and other types.

from cashews.

Krukov avatar Krukov commented on June 6, 2024

Great ! Waiting for your PR )

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.