Comments (10)
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.
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.
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.
Got it.
You are welcome to make a PR even with the decorator. I will be glad
from cashews.
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.
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.
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.
@yashh
You on right way, LGFM. I think that the problem with None
s 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.
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.
Great ! Waiting for your PR )
from cashews.
Related Issues (20)
- 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
- Utilizing Functions as Key Generators HOT 3
- [Migration] [User experience] An exception must be raised if `cache.setup()` was not called HOT 2
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.