question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What problems to solve:

  1. Currently, ioredis supports transforming arguments and replies of commands by calling Redis.Command.setArgumentTransformer and Redis.Command.setReplyTransformer. However, this will apply the transformers to the global namespace that all Redis instances have to apply the transformers, which could be a problem in large applications that have several instances used for different situations and purposes.
  2. 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:closed
  • Created 7 years ago
  • Reactions:10
  • Comments:5

github_iconTop GitHub Comments

2reactions
cuiweiqiangcommented, Oct 26, 2020

Is there any updates for this func ?

1reaction
ozomercommented, Sep 26, 2017

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(...) has getBuffer(...), set(...) has setBuffer(...), hget(...) has hgetBuffer(...) 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, in set the “value” appears in the second parameter, in hset in the third parameter, and in mset the “value” appears in all even parameters. Similarly, get returns a single value to decode and mget returns multiple values to decode.

This solution will allow to dynamically decide in the code whether I want to use get, getBuffer, getSnappy, getMsgpack or getJson, 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found