Here comes some changes of ci framework,with db seperated and the php connection pool

Overview

codeigniter-phpcp

Here comes some changes of ci framework,include db seperated and the php connection pool ...

environment seperate

As the ci defines , there are three enviroments for us , including development | testing | production. So you can find out three more directories in the config directory ,with the names development ,testing, production

development : the config file here enabled while you index.php is set to development enviroment

testing : the config file here enabled while you index.php is set to testing enviroment

production : the config file here enabled while you index.php is set to production enviroment

and files under the root of config directory will enabled while the framework cannot find the file in the enviroment directories

database seperate

You can see that I put a dbmap.php file in each enviroment directory , and this is the most important config for the seperating

 
 * @copyright 	CopyRight 2016
 * @since 		Version 1.0
 * @license
 * @example 
 * 
 * Assuming that your config is like this :
 * 
 * $config['bbcloud'] = array(
 * 		'master' => array('master01','master02'),
 * 		'slave' => array('slave01','slave02')
 * );
 *
 * What you should do is to make sure that your database.php config file has the 
 * master01,master02,slave01,slave02 configurations
 * 
 * 
 */

$config['test1'] = array(
	'master' => array('masterone'),
	'slave' => array('slaveone')
);

$config['test2'] = array(
	'master' => array('mastertwo'),
	'slave' => array('slavetwo')
);

This will be used in the ./application/core/MY_DbConnection.php file , which is the main db connection controller of this function , and you may find the MY_Model.php in the same directory , it will be the parent of the all models instead of the CI_Model and we will mention it latter.

Now let's see an example of using the seperate db connection


 * @copyright 	CopyRight 2016
 * @since 		Version 1.0
 * @license
 */
class Mtest extends MY_Model{

	/**
	 * The relate table name
	 * @var string
	 */
	public $table_name = 'test';

	/**
	 * Default constructor
	 */
	public function __construct(){
		parent::__construct();
	}

	/**
	 * test add data
	 * @param array $data_arr
	 * @return mixed
	 */
	public function add_test($data_arr){
		// return $this->add_data($this->masterTest1,$this->table_name,$data_arr);
		$this->masterTest1->insert($this->table_name,$data_arr);
		return array('affected_rows'=>$this->masterTest1->affected_rows(),'insert_id'=>$this->masterTest1->insert_id());
	}

	/**
	 * test query all data
	 * @return  mixed
	 */
	public function query_all(){
		return $this->slaveTest1->get($this->table_name)->result_array();
	}
}

You may see the code like $this->slaveTest1 and $this->masterTest1 above , as it relates to the dbmap's test1's slave and master , to know more about this function , you should see the config file database.php

 'mysql:host=localhost;dbname=test;charset=utf8',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => 'root',
	'database' => 'test',
	'dbdriver' => 'pdoproxy',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);



$db['mastertwo'] = array(
	'dsn'	=> 'mysql:host=localhost;dbname=test;charset=utf8',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => 'root',
	'database' => 'test',
	'dbdriver' => 'pdoproxy',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);


$db['slaveone'] = array(
	'dsn'	=> 'mysql:host=localhost;dbname=test;charset=utf8',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => 'root',
	'database' => 'test',
	'dbdriver' => 'pdoproxy',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);


$db['slavetwo'] = array(
	'dsn'	=> 'mysql:host=localhost;dbname=test;charset=utf8',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => 'root',
	'database' => 'test',
	'dbdriver' => 'pdoproxy',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);

I think you should be very clearly about the db seperate now , if you cannot understand I would be very guilty .

php pool connection support

As the concurrent volume is more and more important , a lot of way to improve it comes out , like caching or load balancing etc.

Here I just describe the way I got these days , while my boss wanted me to make a db pool like java .

Thanks to github and the developers of php-cp (php connection pool) , I finally got the way

See https://github.com/swoole/php-cp

I clone it from the github and compile it into my php modules , as authors said , I copy the pool.ini to my /etc/ and change the config the same as my dsn in the database.php , and the most important thing is that we should start the pool_server with root

If you want to know more , please go to the github mentioned above, here show my compile process and config :

