redis3m  1.0.0
 All Classes Functions Variables Enumerations Pages
connection.h
1 // Copyright (c) 2014 Luca Marturana. All rights reserved.
2 // Licensed under Apache 2.0, see LICENSE for details
3 
4 #pragma once
5 
6 #include <string>
7 #include <redis3m/utils/exception.h>
8 #include <redis3m/reply.h>
9 #include <vector>
10 #include <boost/shared_ptr.hpp>
11 #include <boost/noncopyable.hpp>
12 #include <boost/assign/list_of.hpp>
13 
14 struct redisContext;
15 
16 namespace redis3m {
17  REDIS3M_EXCEPTION(unable_to_connect)
18  REDIS3M_EXCEPTION(transport_failure)
19 
20  // Utility to build commands, an alias of boost::assign::list_of<std::string>
21  // increases readability of code
22  extern boost::assign_detail::generic_list<std::string>(&command)(const std::string&);
23 
27  class connection: boost::noncopyable
28  {
29  public:
30  typedef boost::shared_ptr<connection> ptr_t;
31 
38  inline static ptr_t create(const std::string& host="localhost",
39  const unsigned int port=6379)
40  {
41  return ptr_t(new connection(host, port));
42  }
43 
44  ~connection();
45 
46  bool is_valid();
47 
52  void append(const std::vector<std::string>& args);
53 
58  reply get_reply();
59 
66  std::vector<reply> get_replies(unsigned int count);
67 
73  inline reply run(const std::vector<std::string>& args)
74  {
75  append(args);
76  return get_reply();
77  }
78 
85  inline redisContext* c_ptr() { return c; }
86 
87  enum role_t {
88  ANY = 0,
89  MASTER = 1,
90  SLAVE = 2
91  };
92 
93  private:
94  friend class connection_pool;
95  connection(const std::string& host, const unsigned int port);
96 
97  role_t _role;
98  redisContext *c;
99  };
100 }
static ptr_t create(const std::string &host="localhost", const unsigned int port=6379)
Create and open a new connection.
Definition: connection.h:38
reply run(const std::vector< std::string > &args)
Utility to call append and then get_reply together.
Definition: connection.h:73
The connection class, represent a connection to a Redis server.
Definition: connection.h:27
Represent a reply received from redis server.
Definition: reply.h:18
redisContext * c_ptr()
Returns raw ptr to hiredis library connection. Use it with caution and pay attention on memory manage...
Definition: connection.h:85