redis_pool
简介
redis客户端连接池,用来方便的维护redis客户端连接
组件接口
redis_pool.gs
函数原型 | 函数作用 |
---|---|
void give_connection(object c) | 将通过take_connection获取的redis客户端连接对象返还到连接池中 |
object take_connection(string db) | 从连接池中获取一个指定名称数据库的redis客户端连接 |
void set_db_config(string db, map db_config) | 设置一个数据库的连接信息 |
mixed get_db_config(string db) | 获取一个数据库的连接配置信息 |
void set_cache_size(int cache_size) | 设置所有连接缓冲池的大小 |
void show_all_connections() | 调试接口, 显示所有连接信息 |
类
CoroutineRedisConnectionCounter
协程当前持有的redis连接计数器
成员变量
变量名 | 类型 | 初始值 | 须初始化 | 描述 |
---|---|---|---|---|
ref_dict | map | nil | 可选 | 持有的redis连接信息 key: 数据库名称 value: redis连接信息(ReidsConnection实例) |
成员方法
函数原型 | 函数作用 |
---|---|
RedisConnection find_connection_by_name(string name) | 根据数据库名称找到对应的redis连接信息 |
array find_connection_by_object(object ob) | 根据redis客户端对象找到对应的数据库名称和redis连接信息 |
void add_connection(string name, RedisConnection connection) | 添加一个redis连接信息 |
void remove_connection(string name) | 移除一个指定数据库名称的redis连接信息 |
RedisConnection inc_ref_count(string name) | 增加指定数据库名称的redis连接信息的引用计数 |
void dec_ref_count(string name, function drop_func = nil) | 减少redis客户端对象对应的redis连接信息的引用计数 |
RedisConnection
redis连接信息
成员变量
变量名 | 类型 | 初始值 | 须初始化 | 描述 |
---|---|---|---|---|
conn | object | nil | 可选 | redis客户端对象(pkg.hiredis.async_client实例) |
ping | int | 0 | 可选 | 最近一次ping时间 |
active | int | 0 | 可选 | 最近一次活跃时间 |
ref_count | int | 0 | 可选 | 引用计数 |
holder | int | 0 | 可选 | 当前持有的协程ID |
成员方法
函数原型 | 函数作用 |
---|---|
bool is_hold_by(coroutine co) | 判断是否被指定协程持有 |
int inc_ref_count(int count) | 增加引用计数 |
void reset_ref_count() | 重置引用计数 |
bool keep_alive() | 检测连接活性 |
RedisConnectionPool
redis客户端连接池
成员变量
变量名 | 类型 | 初始值 | 须初始化 | 描述 |
---|---|---|---|---|
name | string | nil | 可选 | 连接池名称 |
list | array | nil | 可选 | 连接列表 |
cache_size | int | 0 | 可选 | 连接池容量大小 |
成员方法
函数原型 | 函数作用 |
---|---|
void set_cache_size(int cache_size) | 设置连接池缓存数量大小 |
RedisConnection pop_front() | 从连接池中取出一个连接 |
void push_back(RedisConnection connection) | 将连接放入连接池中 |
样例
hiredis.setup_redis({
"thread_num" : 1,
});
map db_config = {
"driver" : "redis",
"host" : "localhost",
"port" : 6379,
"passwd" : "",
"db_index" : 1,
};
redis_pool.set_db_config("testdb", db_config);
object conn = redis_pool=>take_connection("testdb");
defer redis_pool=>give_connection(conn);
printf("conn=%M\n", conn);