更新完上一篇博客的时候恰逢十一黄金周,回家休息了一会。现在补充DBPool的Redis连接池。
我刚开始写mysql_connnection_pool的时候,用的是map这个数据结构来保存连接,每次从池当中取出连接的时候都需要遍历一边map,根据map的第二个参数的bool值来判断是否可用。这样的话效率很低,所以我在这里改用list来保存。每次取出连接的时候都pop_front出来,没次释放连接的时候都push_back回去。这样就不用遍历了,更有效率。同时,我还使用了shared_ptr来管理redis_obj的指针,让delete和我的程序say goodbye。
在redis_obj.h当中加上了:
typedef boost::shared_ptr<RedisObj>RedisObjPtr;
这个池和mysql_connnection_pool很像,同样也是从config里面读取database.xml或者database.json配置文件,从配置文件当中获取到host等等。
当然了,为了线程安全,使用unique_lock来上锁。
// Copyright
// License
// Author: adairjun
// This is used to construct a redis connection pool
#ifndef DBPOOL_INCLUDE_REDIS_CONNECTION_POOL_H_
#define DBPOOL_INCLUDE_REDIS_CONNECTION_POOL_H_
#include <string.h>
#include <mutex>
#include <list>
#include "redis_obj.h"
using std::string;
using std</