Skip to content

CN Store Server United PK

himulawang edited this page Oct 22, 2014 · 1 revision

Store Server United PK

United PK同United Model与United List的运行机理有些不同。

由于MariaDB中无法原子自增,因此在I Dart中仅将MariaDB作为Backup,自增主要在Redis中完成。

backupStep这个选项作为备份的步长。当该值为5时,Redis中的Key值每到达0 5 10 15...任意5的倍数时,向MariaDB中Update一次,作为备份。

当Redis数据丢失时。get和incr会自动将MariaDB中的key值,回写到Redis中去。

orm.dart配置

Map orm = {
    'User': {
        'PK': {
            'className': 'UserPK',
        },
        'PKStore': {
            'backupStep': 5, // 0 means do not use backup
            'storeOrder': [
                {
                    'type': 'redis',
                    'readWriteSeparate': false,
                    'shardMethod': 'NONE',
                    'master': 'SingleCache',
                    'slave': 'SingleCacheSlave',
                    'abb': 'u',
                },
                {
                    'type': 'mariaDB',
                    'readWriteSeparate': false,
                    'shardMethod': 'NONE',
                    'master': 'SingleDB',
                    'slave': 'SingleDBSlave',
                    'abb': 'u',
                    'table': 'PK',
                },
            ],
        },
    },
};

store.dart配置

Map store = {
    'redis': {
        'SingleCache': [
            {'no': 0, 'host': 'localhost', 'port': 6383, 'pwd': null, 'db': '0'},
        ],
        'SingleCacheSlave': [
            {'no': 0, 'host': 'localhost', 'port': 6384, 'pwd': null, 'db': '0'},
        ],
    },
    'mariaDB': {
        'SingleDB': [
            {'no': 0, 'host': 'localhost', 'port': 3306, 'usr': 'root', 'pwd': '123', 'db': 'i_dart', 'maxHandler': 5 },
        ],
        'SingleDBSlave': [
            {'no': 0, 'host': 'localhost', 'port': 3306, 'usr': 'root', 'pwd': '123', 'db': 'i_dart', 'maxHandler': 5 },
        ],
    },
};

使用如下代码进行部署

import 'i_config/deploy.dart';
import 'i_config/orm.dart';
import '../bin/i_maker/lib_i_maker.dart';

void main() {
  IModelMaker modelMaker = new IModelMaker(deploy, orm);
  modelMaker.make();

  IStoreMaker storeMaker = new IStoreMaker(deploy, orm);
  storeMaker.makeServer();

  IUtilMaker utilMaker = new IUtilMaker(deploy);
  utilMaker.make();

  ILibraryMaker libMaker = new ILibraryMaker(deploy);
  libMaker.makeServer();
}

上述代码会在store文件夹下生成3个文件user_pk_rdb_store.dart,user_pk_mdb_store.dart,user_pk_store.dart。

使用方法如下:

main.dart

import 'lib_test.dart';

main() {
  // 初始化pk,在Redis中新建一条Key为u-pk的数据
  UserPKRedisStore store = new UserPKRedisStore();

  // incr()接口为原子操作,可安全的作为全局自增键
  store.incr()
  .then((UserPK pk) {
    print(pk.get()); // 1

    return store.incr();
  })
  .then((UserPK pk) {
    print(pk.get()); // 2
  });
}