@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <hiredis/hiredis.h>
|
||||
|
||||
#include <condition_variable>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class RedisPool {
|
||||
public:
|
||||
RedisPool(std::string url, std::size_t n);
|
||||
~RedisPool();
|
||||
|
||||
RedisPool(const RedisPool&) = delete;
|
||||
RedisPool& operator=(const RedisPool&) = delete;
|
||||
|
||||
class Lease {
|
||||
public:
|
||||
explicit Lease(RedisPool& pool);
|
||||
~Lease();
|
||||
Lease(Lease&& other) noexcept;
|
||||
Lease& operator=(Lease&&) = delete;
|
||||
Lease(const Lease&) = delete;
|
||||
Lease& operator=(const Lease&) = delete;
|
||||
|
||||
redisContext* ctx() const { return ctx_; }
|
||||
|
||||
private:
|
||||
RedisPool* pool_;
|
||||
redisContext* ctx_;
|
||||
};
|
||||
|
||||
Lease acquire();
|
||||
|
||||
bool ping();
|
||||
|
||||
private:
|
||||
friend class Lease;
|
||||
|
||||
struct RedisDeleter {
|
||||
void operator()(redisContext* c) const noexcept {
|
||||
if (c != nullptr) ::redisFree(c);
|
||||
}
|
||||
};
|
||||
using ConnPtr = std::unique_ptr<redisContext, RedisDeleter>;
|
||||
|
||||
redisContext* take();
|
||||
void give_back(redisContext* c);
|
||||
|
||||
std::string url_;
|
||||
std::vector<ConnPtr> all_;
|
||||
std::vector<redisContext*> idle_;
|
||||
std::mutex mu_;
|
||||
std::condition_variable cv_;
|
||||
bool shutting_down_ = false;
|
||||
};
|
||||
Reference in New Issue
Block a user