-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_cache_delegate.dart
More file actions
62 lines (48 loc) · 1.55 KB
/
async_cache_delegate.dart
File metadata and controls
62 lines (48 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import 'package:dartlcemodel/dartlcemodel_cache.dart';
/// Delegate that asynchronously performs caching operations
/// [D] - data type to store
/// [P] - params type
abstract class AsyncCacheDelegate<D extends Object, P extends Object> {
/// Returns data if cached
/// [params] Caching key
Future<Entity<D>?> get(P params);
/// Saves data to cache
/// [params] Caching key
/// [entity] Entity to cache
Future<void> save(P params, Entity<D> entity);
/// Invalidates cached value
/// [params] Caching key
Future<void> invalidate(P params);
/// Invalidates all cached values
Future<void> invalidateAll();
/// Deletes cached value
/// [params] Caching key
Future<void> delete(P params);
/// Wraps a [sync] delegate and adapts it to async world
const factory AsyncCacheDelegate(SyncCacheDelegate<D, P> sync) = _SyncDelegateAdapter;
}
/// Adapts sync delegate to use in async service
class _SyncDelegateAdapter<D extends Object, P extends Object> implements AsyncCacheDelegate<D, P> {
final SyncCacheDelegate<D, P> _delegate;
const _SyncDelegateAdapter(this._delegate);
@override
Future<Entity<D>?> get(P params) async {
return _delegate.get(params);
}
@override
Future<void> save(P params, Entity<D> entity) async {
_delegate.save(params, entity);
}
@override
Future<void> invalidate(P params) async {
_delegate.invalidate(params);
}
@override
Future<void> invalidateAll() async {
_delegate.invalidateAll();
}
@override
Future<void> delete(P params) async {
_delegate.delete(params);
}
}