上一篇:
Lysine介绍(五)MVC - View
进入Model环节之前先介绍一下Lysine对各式存储服务的管理
最常见的存储方式就是关系式数据库了,除此之外还有MongoDB等等新型数据库,这些数据库都提供持久化存储服务
缓存服务其实也是存储服务,比如Memcached,缓存是非持久化存储服务
在Lysine里面,无论常见的关系式数据库,新型的NOSQL数据库,Memcached缓存服务,都统统等同看做存储服务,统一管理
<?php
require '/path/to/lysine/core.php';
$config = array(
'storage' => array(
'pool' => array(
'mdb' => array(
'class' => 'Lysine\Storage\Mongo',
'server' => '127.0.0.1'
),
'pgdb' => array(
'class' => 'Lysine\Storage\DB\Adapter\Pgsql',
'dsn' => 'pgsql:host=127.0.0.1 dbname=test',
'user' => 'dev',
'pass' => 'abc'
),
'mydb' => array(
'class' => 'Lysine\Storage\DB\Adapter\Mysql',
'dsn' => 'mysql:host=127.0.0.1 dbname=test',
'user' => 'dev',
'pass' => 'abc',
'options' => array(
\PDO::ATTR_EMULATE_PREPARES => true
),
),
'my_cache' => array(
'class' => 'Lysine\Storage\Cache\Memcached',
'life_time' => 3600,
'server' => array('127.0.0.1', 11211),
),
),
),
);
Lysine\Config::import($config);
$pool = Lysine\Storage\Pool::instance();
// 对mongodb的连接实例
$mongo_connect = $pool->get('mdb');
// 对mysql的连接实例
$mysql_connect = $pool->get('mydb');
// 对pgsql的连接实例,通过__invoke()魔法方法
$pgsql_connect = $pool('pgdb');
// 对memcached的连接实例,使用预定义函数
$my_cache = storage('my_cache');
Lysine\Storage\Pool类的作用就是管理所有的存储服务,根据名字获得对应的实例
所有的Storage类都继承自Lysine\IStorage接口,这个接口保证所有的Storage子类的构造函数都使用数组风格,然后Pool才能够统一根据数组配置信息实例化类,也就是说,如果要想把自己开发的storage类放到Pool里面管理,也必须继承IStorage接口
除了根据名字拿到对应的连接之外,Pool还提供了自定义路由的方法
假设需要把用户数据水平切分到三台数据库内,用最简单的用id值对3取余数的方式切分
<?php
require '/path/to/lysine/core.php';
$config = array(
'storage' => array(
'pool' => array(
'user0' => array(
'class' => 'Lysine\Storage\DB\Adapter\Pgsql',
'dsn' => 'pgsql:host=192.168.1.2 dbname=users',
'user' => 'dev',
'pass' => 'abc',
),
'user1' => array(
'class' => 'Lysine\Storage\DB\Adapter\Pgsql',
'dsn' => 'pgsql:host=192.168.1.3 dbname=users',
'user' => 'dev',
'pass' => 'abc',
),
'user2' => array(
'class' => 'Lysine\Storage\DB\Adapter\Pgsql',
'dsn' => 'pgsql:host=192.168.1.4 dbname=users',
'user' => 'dev',
'pass' => 'abc',
),
),
),
);
Lysine\Config::import($config);
$pool = Lysine\Storage\Pool::instance();
$pool->setDispatcher('user', function($id) {
return 'user'. ($id % 3);
});
// user1
$db = $pool->get('user', 1);
// user2
$db = $pool->get('user', 2);
// user0
$db = $pool->get('user', 3);
使用Pool setDispatcher()方法,你可以设定自己的路由方式,根据不同的参数连接不同的数据库
再来一个复杂点的,使用一致性哈希算法切分
<?php
// 一个一致性hash实现 http://github.com/pda/flexihash
require_once '/path/to/flexihash/include/init.php';
require_once '/path/to/lysine/core.php';
use Lysine\Config;
$config = array(
'storage' => array(
'pool' => array(
'user1' => array(
'class' => 'Lysine\Storage\DB\Adapter\Pgsql',
'dsn' => 'pgsql:host=127.0.0.1 dbname=users1',
'user' => 'dev',
'pass' => 'abc',
),
'user2' => array(
'class' => 'Lysine\Storage\DB\Adapter\Pgsql',
'dsn' => 'pgsql:host=127.0.0.1 dbname=users2',
'user' => 'dev',
'pass' => 'abc',
),
'user3' => array(
'class' => 'Lysine\Storage\DB\Adapter\Pgsql',
'dsn' => 'pgsql:host=127.0.0.1 dbname=users3',
'user' => 'dev',
'pass' => 'abc',
),
'user4' => array(
'class' => 'Lysine\Storage\DB\Adapter\Pgsql',
'dsn' => 'pgsql:host=127.0.0.1 dbname=users4',
'user' => 'dev',
'pass' => 'abc',
),
'user5' => array(
'class' => 'Lysine\Storage\DB\Adapter\Pgsql',
'dsn' => 'pgsql:host=127.0.0.1 dbname=users5',
'user' => 'dev',
'pass' => 'abc',
),
'topic1' => array(
'class' => 'Lysine\Storage\DB\Adapter\Mysql',
'dsn' => 'mysql:host=127.0.0.1 dbname=topic1',
'user' => 'dev',
'pass' => 'abc',
),
'topic2' => array(
'class' => 'Lysine\Storage\DB\Adapter\Mysql',
'dsn' => 'mysql:host=127.0.0.1 dbname=topic2',
'user' => 'dev',
'pass' => 'abc',
),
'topic3' => array(
'class' => 'Lysine\Storage\DB\Adapter\Mysql',
'dsn' => 'mysql:host=127.0.0.1 dbname=topic3',
'user' => 'dev',
'pass' => 'abc',
),
),
),
);
Config::import($config);
$user_hash = new Flexihash();
$topic_hash = new Flexihash();
foreach (Config::get('storage', 'pool') as $node_name) {
if (substr($node_name, 0, 4) == 'user') {
$user_hash->addTarget($node_name);
} elseif (substr($node_name, 0, 5) == 'topic') {
$topic_hash->addTarget($node_name);
}
}
$pool = Lysine\Storage\Pool::instance();
// user数据以id来分区
$pool->setDispatcher('user', array($user_hash, 'lookup'));
// 或者
$pool->setDispatcher('user', function($user_id) use ($user_hash) {
return $user_hash->lookup($user_id);
});
// topic数据以user id来分区
$pool->setDispatcher('topic', array($topichash, 'lookup'));
// 或者
$pool->setDispatcher('topic', function($user_id) use ($topic_hash) {
return $topic_hash->lookup($user_id);
});
Pool除了可以方便的获得存储服务连接之外,还有两个目的:
- 数据的垂直切分机制,通过名字获得对应连接
- 数据的水平切分机制,通过路由获得对应连接
接下来会介绍内置的两种ORM封装:ActiveRecord和DataMapper,它们都内置了垂直切分机制
分享到:
相关推荐
Lysine2, 用于restfulweb应用的PHP框架 Lysine不再更新,新框架在 https://github.com/yeaha/owl基于PHP 5.4新特性,命名空间及特性rest式controller,以资源的方式组织web app,通过HTTP标准
网络技术-网络基础
多组学联合,泛素化与蛋白质组学PPI需求对应文献——2016.01-SAHA再来一篇文章.pdf,Quantitative Analysis of ...Proteome and Lysine Acetylome Reveal the Differential Impacts of VPA and SAHA on HL60 Cells
Diethylenetriaminepentaacetic dianhydride was co-polymerized with dodecyl ester and benzyl ester of lysine , and octodecyl ester and benzyl ester of lysine respectively to give two terpolymers ....
pH值对赖氨酸保护作用影响的研究,贺洪,刘慧敏,用紫外吸收研究力竭运动后大鼠血清中赖氨酸(lysine)对丙二醛(MDA)的清除。通过试管反应发现,在适宜的浓度和pH值下,赖氨酸能与M
键电负性均衡方法的浮动电荷分子力场( ABEEMσπ/ MM),对重组人纤溶酶原 Kringle 1结构域 ( K1Pg)与配体ε-Aminocaproic acid ( EACA),trans-4-( Aminomethyl) cyclohexane-1-carboxylic acid ( AMCHA),L-Lysine( ...
Biodegradable multilayer films of alternating poly- L-lysine(PLL) and deoxyribonucleic acid ( DNA) layers were fabricated onto quartz substrates by the layer-by-layer( LbL) self-assembly meth- od ....
Motivation: Protein ubiquitination is one of the important post-translational modifications by attaching ubiquitin to specific lysine (K) residues in target proteins, and plays important regulatory ...
Lys: Lysine; Met: Methonine; MG: Methylguanidine; m-I: myo-Inositol; NAG: N-acetyl glycoprotein signals; OAG: O-acetyl glycoprotein signals; PC: Phosphocholine; Phe: Phenylalanine; Py: Pyruvate; Suc:...
2.3.3 Lysine Chemistry – Carbamylation . . . . . . . . . . . . . . . . . . . . . . . . . .20 2.4 Multiplexed Approaches to Proteomics . . . . . . . . . . . . . . . . . . . . . . . . . . . .22 2.5 ...