> /etc/php5/apache2/php.ini && echo "extension=connect_pool.so" >> /etc/php5/cli/php.ini cp pool.ini /etc/ cp pool_server /var/www/html/script ------------------------------------------------------------------------------------------------------ vim /etc/pool.ini [common] log_file = /tmp/phpcp.log ;the num of TCP connections release every idel_time cycles(连接空闲后的回收力度,值越大回收的越快,但是会造成更多的消耗) recycle_num = 2 ;In this period of time,if no process use this connection ,the TCP connection will release(空闲连接回收的发呆时间 单位秒) idel_time = 2 ;;max query package len,exceed will throw exception(最大转发的数据包,超过跑异常5M) max_read_len = 5242880 ;run as daemonize(是否开启守护进程化) daemonize = 1 ;If the num of connection to max, whether to use the queue buffer, set to 0 throw an exception(连接都被占用后,再获取连接是否使用队列缓冲,设置为0直接抛异常) use_wait_queue = 1 ;the get_disable_list() function returns the maximum number of IP(get_disable_list函数最多返回多少个失效结点,防止网络抖动踢掉所有的机器) max_fail_num = 1 ;just the pool_server process port(端口) port = 6253 ;注意数据源需要加 '' ;PDO数据源要与new PDO的第一个参数完全一致(包括顺序) ;如果不配置数据源 那么默认最大是20 最小是1 ,在第一次查询的时候自动创建 ['mysql:host=localhost;dbname=test;charset=utf8'];mysql配置 pool_min = 2 pool_max = 30 ------------------------------------------------------------------------------------------------------ php start /var/www/html/script/pool_server ">
git  clone https://github.com/swoole/php-cp.git
cd php-cp
phpize
./configure
make && make install
echo "extension=connect_pool.so" >> /etc/php5/apache2/php.ini && echo "extension=connect_pool.so" >> /etc/php5/cli/php.ini
cp pool.ini /etc/
cp pool_server /var/www/html/script

------------------------------------------------------------------------------------------------------

vim /etc/pool.ini

[common]
log_file = /tmp/phpcp.log
;the num of TCP connections release every idel_time cycles(连接空闲后的回收力度,值越大回收的越快,但是会造成更多的消耗)
recycle_num = 2
;In this period of time,if no process use this connection ,the TCP connection will release(空闲连接回收的发呆时间 单位秒)
idel_time = 2
;;max query package len,exceed will throw exception(最大转发的数据包,超过跑异常5M)
max_read_len = 5242880
;run as daemonize(是否开启守护进程化)
daemonize = 1
;If the num of connection to max, whether to use the queue buffer, set to 0 throw an exception(连接都被占用后,再获取连接是否使用队列缓冲,设置为0直接抛异常)
use_wait_queue = 1
;the get_disable_list() function returns the maximum number of IP(get_disable_list函数最多返回多少个失效结点,防止网络抖动踢掉所有的机器)
max_fail_num = 1
;just the pool_server process port(端口)
port = 6253

;注意数据源需要加 ''
;PDO数据源要与new PDO的第一个参数完全一致(包括顺序)
;如果不配置数据源 那么默认最大是20 最小是1 ,在第一次查询的时候自动创建
['mysql:host=localhost;dbname=test;charset=utf8'];mysql配置
pool_min = 2
pool_max = 30

------------------------------------------------------------------------------------------------------

php start /var/www/html/script/pool_server

After the server start , you can change the database.php ,modify the dbdriver to pdoproxy , and you can use the normal ci db usage to doing you work , while the proproxy driver doing the release backend .

If you just want the driver , you can get it in the directory ./system/database/drivers/pdoproxy

MY_Model.php - The parent of all Models

MY_Model.php extends the MY_DbConnection , supports the db seperate and supplies some common method for quick queries , but it's not the most important one , you can alse extends MY_DbConnection in you model directly.

RedisServer

You can find the RedisServer.php in the ./application/libraries , which will be the parent of the ./application/models/redis/RedisMain.php , make some common config of the redis , and you can see the redis config in the config directory redis.php

 
 * @copyright 	CopyRight 2016
 * @since 		Version 1.0
 * @license
 * @example 
 * 
 * 
 */

$config['redis'] = array(
	'host' => '',
	'port' => 6379,
	'password' => ''
);

//--------------------------------------------------------------------------------------------------
// 不同环境的前缀配置,目前有开发环境、测试环境和生产环境三种
// 此处不配置生产环境的前缀,主要是为了兼容之前的一些redis键值
//--------------------------------------------------------------------------------------------------
$config['prefix'] = array(
	'development' => 'dev_',
	'testing' => 'test_',
	'production' => ''
);

I will describe the usage later

You might also like...
Leaf is a PHP framework that helps you create clean, simple but powerful web apps and APIs quickly and easily.
Leaf is a PHP framework that helps you create clean, simple but powerful web apps and APIs quickly and easily.

