Skip to content

Update cache_object_provider.dart #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 38 additions & 26 deletions flutter_cache_manager/lib/src/storage/cache_object_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,28 @@ class CacheObjectProvider implements CacheInfoRepository {
Future open() async {
db = await openDatabase(path, version: 3,
onCreate: (Database db, int version) async {
await db.execute('''
create table $_tableCacheObject (
${CacheObject.columnId} integer primary key,
${CacheObject.columnUrl} text,
${CacheObject.columnKey} text,
${CacheObject.columnPath} text,
${CacheObject.columnETag} text,
${CacheObject.columnValidTill} integer,
${CacheObject.columnTouched} integer,
${CacheObject.columnLength} integer
);
create unique index $_tableCacheObject${CacheObject.columnKey}
ON $_tableCacheObject (${CacheObject.columnKey});
''');
db.transaction((txn) async {
return await txn.execute('''
create table $_tableCacheObject (
${CacheObject.columnId} integer primary key,
${CacheObject.columnUrl} text,
${CacheObject.columnKey} text,
${CacheObject.columnPath} text,
${CacheObject.columnETag} text,
${CacheObject.columnValidTill} integer,
${CacheObject.columnTouched} integer,
${CacheObject.columnLength} integer
);
create unique index $_tableCacheObject${CacheObject.columnKey}
ON $_tableCacheObject (${CacheObject.columnKey});
''');
});
}, onUpgrade: (Database db, int oldVersion, int newVersion) async {
// Migration for adding the optional key, does the following:
// Adds the new column
// Creates a unique index for the column
// Migrates over any existing URLs to keys
if (oldVersion <= 1) {

await db.transaction((txn) async {
await txn.execute('''
alter table $_tableCacheObject
Expand All @@ -52,10 +53,12 @@ class CacheObjectProvider implements CacheInfoRepository {
});
}
if (oldVersion <= 2) {
await db.execute('''
alter table $_tableCacheObject
add ${CacheObject.columnLength} integer;
''');
await db.transaction((txn) async {
return await txn.execute('''
alter table $_tableCacheObject
add ${CacheObject.columnLength} integer;
''');
});
}
});
}
Expand All @@ -71,7 +74,9 @@ class CacheObjectProvider implements CacheInfoRepository {

@override
Future<CacheObject> insert(CacheObject cacheObject) async {
var id = await db.insert(_tableCacheObject, cacheObject.toMap());
var id = await db.transaction((txn) async {
return await txn.insert(_tableCacheObject, cacheObject.toMap());
});
return cacheObject.copyWith(id: id);
}

Expand All @@ -87,20 +92,26 @@ class CacheObjectProvider implements CacheInfoRepository {

@override
Future<int> delete(int id) {
return db.delete(_tableCacheObject,
where: '${CacheObject.columnId} = ?', whereArgs: [id]);
return db.transaction((txn) async {
return txn.delete(_tableCacheObject,
where: '${CacheObject.columnId} = ?', whereArgs: [id]);
});
}

@override
Future deleteAll(Iterable<int> ids) {
return db.delete(_tableCacheObject,
where: '${CacheObject.columnId} IN (' + ids.join(',') + ')');
return db.transaction((txn) async {
return txn.delete(_tableCacheObject,
where: '${CacheObject.columnId} IN (' + ids.join(',') + ')');
});
}

@override
Future<int> update(CacheObject cacheObject) {
return db.update(_tableCacheObject, cacheObject.toMap(),
where: '${CacheObject.columnId} = ?', whereArgs: [cacheObject.id]);
return db.transaction((txn) async {
return txn.update(_tableCacheObject, cacheObject.toMap(),
where: '${CacheObject.columnId} = ?', whereArgs: [cacheObject.id]);
});
}

@override
Expand Down Expand Up @@ -139,3 +150,4 @@ class CacheObjectProvider implements CacheInfoRepository {
@override
Future close() => db.close();
}