Skip to content

Commit d460334

Browse files
committed
refactor: rename JsonCachePrefs to JsonCacheSharedPreferences
1 parent 3f0d9a0 commit d460334

File tree

6 files changed

+33
-30
lines changed

6 files changed

+33
-30
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
- rename class JsonCacheSecStorage to JsonCacheFlutterSecureStorage — **BREAKING CHANGE**[126](https://github.com/dartoos-dev/json_cache/issues/126).
1515

16+
- rename class JsonCachePrefs to JsonCacheSharedPreferences — **BREAKING CHANGE**[127](https://github.com/dartoos-dev/json_cache/issues/127).
17+
1618
### Removed
1719

1820
- support for the 'EncryptedSharedPreferences' package — **BREAKING CHANGE**[125](https://github.com/dartoos-dev/json_cache/issues/125).

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Rultor.com](https://www.rultor.com/b/dartoos-dev/json_cache)](https://www.rultor
2828
- [Implementations](#implementations)
2929
- [JsonCacheMem — Thread-safe In-memory cache](#jsoncachemem)
3030
- [JsonCacheTry — Enhanced Diagnostic Messages](#jsoncachetry)
31-
- [JsonCachePrefs — SharedPreferences](#jsoncacheprefs)
31+
- [JsonCacheSharedPreferences — SharedPreferences](#jsoncachesharedpreferences)
3232
- [JsonCacheLocalStorage — LocalStorage](#jsoncachelocalstorage)
3333
- [JsonCacheFlutterSecureStorage — FlutterSecureStorage](#jsoncachefluttersecurestorage)
3434
- [JsonCacheCrossLocalStorage — CrossLocalStorage](#jsoncachecrosslocalstorage)
@@ -202,8 +202,8 @@ normally pass another `JsonCache` instance to it whenever you instantiate a
202202
```dart
203203
204204
/// Cache initialization
205-
final prefs = await SharedPreferences.getInstance();
206-
final JsonCacheMem jsonCache = JsonCacheMem(JsonCachePrefs(prefs));
205+
final sharedPrefs = await SharedPreferences.getInstance();
206+
final JsonCacheMem jsonCache = JsonCacheMem(JsonCacheSharedPreferences(sharedPrefs));
207207
208208
/// Saving profile and preferences data.
209209
await jsonCache.refresh('profile', {'name': 'John Doe', 'email': '[email protected]', 'accountType': 'premium'});
@@ -254,22 +254,22 @@ object. For example:
254254
```dart
255255
256256
// Local storage cache initialization
257-
final prefs = await SharedPreferences.getInstance();
257+
final sharedPrefs = await SharedPreferences.getInstance();
258258
// JsonCacheTry instance initialized with in-memory and local storage caches.
259-
final jsonCacheTry = JsonCacheTry(JsonCacheMem(JsonCachePrefs(prefs)));
259+
final jsonCacheTry = JsonCacheTry(JsonCacheMem(JsonCacheSharedPreferences(sharedPrefs)));
260260
261261
```
262262

263-
### JsonCachePrefs
263+
### JsonCacheSharedPreferences
264264

265-
[JsonCachePrefs](https://pub.dev/documentation/json_cache/latest/json_cache/JsonCachePrefs-class.html)
265+
[JsonCacheSharedPreferences](https://pub.dev/documentation/json_cache/latest/json_cache/JsonCacheSharedPreferences-class.html)
266266
is an implementation on top of the
267267
[shared_preferences](https://pub.dev/packages/shared_preferences) package.
268268

269269
```dart
270270
271-
final prefs = await SharedPreferences.getInstance();
272-
final JsonCache jsonCache = JsonCacheMem(JsonCachePrefs(prefs));
271+
final sharedPrefs = await SharedPreferences.getInstance();
272+
final JsonCache jsonCache = JsonCacheMem(JsonCacheSharedPreferences(sharedPrefs));
273273
274274
```
275275
### JsonCacheLocalStorage
@@ -314,8 +314,8 @@ is an implementation on top of the
314314

315315
```dart
316316
317-
final LocalStorageInterface prefs = await LocalStorage.getInstance();
318-
final JsonCache jsonCache = JsonCacheMem(JsonCacheCrossLocalStorage(prefs));
317+
final LocalStorageInterface localStorage = await LocalStorage.getInstance();
318+
final JsonCache jsonCache = JsonCacheMem(JsonCacheCrossLocalStorage(localStorage));
319319
```
320320

321321
### JsonCacheHive

lib/json_cache.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export 'src/json_cache_hive.dart';
1010
export 'src/json_cache_hollow.dart';
1111
export 'src/json_cache_local_storage.dart';
1212
export 'src/json_cache_mem.dart';
13-
export 'src/json_cache_prefs.dart';
13+
export 'src/json_cache_shared_preferences.dart';
1414
export 'src/json_cache_try.dart';
1515
export 'src/json_cache_wrap.dart';

lib/src/json_cache_prefs.dart renamed to lib/src/json_cache_shared_preferences.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ import 'package:json_cache/json_cache.dart';
44
import 'package:shared_preferences/shared_preferences.dart';
55

66
/// Persistent preferences file cache.
7-
class JsonCachePrefs implements JsonCache {
8-
/// [_prefs] a [SharedPreferences] instance.
9-
const JsonCachePrefs(this._prefs);
7+
class JsonCacheSharedPreferences implements JsonCache {
8+
/// Sets the [SharedPreferences] instance.
9+
const JsonCacheSharedPreferences(this._sharedPreferences);
1010

11-
// The preferences file object.
12-
final SharedPreferences _prefs;
11+
// The shared preferences file object.
12+
final SharedPreferences _sharedPreferences;
1313

1414
/// Frees up the preferences file space.
1515
@override
1616
Future<void> clear() async {
17-
await _prefs.clear();
17+
await _sharedPreferences.clear();
1818
}
1919

2020
/// Removes an entry from the preferences file storage at [key].
2121
@override
2222
Future<void> remove(String key) async {
23-
await _prefs.remove(key);
23+
await _sharedPreferences.remove(key);
2424
}
2525

2626
/// Writes [value] to the preferences file.
@@ -29,14 +29,14 @@ class JsonCachePrefs implements JsonCache {
2929
/// under the hood.
3030
@override
3131
Future<void> refresh(String key, Map<String, dynamic> value) async {
32-
await _prefs.setString(key, json.encode(value));
32+
await _sharedPreferences.setString(key, json.encode(value));
3333
}
3434

3535
/// The value at [key] from the preferences file; it returns null if a cache
3636
/// miss occurs.
3737
@override
3838
Future<Map<String, dynamic>?> value(String key) async {
39-
final strJson = _prefs.getString(key);
39+
final strJson = _sharedPreferences.getString(key);
4040
return strJson == null
4141
? null
4242
: json.decode(strJson) as Map<String, dynamic>;
@@ -45,6 +45,6 @@ class JsonCachePrefs implements JsonCache {
4545
/// Checks whether there is data in the preferences at [key].
4646
@override
4747
Future<bool> contains(String key) async {
48-
return _prefs.containsKey(key);
48+
return _sharedPreferences.containsKey(key);
4949
}
5050
}

test/json_cache_mem_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ void main() {
195195
test('value missing from L1 cache', () async {
196196
const key = 'aValue';
197197
const value = <String, dynamic>{'must be a json encodable value': true};
198-
final prefs = JsonCachePrefs(await SharedPreferences.getInstance());
198+
final prefs =
199+
JsonCacheSharedPreferences(await SharedPreferences.getInstance());
199200
await prefs.refresh(key, value);
200201
final JsonCacheMem memCache = JsonCacheMem(prefs);
201202
final cacheL2Value = await memCache.value(key);

test/json_cache_prefs_test.dart renamed to test/json_cache_shared_preferences_test.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import 'package:shared_preferences/shared_preferences.dart';
44

55
void main() {
66
SharedPreferences.setMockInitialValues({});
7-
group('JsonCachePrefs', () {
7+
group('JsonCacheSharedPreferences', () {
88
test('clear, value, refresh', () async {
99
const profKey = 'profile';
1010
const profData = <String, Object>{'id': 1, 'name': 'John Due'};
11-
final JsonCachePrefs prefsCache =
12-
JsonCachePrefs(await SharedPreferences.getInstance());
11+
final JsonCacheSharedPreferences prefsCache =
12+
JsonCacheSharedPreferences(await SharedPreferences.getInstance());
1313
await prefsCache.refresh(profKey, profData);
1414
var prof = await prefsCache.value(profKey);
1515
expect(prof, profData);
@@ -26,8 +26,8 @@ void main() {
2626
'theme': 'dark',
2727
'notifications': {'enabled': true}
2828
};
29-
final JsonCachePrefs prefs =
30-
JsonCachePrefs(await SharedPreferences.getInstance());
29+
final JsonCacheSharedPreferences prefs =
30+
JsonCacheSharedPreferences(await SharedPreferences.getInstance());
3131
// update data
3232
await prefs.refresh(profKey, profData);
3333
await prefs.refresh(prefKey, prefData);
@@ -51,8 +51,8 @@ void main() {
5151
'theme': 'dark',
5252
'notifications': {'enabled': true}
5353
};
54-
final JsonCachePrefs prefsCache =
55-
JsonCachePrefs(await SharedPreferences.getInstance());
54+
final JsonCacheSharedPreferences prefsCache =
55+
JsonCacheSharedPreferences(await SharedPreferences.getInstance());
5656
await prefsCache.refresh(profKey, profData);
5757
await prefsCache.refresh(prefKey, prefData);
5858

0 commit comments

Comments
 (0)