Leaf is a PHP framework that helps you create clean, simple but powerful web apps and APIs quickly and easily. Leaf introduces a cleaner and much simpler structure to the PHP language while maintaining it's flexibility. With a simple structure and a shallow learning curve, it's an excellent way to rapidly build powerful and high performant web apps and APIs.

Symprowire is a PHP MVC Framework based and built on Symfony, using the ProcessWire CMS as DBAL and Service Provider.

Symprowire - PHP MVC Framework for ProcessWire 3.x Symprowire is a PHP MVC Framework based and built on Symfony using ProcessWire 3.x as DBAL and Serv

FlyCubePHP is an MVC Web Framework developed in PHP and repeating the ideology and principles of building WEB applications, embedded in Ruby on Rails.

FlyCubePHP FlyCubePHP is an MVC Web Framework developed in PHP and repeating the ideology and principles of building WEB applications, embedded in Rub

Implementing programming best practices and patterns, and creating a custom PHP framework from scratch.

Implementing programming best practices and patterns, and creating a custom PHP framework from scratch.

An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols. PHP>=5.3.
An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols. PHP=5.3.

Workerman What is it Workerman is an asynchronous event-driven PHP framework with high performance to build fast and scalable network applications. Wo

Kit is a lightweight, high-performance and event-driven web services framework that provides core components such as config, container, http, log and route.

Kit What is it Kit is a lightweight, high-performance and event-driven web services framework that provides core components such as config, container,

Yii 2: The Fast, Secure and Professional PHP Framework
Yii 2: The Fast, Secure and Professional PHP Framework

Yii 2 is a modern framework designed to be a solid foundation for your PHP application. It is fast, secure and efficient and works right out of the bo

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.

Slim Framework Slim is a PHP micro-framework that helps you quickly write simple yet powerful web applications and APIs. Installation It's recommended

A simple, secure, and scalable PHP application framework

Opulence Introduction Opulence is a PHP web application framework that simplifies the difficult parts of creating and maintaining a secure, scalable w

Owner
null
This package provides some basic methods to implement a self updating functionality for your Laravel application. Already bundled are some methods to provide a self-update mechanism via Github or some private repository via http.

This package provides some basic methods to implement a self updating functionality for your Laravel 5 application. Already bundled are some methods to provide a self-update mechanism via Github.

Holger Lösken 311 Dec 31, 2022
A set of shady Slim Framework middlewares that can solve some annoyances...

Shady A set of shady Slim Framework middlewares that can solve some annoyances... What does it contain? Available middlewares: ApacheVirtualHostFix Ur

Jan-Age Laroo 7 Jun 22, 2021
CleverStyle Framework is simple, scalable, fast and secure full-stack PHP framework

CleverStyle Framework is simple, scalable, fast and secure full-stack PHP framework. It is free, Open Source and is distributed under Free Public Lice

Nazar Mokrynskyi 150 Apr 12, 2022
Framework X is a simple and fast micro framework based on PHP

Framework X is a simple and fast micro framework based on PHP. I've created a simple CRUD application to understand how it works. I used twig and I created a custom middleware to handle PUT, DELETE methods.

Mahmut Bayri 6 Oct 14, 2022
Spiral Framework is a High-Performance PHP/Go Full-Stack framework and group of over sixty PSR-compatible components

Spiral HTTP Application Skeleton Spiral Framework is a High-Performance PHP/Go Full-Stack framework and group of over sixty PSR-compatible components.

Spiral Scout 152 Dec 18, 2022
Sunhill Framework is a simple, fast, and powerful PHP App Development Framework

Sunhill Framework is a simple, fast, and powerful PHP App Development Framework that enables you to develop more modern applications by using MVC (Model - View - Controller) pattern.

Mehmet Selcuk Batal 3 Dec 29, 2022
Framework X – the simple and fast micro framework for building reactive web applications that run anywhere.

Framework X Framework X – the simple and fast micro framework for building reactive web applications that run anywhere. Quickstart Documentation Tests

Christian Lück 620 Jan 7, 2023
I made my own simple php framework inspired from laravel framework.

Simple MVC About Since 2019, I started learning the php programming language and have worked on many projects using the php framework. Laravel is one

null 14 Aug 14, 2022
PHPR or PHP Array Framework is a framework highly dependent to an array structure.

this is new repository for php-framework Introduction PHPR or PHP Array Framework is a framework highly dependent to an array structure. PHPR Framewor

Agung Zon Blade 2 Feb 12, 2022
I made my own simple php framework inspired from laravel framework.

Simple MVC About Since 2019, I started learning the php programming language and have worked on many projects using the php framework. Laravel is one

Rizky Alamsyah 14 Aug 14, 2022