Skip to content

CN Store Server Redis List

himulawang edited this page Oct 22, 2014 · 3 revisions

Store Server Redis List

orm.dart配置

Map orm = {
    'User': {
        'Model': {
            'pk': [0],
            'column': [
                'groupId',
                'id',
                'name',
                'email',
                'country',
                'city',
            ],
            'toAddFilter': [],
            'toSetFilter': [],
            'toFullFilter': [],
            'toAbbFilter': [],
            'toListFilter': [],
        },
        'List': {
            'className': 'UserList',
            'pk': [0],
            'childPK': [1],
        },
        'ListStore': {
            'storeOrder': [
                {
                    'type': 'redis',
                    'readWriteSeparate': false,
                    'sharding': true,
                    'shardMethod': 'CRC32', // CRC32 & Consistent Hashing
                    'master': 'GameCache',
                    'slave': 'GameCacheSlave',
                    'expire': 86400,
                    'mode': 'Atom', // TODO 'Atom' mode use hash type store model, 'Block' mode compress model to string type
                    'abb': 'u',
                },
            ],
        },
    },
};

store.dart配置

Map store = {
    'redis': {
        'GameCache': [
            {'no': 0, 'host': 'localhost', 'port': 6379, 'pwd': null, 'db': '0'},
            {'no': 1, 'host': 'localhost', 'port': 6380, 'pwd': null, 'db': '0'},
        ],
        'GameCacheSlave': [
            {'no': 0, 'host': 'localhost', 'port': 6381, 'pwd': null, 'db': '0'},
            {'no': 1, 'host': 'localhost', 'port': 6382, 'pwd': null, 'db': '0'},
        ],
    },
};

使用如下代码进行部署

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目录下生成user_list_rdb_store.dart

使用方法如下:

main.dart

import 'lib_test.dart';

main() {
  User user1 = new User([7, 1, 'ila', 'ila[at]ilaempire.com', 'Japan', 'Tokyo']);
  User user2 = new User([7, 2, 'Dya', 'dya[at]ilaempire.com', 'Japan', 'Kyoto']);

  // 假定7 是devTeam的GroupId
  UserList devTeam = new UserList(7);

  devTeam.add(user1);
  devTeam.add(user2);

  // 初始化store
  UserListRedisStore store = new UserListRedisStore(7);
  store.set(devTeam)
  .then((_)) {
    print('Set list done.');
  });

  // 上述代码会将devTeam的数据转换成Redis的命令:
  // hmset u_c-7-1 gi 7 i 1 n ila e ila[at]ilaempire.com c Japan c1 Tokyo
  // expire u_c-7-1 86400
  // sadd u_l-7 u_c-7-1

  // hmset u_c-7-2 gi 7 i 2 n Dya e dya[at]ilaempire.com c Japan c1 Kyoto
  // expire u_c-7-2 86400
  // sadd u_l-7 u_c-7-2

  // expire u_l-7 86400

  // 从Redis中获取devTeam
  UserListRedisStore store = new UserListRedisStore(7);

  // 转换成Redis命令:
  // smember u_l-7
  // hmget u_c-7-1 gi i n e c c1
  // hmget u_c-7-2 gi i n e c c1
  store.get(7)
  .then((UserList devTeam) {
    User user1 = devTeam.get(1);
    print(devTeam.toFull());
    /*
    {
      {"groupId":7,"id":1,"name":"ila","email":"ila[at]ilaempire.com","country":"Japan","city":"Tokyo"},
      {"groupId":7,"id":2,"name":"Dya","email":"dya[at]ilaempire.com","country":"Japan","city":"Kyoto"},
    }
    */
  });
}