如何解决存储缓存慢的问题?

时间:2019-04-11 11:07:16

标签: sqlite dart flutter flutter-dependencies

响应时间为18秒,但存储到缓存时间为3分钟。这是为什么?我使用了 sqflite插件来存储数据。 这是我的函数调用服务器,带有要缓存的插入内容

storeWoDescription(String url,String token) async {
  final response = await http.get(
    '${url}/v1.0/WoDescription',
    headers: {'Authorization': 'Bearer ${token}'},);
  final jsonResponse = json.decode(response.body);
  WoDescription model = WoDescription.fromJson(jsonResponse);
  int length = model.data.length;

  for(int i=0; i<length; i++) {
    var data = DataWoDescription(
      i: model.data[i].i,
      d: model.data[i].d,
      e: model.data[i].e,
      w: model.data[i].w,
      a: model.data[i].a,
      r: model.data[i].r,
      t: model.data[i].t,
      du: model.data[i].du,
      s: model.data[i].s,
      ra: model.data[i].ra,
      cul: model.data[i].cul,
    );
    await HelperDefCatMaster().insertWoDescription(data);
  }
}

点击按钮后的调用方式如下:

   await storeWoDescription(_url,tokens);

HelperDefCatMaster 这是我的数据库提供商。我在一个数据库中创建了多个表。在插入之前,我删除了数据库,然后插入。

class HelperDefCatMaster {
  static final HelperDefCatMaster _instance = HelperDefCatMaster.internal();

  factory HelperDefCatMaster() => _instance;
  static Database _db;

  Future<Database> get db1 async {
    if (_db != null) return _db;
    _db = await initDb();
    return _db;
  }

  HelperDefCatMaster.internal();

  initDb() async {
    io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
    String path = join(documentsDirectory.path, "HelperDefCatMasterDB.db");
    var theDb = await openDatabase(path, version: 1, onCreate: _onCreate);
    return theDb;
  }

  void _onCreate(Database db, int version) async {
    await db.execute(
        """CREATE TABLE WoDescriptionTable(i INTEGER, d STRING, e INTEGER, w STRING, a INTEGER, r INTEGER, t INTEGER, du DOUBLE, s INTEGER, ra INTEGER, cul INTEGER)""");
  }

  Future<void> insertWoDescription(DataWoDescription assetregister) async {
    var db = await db1;
    await db.insert(
      'WoDescriptionTable',
      assetregister.toMap()
     // conflictAlgorithm: ConflictAlgorithm.replace,
    );
  }


  Future<void> deleteWoDescription() async {
    var db = await db1;
    await db.delete('WoDescriptionTable');
  }



  Future<List<DataWoDescription>> displayWoDescription() async {
    var db = await db1;
    final List<Map<String, dynamic>> maps = await db.query('WoDescriptionTable');

    return List.generate(maps.length, (i) {
      return DataWoDescription(
        i: maps[i]['i'],
        d: '${maps[i]['d']}',
        e: maps[i]['e'],
        w: '${maps[i]['w']}',
        a: maps[i]['a'],
        r: maps[i]['r'],
        t: maps[i]['t'],
        du: maps[i]['du'],
        s: maps[i]['s'],
        ra: maps[i]['ra'],
        cul: maps[i]['cul'],
      );
    });
  }
}

0 个答案:

没有答案