Skip to content

Commit

Permalink
Update UUID dependency and synchronized
Browse files Browse the repository at this point in the history
Improved locking mechanism for instance initialization
  • Loading branch information
renefloor committed Aug 30, 2018
1 parent 2adfc0f commit 7286b78
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .flutter-plugins
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
path_provider=/Users/rene/.pub-cache/hosted/pub.dartlang.org/path_provider-0.4.0/
shared_preferences=/Users/rene/.pub-cache/hosted/pub.dartlang.org/shared_preferences-0.4.0/
path_provider=/Users/rene/.pub-cache/hosted/pub.dartlang.org/path_provider-0.4.1/
shared_preferences=/Users/rene/.pub-cache/hosted/pub.dartlang.org/shared_preferences-0.4.2/
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [0.1.2] - 2018-08-30

* Fixed library compatibility issue
* Improved some synchronization

## [0.1.1] - 2018-04-27

* Fixed some issues when file could not be downloaded the first time it is trying to be retrieved.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.flutter.plugins;

import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.pathprovider.PathProviderPlugin;
import io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin;

/**
* Generated file. Do not edit.
*/
public final class GeneratedPluginRegistrant {
public static void registerWith(PluginRegistry registry) {
if (alreadyRegisteredWith(registry)) {
return;
}
PathProviderPlugin.registerWith(registry.registrarFor("io.flutter.plugins.pathprovider.PathProviderPlugin"));
SharedPreferencesPlugin.registerWith(registry.registrarFor("io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin"));
}

private static boolean alreadyRegisteredWith(PluginRegistry registry) {
final String key = GeneratedPluginRegistrant.class.getCanonicalName();
if (registry.hasPlugin(key)) {
return true;
}
registry.registrarFor(key);
return false;
}
}
3 changes: 3 additions & 0 deletions android/local.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sdk.dir=/Users/rene/Library/Android/sdk
flutter.sdk=/Users/rene/flutter
flutter.versionName=0.1.2
2 changes: 2 additions & 0 deletions ios/Flutter/Generated.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ FLUTTER_BUILD_MODE=debug
FLUTTER_BUILD_DIR=build
SYMROOT=${SOURCE_ROOT}/../build/ios
FLUTTER_FRAMEWORK_DIR=/Users/rene/flutter/bin/cache/artifacts/engine/ios
FLUTTER_BUILD_NAME=0.1.2
PREVIEW_DART_2=true
27 changes: 15 additions & 12 deletions lib/flutter_cache_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ class CacheManager {
static bool showDebugLogs = false;

static CacheManager _instance;

static Future<CacheManager> getInstance() async {
if (_instance == null) {
await synchronized(_lock, () async {
await _lock.synchronized(() async {
if (_instance == null) {
_instance = new CacheManager._();
await _instance._init();
// keep local instance till it is fully initialized
var newInstance = new CacheManager._();
await newInstance._init();
_instance = newInstance;
}
});
}
Expand All @@ -41,18 +44,18 @@ class CacheManager {
Map<String, CacheObject> _cacheData;
DateTime lastCacheClean;

static Object _lock = new Object();
static Lock _lock = new Lock();

///Shared preferences is used to keep track of the information about the files
_init() async {
Future _init() async {
_prefs = await SharedPreferences.getInstance();
_getSavedCacheDataFromPreferences();
_getLastCleanTimestampFromPreferences();
}

bool _isStoringData = false;
bool _shouldStoreDataAgain = false;
Object _storeLock = new Object();
Lock _storeLock = new Lock();

_getSavedCacheDataFromPreferences() {
//get saved cache data from shared prefs
Expand All @@ -79,7 +82,7 @@ class CacheManager {
}

Future<bool> _canSave() async {
return await synchronized(_storeLock, () {
return await _storeLock.synchronized(() {
if (_isStoringData) {
_shouldStoreDataAgain = true;
return false;
Expand All @@ -90,7 +93,7 @@ class CacheManager {
}

Future<bool> _shouldSaveAgain() async {
return await synchronized(_storeLock, () {
return await _storeLock.synchronized(() {
if (_shouldStoreDataAgain) {
_shouldStoreDataAgain = false;
return true;
Expand All @@ -103,7 +106,7 @@ class CacheManager {
_saveDataInPrefs() async {
Map json = new Map();

await synchronized(_lock, () {
await _lock.synchronized(() {
_cacheData.forEach((key, cache) {
json[key] = cache?.toMap();
});
Expand Down Expand Up @@ -133,7 +136,7 @@ class CacheManager {
if (force ||
sinceLastClean > inBetweenCleans ||
_cacheData.length > maxNrOfCacheObjects) {
await synchronized(_lock, () async {
await _lock.synchronized(() async {
await _removeOldObjectsFromCache();
await _shrinkLargeCache();

Expand Down Expand Up @@ -188,15 +191,15 @@ class CacheManager {
String log = "[Flutter Cache Manager] Loading $url";

if (!_cacheData.containsKey(url)) {
await synchronized(_lock, () {
await _lock.synchronized(() {
if (!_cacheData.containsKey(url)) {
_cacheData[url] = new CacheObject(url);
}
});
}

var cacheObject = _cacheData[url];
await synchronized(cacheObject.lock, () async {
await cacheObject.lock.synchronized(() async {
// Set touched date to show that this object is being used recently
cacheObject.touch();

Expand Down
7 changes: 4 additions & 3 deletions lib/src/cache_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:async';
import 'dart:io';

import 'package:path_provider/path_provider.dart';
import 'package:synchronized/synchronized.dart';
import 'package:uuid/uuid.dart';

///Cache information of one file
Expand Down Expand Up @@ -47,15 +48,15 @@ class CacheObject {
DateTime touched;
String url;

Object lock;
Lock lock;
Map _map;

CacheObject(String url, {this.lock}) {
this.url = url;
_map = new Map();
touch();
if (lock == null) {
lock = new Object();
lock = new Lock();
}
}

Expand All @@ -69,7 +70,7 @@ class CacheObject {
touch();
}
if (lock == null) {
lock = new Object();
lock = new Lock();
}
}

Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_cache_manager
description: Generic cache manager for flutter
version: 0.1.1
version: 0.1.2
author: Rene Floor <pub@renefloor.nl>
homepage: https://github.com/renefloor/flutter_cache_manager

Expand All @@ -12,6 +12,6 @@ dependencies:
sdk: flutter
shared_preferences: "^0.4.0"
path_provider: "^0.4.0"
synchronized: "^1.3.0"
uuid: "^0.5.3"
synchronized: "^1.5.1"
uuid: "^1.0.3"
http: "^0.11.3+14"

1 comment on commit 7286b78

@Cretezy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for finally updating.

android/local.properties and .flutter-plugins should be ignored and removed.

Please sign in to comment.