Skip to content

Commit 8c1ff71

Browse files
author
Daniel Martin
committed
Implemented lint, added example
1 parent 4d30763 commit 8c1ff71

File tree

7 files changed

+70
-49
lines changed

7 files changed

+70
-49
lines changed

.vscode/launch.json

Lines changed: 0 additions & 19 deletions
This file was deleted.

analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include: package:lint/analysis_options_package.yaml

example/main.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'package:temp_cache/cache.dart';
2+
3+
void main(List<String> args) {
4+
final cache = Cache<TestItem>();
5+
final item1 = TestItem('1');
6+
final item2 = TestItem('2');
7+
final item3 = TestItem('3');
8+
9+
// put one
10+
cache.put(item1.id, item1);
11+
12+
// put many
13+
final List<TestItem> items = [item2, item3];
14+
cache.putMany(items, (item) => item.id);
15+
16+
// get one
17+
// ignore: unused_local_variable
18+
final i1 = cache.get('1');
19+
20+
// get all
21+
// ignore: unused_local_variable
22+
final retrievedList = cache.getAll();
23+
}
24+
25+
class TestItem {
26+
late final String id;
27+
late final bool isTrue;
28+
29+
TestItem(this.id);
30+
}

lib/cache.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Cache<T> {
1414
Timer? _timer;
1515

1616
Cache({Duration? duration}) {
17-
_duration = duration ?? Duration(minutes: 60);
17+
_duration = duration ?? const Duration(minutes: 60);
1818
}
1919

2020
/// Gets a cached item with the given key
@@ -41,7 +41,7 @@ class Cache<T> {
4141
List<T>? list;
4242

4343
for (int index = _cache.length - 1; index >= 0; index--) {
44-
var item = _cache[index];
44+
final item = _cache[index];
4545

4646
if (_isItemOutdated(item)) {
4747
removeItem(item);
@@ -61,7 +61,7 @@ class Cache<T> {
6161

6262
/// Inserts an item with the given key and duration into the cache
6363
void putWithDuration(String key, T item, Duration duration) {
64-
var endTime = _nowInSeconds() + duration.inSeconds;
64+
final endTime = _nowInSeconds() + duration.inSeconds;
6565

6666
final CacheItem<T> cacheItem = CacheItem<T>(key, item, endTime);
6767
_cache.add(cacheItem);
@@ -78,13 +78,13 @@ class Cache<T> {
7878
/// Inserts many items into the cache with the given duration
7979
void putManyWithDuration(
8080
List<T> items, String Function(T item) getKey, Duration duration) {
81-
var endTime = _nowInSeconds() + duration.inSeconds;
81+
final endTime = _nowInSeconds() + duration.inSeconds;
8282

83-
items.forEach((item) {
84-
var key = getKey(item);
83+
for (final item in items) {
84+
final key = getKey(item);
8585
final CacheItem<T> cacheItem = CacheItem<T>(key, item, endTime);
8686
_cache.add(cacheItem);
87-
});
87+
}
8888

8989
// Try to start the timer if not done yet
9090
_tryStartTimer();
@@ -106,6 +106,7 @@ class Cache<T> {
106106
}
107107

108108
/// Sets a new default duration
109+
// ignore: avoid_setters_without_getters
109110
set duration(Duration dur) {
110111
_duration = dur;
111112
}
@@ -118,7 +119,7 @@ class Cache<T> {
118119
return;
119120
}
120121

121-
_timer = Timer.periodic(Duration(seconds: 10), _timerCallBack);
122+
_timer = Timer.periodic(const Duration(seconds: 10), _timerCallBack);
122123
}
123124

124125
void _timerCallBack(Timer timer) {

pubspec.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ packages:
6060
description: flutter
6161
source: sdk
6262
version: "0.0.0"
63+
lint:
64+
dependency: "direct dev"
65+
description:
66+
name: lint
67+
url: "https://pub.dartlang.org"
68+
source: hosted
69+
version: "1.6.0"
6370
matcher:
6471
dependency: transitive
6572
description:
@@ -143,5 +150,5 @@ packages:
143150
source: hosted
144151
version: "2.1.0"
145152
sdks:
146-
dart: ">=2.12.0 <3.0.0"
153+
dart: ">=2.13.0 <3.0.0"
147154
flutter: ">=1.17.0"

pubspec.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
name: temp_cache
22
description: A package which provides functionality to store objects inside a temporary cache inside memory
3-
version: 0.0.1
3+
version: 0.0.4
44
homepage: https://github.com/DirtyNative/temp_cache
55

66
environment:
77
sdk: ">=2.12.0 <3.0.0"
88
flutter: ">=1.17.0"
99

1010
dependencies:
11+
collection: ^1.15.0
1112
flutter:
1213
sdk: flutter
13-
collection: ^1.15.0
14-
14+
1515
dev_dependencies:
1616
flutter_test:
1717
sdk: flutter
1818

19+
lint:
20+
1921
flutter:

test/temp_cache_test.dart

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import 'package:temp_cache/temp_cache.dart';
33

44
void main() {
55
test('Cache single', () {
6-
var cache = Cache<TestItem>();
6+
final cache = Cache<TestItem>();
77

8-
final item1 = TestItem('1', true);
9-
final item2 = TestItem('2', true);
10-
final item3 = TestItem('3', false);
8+
final item1 = TestItem('1');
9+
final item2 = TestItem('2');
10+
final item3 = TestItem('3');
1111

1212
cache.put(item1.id, item1);
1313
cache.put(item2.id, item2);
@@ -19,11 +19,11 @@ void main() {
1919
});
2020

2121
test('Cache multiple', () {
22-
var cache = Cache<TestItem>();
22+
final cache = Cache<TestItem>();
2323

24-
final item1 = TestItem('1', true);
25-
final item2 = TestItem('2', true);
26-
final item3 = TestItem('3', false);
24+
final item1 = TestItem('1');
25+
final item2 = TestItem('2');
26+
final item3 = TestItem('3');
2727

2828
final List<TestItem> items = [item1, item2, item3];
2929

@@ -35,11 +35,11 @@ void main() {
3535
});
3636

3737
test('Get all', () {
38-
var cache = Cache<TestItem>();
38+
final cache = Cache<TestItem>();
3939

40-
final item1 = TestItem('1', true);
41-
final item2 = TestItem('2', true);
42-
final item3 = TestItem('3', false);
40+
final item1 = TestItem('1');
41+
final item2 = TestItem('2');
42+
final item3 = TestItem('3');
4343

4444
final List<TestItem> items = [item1, item2, item3];
4545

@@ -49,11 +49,11 @@ void main() {
4949
});
5050

5151
test('Remove item', () {
52-
var cache = Cache<TestItem>();
52+
final cache = Cache<TestItem>();
5353

54-
final item1 = TestItem('1', true);
55-
final item2 = TestItem('2', true);
56-
final item3 = TestItem('3', false);
54+
final item1 = TestItem('1');
55+
final item2 = TestItem('2');
56+
final item3 = TestItem('3');
5757

5858
final List<TestItem> items = [item1, item2, item3];
5959

@@ -67,7 +67,6 @@ void main() {
6767

6868
class TestItem {
6969
late final String id;
70-
late final bool isTrue;
7170

72-
TestItem(this.id, this.isTrue);
71+
TestItem(this.id);
7372
}

0 commit comments

Comments
 (0)