Skip to content

Commit 2d757cc

Browse files
committed
feat: JsoCacheLocalStorage
Closes #29
1 parent 811fd84 commit 2d757cc

File tree

8 files changed

+173
-11
lines changed

8 files changed

+173
-11
lines changed

CHANGELOG.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- JsonCacheLocalStorage: implementation on top of the localstorage package —
13+
[29](https://github.com/dartoos-dev/json_cache/issues/29).
14+
15+
### Added
16+
17+
- Improvements in README file and documentation in general.
18+
1019
## [0.2.0] - 2021-08-22
1120

1221
### Added
1322

14-
- JsonCachePrefs, a implementation on top of the shared_preferences package —
23+
- JsonCachePrefs: implementation on top of the shared_preferences package —
1524
[26](https://github.com/dartoos-dev/json_cache/issues/26).
1625

1726
### Changed
1827

19-
- renaming JsonCache methods: erase to remove; recovery to value. **BREAKING
20-
CHANGES**.
28+
- renaming of JsonCache's methods. Method `erase` renamed to `remove`; method
29+
`recovery`, to `value`. **BREAKING CHANGES**.
2130

2231
## [0.1.0] - 2021-08-22
2332

README.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Rultor.com](https://www.rultor.com/b/dartoos-dev/json_cache)](https://www.rultor
2121

2222
- [Overview](#overview)
2323
- [Getting Started](#getting-started)
24+
- [List of JsonCache implementations](#list-of-jsoncache-implementations)
25+
- [JsonCacheMem — Thread-safe In-memory cache](#jsoncachemem)
26+
- [JsonCachePrefs — SharedPreferences](#jsoncacheprefs)
27+
- [JsonCacheLocalStorage — LocalStorage](#jsoncachelocalstorage)
2428
- [Demo application](#demo-application)
2529
- [References](#references)
2630

@@ -90,17 +94,29 @@ represents the name of a single data group. For example:
9094
Above, the _profile_ key is associated with the profile-related data group,
9195
while the _preferences_ key is associated with the preferences-related data.
9296

97+
## List of JsonCache Implementations
98+
99+
The library
100+
[JsonCache](https://pub.dev/documentation/json_cache/latest/json_cache/json_cache-library.html)
101+
contains all the classes that implement the
102+
[JsonCache](https://pub.dev/documentation/json_cache/latest/json_cache/JsonCache-class.html)
103+
interface with more in-depth details.
104+
105+
The following sections are an overview of each implementation.
106+
93107
### JsonCacheMem
94108

95-
It is a thread-safe, in-memory implementation of the `JsonCache` interface.
109+
[JsonCacheMem](https://pub.dev/documentation/json_cache/latest/json_cache/JsonCacheMem-class.html)
110+
is is a thread-safe in-memory implementation of the `JsonCache` interface.
96111
Moreover, it encapsulates a secondary cache or "slower level2 cache". Typically,
97112
the secondary cache instance is responsible for the local cache; that is, it is
98113
the cache instance that persists data on the user's device.
99114

100-
#### JsonCacheMem Usage
115+
#### Typical Usage
101116

102117
Due to the fact that `JsonCacheMem` is a decorator, you should always pass
103-
another `JsonCache` instance when you instantiates it. For example:
118+
another `JsonCache` instance to it whenever you instantiates a `JsonCacheMem`
119+
object. For example:
104120

105121
```dart
106122
@@ -109,6 +125,32 @@ another `JsonCache` instance when you instantiates it. For example:
109125
110126
```
111127

128+
### JsonCachePrefs
129+
130+
[JsonCachePrefs](https://pub.dev/documentation/json_cache/latest/json_cache/JsonCachePrefs-class.html)
131+
is an implementation on top of the
132+
[shared_preferences](https://pub.dev/packages/shared_preferences) package.
133+
134+
```dart
135+
136+
final prefs = await SharedPreferences.getInstance();
137+
final JsonCache jsonCache = JsonCacheMem(JsonCachePrefs(prefs));
138+
139+
```
140+
141+
### JsonCacheLocalStorage
142+
143+
[JsonCacheLocalStorage](https://pub.dev/documentation/json_cache/latest/json_cache/JsonCacheLocalStorage-class.html)
144+
is an implementation on top of the
145+
[shared_preferences](https://pub.dev/packages/shared_preferences) package.
146+
147+
```dart
148+
149+
final LocalStorage storage = LocalStorage('my_data');
150+
final JsonCache jsonCache = JsonCacheMem(JsonCacheLocalStorage(storage));
151+
152+
```
153+
112154
## Demo application
113155

114156
The demo application provides a fully working example, focused on demonstrating

lib/json_cache.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ library json_cache;
33

44
export 'src/json_cache.dart';
55
export 'src/json_cache_fake.dart';
6+
export 'src/json_cache_local_storage.dart';
67
export 'src/json_cache_mem.dart';
78
export 'src/json_cache_prefs.dart';
89
export 'src/json_cache_wrap.dart';

lib/src/json_cache_local_storage.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import 'package:localstorage/localstorage.dart';
2+
3+
import 'json_cache.dart';
4+
5+
/// Implementation on top of the LocalStorage package.
6+
///
7+
/// See: [local storage](https://pub.dev/packages/localstorage)
8+
class JsonCacheLocalStorage implements JsonCache {
9+
/// Encapsulates a [LocalStorage] instance.
10+
JsonCacheLocalStorage(this._storage);
11+
12+
final LocalStorage _storage;
13+
14+
Future<bool> get _getReady => _storage.ready;
15+
16+
@override
17+
Future<void> clear() async {
18+
await _getReady;
19+
await _storage.clear();
20+
}
21+
22+
@override
23+
Future<void> refresh(String key, Map<String, dynamic> value) async {
24+
await _getReady;
25+
await _storage.setItem(key, value);
26+
}
27+
28+
@override
29+
Future<void> remove(String key) async {
30+
await _getReady;
31+
await _storage.deleteItem(key);
32+
}
33+
34+
@override
35+
Future<Map<String, dynamic>?> value(String key) async {
36+
await _getReady;
37+
return await _storage.getItem(key) as Map<String, dynamic>?;
38+
}
39+
}

lib/src/json_cache_mem.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'package:mutex/mutex.dart';
22

33
import 'json_cache.dart';
44

5-
/// Thread-safe, in-memory [JsonCache] decorator.
5+
/// Thread-safe in-memory [JsonCache] decorator.
66
///
77
/// It is a kind of level 1 cache.
88
///

pubspec.lock

Lines changed: 25 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:
@@ -93,6 +93,13 @@ packages:
9393
url: "https://pub.dartlang.org"
9494
source: hosted
9595
version: "1.6.0"
96+
localstorage:
97+
dependency: "direct main"
98+
description:
99+
name: localstorage
100+
url: "https://pub.dartlang.org"
101+
source: hosted
102+
version: "4.0.0+1"
96103
matcher:
97104
dependency: transitive
98105
description:
@@ -106,7 +113,7 @@ packages:
106113
name: meta
107114
url: "https://pub.dartlang.org"
108115
source: hosted
109-
version: "1.3.0"
116+
version: "1.7.0"
110117
mutex:
111118
dependency: "direct main"
112119
description:
@@ -121,13 +128,27 @@ packages:
121128
url: "https://pub.dartlang.org"
122129
source: hosted
123130
version: "1.8.0"
131+
path_provider:
132+
dependency: transitive
133+
description:
134+
name: path_provider
135+
url: "https://pub.dartlang.org"
136+
source: hosted
137+
version: "2.0.2"
124138
path_provider_linux:
125139
dependency: transitive
126140
description:
127141
name: path_provider_linux
128142
url: "https://pub.dartlang.org"
129143
source: hosted
130144
version: "2.0.2"
145+
path_provider_macos:
146+
dependency: transitive
147+
description:
148+
name: path_provider_macos
149+
url: "https://pub.dartlang.org"
150+
source: hosted
151+
version: "2.0.2"
131152
path_provider_platform_interface:
132153
dependency: transitive
133154
description:
@@ -251,7 +272,7 @@ packages:
251272
name: test_api
252273
url: "https://pub.dartlang.org"
253274
source: hosted
254-
version: "0.3.0"
275+
version: "0.4.2"
255276
typed_data:
256277
dependency: transitive
257278
description:

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ environment:
1111
dependencies:
1212
flutter:
1313
sdk: flutter
14+
localstorage: ^4.0.0+1
1415
mutex: ^3.0.0
1516
shared_preferences: ^2.0.6
1617

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:json_cache/json_cache.dart';
3+
import 'package:localstorage/localstorage.dart';
4+
5+
void main() {
6+
final LocalStorage storage =
7+
LocalStorage('json_cache_local_storage_unit_test');
8+
group('JsonCacheLocalStorage', () {
9+
test('clear, recover and refresh', () async {
10+
const profKey = 'profile';
11+
const profData = <String, Object>{'id': 1, 'name': 'John Due'};
12+
final JsonCacheLocalStorage localStorageCache =
13+
JsonCacheLocalStorage(storage);
14+
await localStorageCache.refresh(profKey, profData);
15+
var prof = await localStorageCache.value(profKey);
16+
expect(prof, profData);
17+
await localStorageCache.clear();
18+
prof = await localStorageCache.value(profKey);
19+
expect(prof, isNull);
20+
});
21+
22+
test('remove', () async {
23+
const profKey = 'profile';
24+
const prefKey = 'preferences';
25+
final profData = <String, Object>{'id': 1, 'name': 'John Due'};
26+
final prefData = <String, Object>{
27+
'theme': 'dark',
28+
'notifications': {'enabled': true}
29+
};
30+
final JsonCacheLocalStorage localStorageCache =
31+
JsonCacheLocalStorage(storage);
32+
await localStorageCache.refresh(profKey, profData);
33+
await localStorageCache.refresh(prefKey, prefData);
34+
35+
var prof = await localStorageCache.value(profKey);
36+
expect(prof, profData);
37+
38+
await localStorageCache.remove(profKey);
39+
prof = await localStorageCache.value(profKey);
40+
expect(prof, isNull);
41+
42+
var pref = await localStorageCache.value(prefKey);
43+
expect(pref, prefData);
44+
await localStorageCache.remove(prefKey);
45+
pref = await localStorageCache.value(prefKey);
46+
expect(pref, isNull);
47+
});
48+
});
49+
}

0 commit comments

Comments
 (0)