Skip to content

Commit d3ab571

Browse files
author
Daniel Martin
committed
Made default list null
1 parent 2b87e69 commit d3ab571

4 files changed

Lines changed: 47 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ Implemented basic functionality
55
## 0.0.5
66

77
Implemented function 'getWhere()' which helps retrieving the correct items
8+
9+
## 0.0.6
10+
11+
Made 'getAll()' and 'getWhere()' returning a nun-nullable List<T>. If no item was found, it returns now an empty List<T>
12+
13+
## 0.0.7
14+
15+
Partially reverted the last changes, but "get()" and "getWhere()" returns now null if the cache has never been used.

lib/cache.dart

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Cache<T> {
88
late Duration _duration;
99

1010
/// The cache itself
11-
final List<CacheItem<T>> _cache = [];
11+
List<CacheItem<T>>? _cache;
1212

1313
/// The timer which handles the loop
1414
Timer? _timer;
@@ -19,8 +19,12 @@ class Cache<T> {
1919

2020
/// Gets a cached item with the given key
2121
T? get(String key) {
22+
if (_cache == null) {
23+
return null;
24+
}
25+
2226
final CacheItem<T>? cacheItem =
23-
_cache.firstWhereOrNull((element) => element.key == key);
27+
_cache!.firstWhereOrNull((element) => element.key == key);
2428

2529
// If there is no item cached
2630
if (cacheItem == null) {
@@ -38,15 +42,18 @@ class Cache<T> {
3842

3943
/// Gets all cached items
4044
List<T>? getAll() {
41-
List<T>? list;
45+
if (_cache == null) {
46+
return null;
47+
}
48+
49+
final List<T> list = [];
4250

43-
for (int index = _cache.length - 1; index >= 0; index--) {
44-
final item = _cache[index];
51+
for (int index = _cache!.length - 1; index >= 0; index--) {
52+
final item = _cache![index];
4553

4654
if (_isItemOutdated(item)) {
4755
removeItem(item);
4856
} else {
49-
list ??= [];
5057
list.add(item.item);
5158
}
5259
}
@@ -55,15 +62,18 @@ class Cache<T> {
5562
}
5663

5764
List<T>? getWhere(bool Function(CacheItem<T>) function) {
58-
final items = _cache.where(function);
65+
if (_cache == null) {
66+
return null;
67+
}
68+
69+
final items = _cache!.where(function);
5970

60-
List<T>? list;
71+
final List<T> list = [];
6172

6273
for (final item in items) {
6374
if (_isItemOutdated(item)) {
6475
removeItem(item);
6576
} else {
66-
list ??= [];
6777
list.add(item.item);
6878
}
6979
}
@@ -78,10 +88,13 @@ class Cache<T> {
7888

7989
/// Inserts an item with the given key and duration into the cache
8090
void putWithDuration(String key, T item, Duration duration) {
91+
// Initialize the cache if needed
92+
_cache ??= [];
93+
8194
final endTime = _nowInSeconds() + duration.inSeconds;
8295

8396
final CacheItem<T> cacheItem = CacheItem<T>(key, item, endTime);
84-
_cache.add(cacheItem);
97+
_cache!.add(cacheItem);
8598

8699
// Try to start the timer if not done yet
87100
_tryStartTimer();
@@ -95,12 +108,15 @@ class Cache<T> {
95108
/// Inserts many items into the cache with the given duration
96109
void putManyWithDuration(
97110
List<T> items, String Function(T item) getKey, Duration duration) {
111+
// Initialize the cache if needed
112+
_cache ??= [];
113+
98114
final endTime = _nowInSeconds() + duration.inSeconds;
99115

100116
for (final item in items) {
101117
final key = getKey(item);
102118
final CacheItem<T> cacheItem = CacheItem<T>(key, item, endTime);
103-
_cache.add(cacheItem);
119+
_cache!.add(cacheItem);
104120
}
105121

106122
// Try to start the timer if not done yet
@@ -109,17 +125,17 @@ class Cache<T> {
109125

110126
/// Removes the item with the given key from the cache
111127
void remove(String key) {
112-
_cache.removeWhere((element) => element.key == key);
128+
_cache?.removeWhere((element) => element.key == key);
113129
}
114130

115131
/// Removes the given item from the cache
116132
void removeItem(CacheItem item) {
117-
_cache.remove(item);
133+
_cache?.remove(item);
118134
}
119135

120136
/// Removes all items from the cache
121137
void removeAll() {
122-
_cache.clear();
138+
_cache?.clear();
123139
}
124140

125141
/// Sets a new default duration
@@ -132,15 +148,17 @@ class Cache<T> {
132148
bool _isItemOutdated(CacheItem<T> item) => item.endTime <= _nowInSeconds();
133149

134150
void _tryStartTimer() {
135-
if (_cache.isEmpty || (_timer != null && _timer!.isActive)) {
151+
if (_cache == null ||
152+
_cache!.isEmpty ||
153+
(_timer != null && _timer!.isActive)) {
136154
return;
137155
}
138156

139157
_timer = Timer.periodic(const Duration(seconds: 10), _timerCallBack);
140158
}
141159

142160
void _timerCallBack(Timer timer) {
143-
if (_cache.isEmpty) {
161+
if (_cache == null || _cache!.isEmpty) {
144162
_timer?.cancel();
145163
return;
146164
}

pubspec.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
name: async
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "2.6.1"
10+
version: "2.8.1"
1111
boolean_selector:
1212
dependency: transitive
1313
description:
@@ -28,7 +28,7 @@ packages:
2828
name: charcode
2929
url: "https://pub.dartlang.org"
3030
source: hosted
31-
version: "1.2.0"
31+
version: "1.3.1"
3232
clock:
3333
dependency: transitive
3434
description:
@@ -80,7 +80,7 @@ packages:
8080
name: meta
8181
url: "https://pub.dartlang.org"
8282
source: hosted
83-
version: "1.3.0"
83+
version: "1.7.0"
8484
path:
8585
dependency: transitive
8686
description:
@@ -134,7 +134,7 @@ packages:
134134
name: test_api
135135
url: "https://pub.dartlang.org"
136136
source: hosted
137-
version: "0.3.0"
137+
version: "0.4.2"
138138
typed_data:
139139
dependency: transitive
140140
description:

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: temp_cache
22
description: A package which provides functionality to store objects inside a temporary cache inside memory
3-
version: 0.0.5
3+
version: 0.0.7
44
homepage: https://github.com/DirtyNative/temp_cache
55

66
environment:

0 commit comments

Comments
 (0)