12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <?php
- namespace App\Common;
- trait DBTrait
- {
- public static $db = null;
- public static $redis = null;
- public static $mongo = null;
- public static $db_config = [
- 'host' => '192.168.10.10',
- 'port' => 3306,
- 'user' => 'homestead',
- 'password' => 'secret',
- 'db_name' => 'weilaibike'
- ];
- public static $redis_config = [
- 'host' => '127.0.0.1',
- 'port' => 6379,
- 'password' => '',
- 'database' => 0
- ];
- public static $mongodb_config = [
- 'url' => 'mongodb://114.116.38.6',
- 'database' => 'weilaibike'
- ];
- public static function initDataBase($config = [1, 2, 3])
- {
- if (in_array(1, $config)) {
- self::$db_config = Config['db'];
- self::$db = new \Workerman\MySQL\Connection(self::$db_config['host'], self::$db_config['port'], self::$db_config['user'], self::$db_config['password'], self::$db_config['db_name']);
- }
- //
- if (in_array(2, $config)) {
- self::$redis_config = Config['redis'];
- self::$redis = new \Redis();
- self::$redis->pconnect(self::$redis_config['host'], self::$redis_config['port']);
- if (!empty(self::$redis_config['password'])) {
- self::$redis->auth(self::$redis_config['password']);
- }
- self::$redis->select(self::$redis_config['database']);
- }
- //
- // if (in_array(3, $config)) {
- // self::$mongodb_config = Config['mongodb'];
- // self::$mongo = (new MongoDB\Client(self::$mongodb_config['url']))->selectDatabase(self::$mongodb_config['database']);
- // }
- }
- }
|