Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper way to handle RedisConnection after sending commands #5

Closed
nidev opened this issue Feb 10, 2017 · 2 comments
Closed

Proper way to handle RedisConnection after sending commands #5

nidev opened this issue Feb 10, 2017 · 2 comments

Comments

@nidev
Copy link

nidev commented Feb 10, 2017

First, thank you for great library that made me keep studying Dart with Redis!

Writing codes for wrapper to use like Map object, I made simple Function to deal with async calls:
(Part of whole code. If you are interested in it, please watch it at here.)

   Function _redisCommander = (redisCommand) async {
      Command command = await _redis.connect(host, port);

      String authenticate = "OK";
      if (password.isNotEmpty) {
        authenticate = await command.send_object(["AUTH", password]);
      }

      if (authenticate == "OK") {
        return command.send_object(redisCommand);
      }
      else {
        throw new Exception("Incorrect password. Redis access is unauthorized.");
      }
    };

What I wonder is, 'close' may be required when 'connect' exists. And I found function 'close' on connection.dart file. I understood it closes socket connection. But there's no example code calling 'close' after operation.

How do I handle RedisConnection properly? Isn't it necessary to handle RedisConnection manually?

@nidev nidev changed the title Proper way to handle RedisCommand after sending commands Proper way to handle RedisConnection after sending commands Feb 10, 2017
@ra1u
Copy link
Owner

ra1u commented Feb 10, 2017

Nice to hear that you find inspiration in this particular libraray.

In general I had in mind that single connection is used among multiple users/clients.
Main idea is that everyone is using same connection. This makes best performance, and requires
single connection.

Alternatively, you can write your own wrapper around connector. That is.
function/class that takes connection argument and computation to execute. Function opens connection
it execute computation and once it is done it closes connection.

Some sketch code.

Future<> RedisConnHadler({String host = "127.0.0.1",
                               int port = 6379, 
                              String password = "", String redisKey = ""}
                              ,var func) {

   //  connect
   conn = new RedisConnection();
   Command command = await  conn.connect(host, port);         
   await auth(command,password);

   // func is expected to have prototype like
   // Future<X> func(Command cmd) or equvalent async
   await func(command);
   await conn.close()
   return Future.value()  // dummy future
}

Use of such interface can go wrong if future returned from func is not last future, that can frequently happen if return is forgotten, but less likely if async is used.

@nidev
Copy link
Author

nidev commented Feb 11, 2017

Thanks for your advice. Now, I think i can make different approach. Still a little bit confused about async/await operation.

I'll close this issue as my concern is cleared.

@nidev nidev closed this as completed Feb 11, 2017
nidev added a commit to nidev/smallservlet that referenced this issue Feb 11, 2017
Single connection gives better performance. Thus, I try to implement RedisCacheDriver to work in same way.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants