sewenew / redis-plus-plus

Redis client written in C++
Apache License 2.0
1.6k stars 347 forks source link

[QUESTION] Approach for application code generically supporting Redis or RedisCluster #586

Closed CJLove closed 2 weeks ago

CJLove commented 1 month ago

Describe the problem Thanks for a great library! As the README points out, the RedisCluster interface is very similar to the Redis interface with a few exceptions. That being said, these are two distinct concrete classes with no shared base, so application code using redis-plus-plus would need to choose one or the other. Are there any creative approaches folks have used to decouple application code from the Redis deployment architecture being used?

Thanks!

Environment:

Additional context Add any other context about the problem here.

sewenew commented 1 month ago

As you mentioned, Redis and RedisCluster interfaces are very similar, however, you cannot simply replace one for another. Since they have different behaviors, especially on multiple keys command.

Normally, if you are sure that you only need the common parts of Redis and RedisCluster interfaces, you can use template to make your application code works for both cases.

template <typename RedisInstance>
class App {
public:
  App(RedisInstance &&instance) : _instance(instance) {}

  void set(const string_view &key, const string_view &val) {
    _instance.set(key, value);
  }
private:
  RedisInstance _instance;
}

The RedLock and test cases use this pattern to make them work with both Redis and RedisCluster.

Regards