/** * Запуск скрипта * * @param array &$ServiceManager объект сервис-менеджера */ function run(array &$ServiceManager) { $MysqlFactory =& ServiceManager\getFactory($ServiceManager, 'Mysql'); $shardsConfig = MysqlFactory\getShardsConfig($MysqlFactory); // Заказчики $CustomerRepository =& ServiceManager\get($ServiceManager, 'CustomerRepository'); $maxCustomerId = 1; foreach ($shardsConfig[CustomerRepository\SHARD_CONFIG] as $shardId => $_) { $Mysql = MysqlFactory\createShard($MysqlFactory, CustomerRepository\SHARD_CONFIG, $shardId); $data = Mysql\query($Mysql, 'SELECT * FROM ' . CustomerRepository\DATABASE_NAME . '.' . CustomerRepository\TABLE_NAME); while (($customerData = Mysql\fetchAssoc($Mysql, $data)) !== false) { $Customer =& Customer\unserializeFromMysql($customerData); CustomerRepository\saveToMemcached($CustomerRepository, $Customer); CustomerRepository\savePasswordHashToMemcached($CustomerRepository, $Customer, $customerData['password_hash']); $maxCustomerId = max($maxCustomerId, Customer\getId($Customer)); } } CustomerRepository\syncLastCustomerId($CustomerRepository, $maxCustomerId); // Исполнители $ExecutorRepository =& ServiceManager\get($ServiceManager, 'ExecutorRepository'); $maxExecutorId = 1; foreach ($shardsConfig[ExecutorRepository\SHARD_CONFIG] as $shardId => $_) { $Mysql =& MysqlFactory\createShard($MysqlFactory, ExecutorRepository\SHARD_CONFIG, $shardId); $data = Mysql\query($Mysql, 'SELECT * FROM ' . ExecutorRepository\TABLE_NAME . '.' . ExecutorRepository\TABLE_NAME); while (($executorData = Mysql\fetchAssoc($Mysql, $data)) !== false) { $Executor =& Executor\unserializeFromMysql($executorData); ExecutorRepository\saveLoginToMemcached($ExecutorRepository, Executor\getLogin($Executor), Executor\getId($Executor)); ExecutorRepository\savePasswordHashToMemcached($ExecutorRepository, $Executor, $executorData['password_hash']); $maxExecutorId = max($maxExecutorId, Executor\getId($Executor)); } } ExecutorRepository\syncLastExecutorId($ExecutorRepository, $maxExecutorId); // Заказы $OrderRepository =& ServiceManager\get($ServiceManager, 'OrderRepository'); foreach (OrderRepository\fetchAll($OrderRepository) as $Order) { OrderRepository\savePriceToMemcached($OrderRepository, Order\getPrice($Order)); } }
/** * @param array &$ExecutorRepository объект репозитория исполнителя * @param string $login логин исполнителя * * @return &array|null объект исполнителя или null, если таковой не найден */ function &fetch(array &$ExecutorRepository, $login) { $Memcached =& getMemcached($ExecutorRepository); $id = Memcached\get($Memcached, createMemcachedKeyForLogin($login)); if ($id === false) { // нет такого исполнителя return null; } $id = (int) $id; $Mysql =& MysqlFactory\createShard(getMysqlFactory($ExecutorRepository), SHARD_CONFIG, getShardId($id)); $result = Mysql\query($Mysql, 'SELECT id, fio, login, salary FROM ' . DATABASE_NAME . '.' . TABLE_NAME . ' WHERE id = ' . (int) $id); if (Mysql\numRows($result) == 0) { return null; } return Executor\unserializeFromMysql(Mysql\fetchAssoc($Mysql, $result)); }