What problems to solve:
- Currently, ioredis supports transforming arguments and replies of commands by calling
Redis.Command.setArgumentTransformer
andRedis.Command.setReplyTransformer
. However, this will apply the transformers to the global namespace that allRedis
instances have to apply the transformers, which could be a problem in large applications that have several instances used for different situations and purposes. - What’s more, transformers are not flexible enough in some situations (see https://github.com/luin/ioredis/issues/319).
How to solve the problems:
Async hooks is a good way to solves these problems. Two types of hooks can be specified when creating new Redis
instances: beforeSend
and afterFetch
, which will be invoked before sending commands to the Redis server and after any replies being fetched. A sample syntax:
var redis = new Redis({
hooks: {
beforeSend(command, args) {
if (command.name === 'set') {
return compress(args[1]).then(ret => args[1] = ret);
}
},
afterFetch(command, replies) {
if (command.name === 'get') {
return uncompress(replies[0]).then(ret => replies[0] = ret);
}
}
}
});
Since all transformers can be implemented with hooks, transformers will be removed in the next major version.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:10
- Comments:5
Top Results From Across the Web
hooks - Amazon.com
15 Pcs Black Wall Mounted Coat Hooks, Hanger Hook with 30 Pieces Screws for Hanging Hat, Towel, Key, Robe, Coats, Scarf, Bag, Cap,...
Read more >Hooks - Storage & Organization - The Home Depot
Get free shipping on qualified Hooks products or Buy Online Pick Up in Store today in the Storage & Organization Department.
Read more >Hooks in Hardware - Walmart.com
Shop for Hooks in Hardware. Buy products such as BirdRock Home Wall-Mounted Hook Rails, 2 Count at Walmart and save.
Read more >Wall Hooks & Hook Racks - Target
Shop Target for Wall Hooks & Hook Racks you will love at great low prices. Free shipping on orders of $35+ or same-day...
Read more >Introducing Hooks - React
Hooks are a new addition in React 16.8. ... This new function useState is the first “Hook” we'll learn about, but this example...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Is there any updates for this func ?
I would like to suggest a different feature that could fix the same problem: dynamically overloading the api with more functions. The current api already provides a “Buffer” api for each function:
get(...)
hasgetBuffer(...)
,set(...)
hassetBuffer(...)
,hget(...)
hashgetBuffer(...)
etc. These functions allow to pack/unpack the value in two different ways: string (default) and buffer. I would like to be able to call something like:Redis.Command.setDynamicTransformer('Snappy', function decoder(compressedValue) { return uncompress(compressedValue); }, function encoder(uncompressedValue) { return compress(uncompresedValue); })
, that will add a third way to pack/unpack the values:getSnappy(...)
,setSnappy(...)
. The encoder transformer should operate on the “values” of the function, which appear in different positions in each command - for example, inset
the “value” appears in the second parameter, inhset
in the third parameter, and inmset
the “value” appears in all even parameters. Similarly,get
returns a single value to decode andmget
returns multiple values to decode.This solution will allow to dynamically decide in the code whether I want to use
get
,getBuffer
,getSnappy
,getMsgpack
orgetJson
, instead of writing the transformations in one place and having to adapt it to each command by yourself.P.S. It would also be great to be able to make the encoder/decoder functions return a Promise.