@@ -10,51 +10,51 @@ typedef OnInitError = FutureOr<Null> Function(Object, StackTrace);
1010
1111/// Thread-safe in-memory [JsonCache] decorator.
1212///
13- /// It is a kind of level 1 cache.
13+ /// It is a kind of _level 1_ cache.
1414///
1515/// It encapsulates a slower cache and keeps its own data in-memory.
1616class JsonCacheMem implements JsonCache {
17- /// In-memory level1 cache with an optional level2 instance.
17+ /// In-memory _level 1_ cache with an optional _level 2_ instance.
1818 ///
1919 /// **Note**: if you do not pass an object to the parameter [level2] , the data
2020 /// will remain cached in-memory only; that is, no data will be persited to
21- /// the user's device's local storage area . Indeed, not persisting data on the
21+ /// the local storage of the user's device. Indeed, not persisting data on the
2222 /// user's device might be the desired behavior if you are at the prototyping
2323 /// or mocking phase. However, its unlikely to be the right behavior in
2424 /// production code.
2525 JsonCacheMem ([JsonCache ? level2])
2626 : this .mem (_shrMem, level2: level2, mutex: _shrMutex);
2727
28- /// Initializes both the level1 (internal memory) and level2 (user's device's
29- /// local storage area) caches with data.
28+ /// Initializes both the internal memory (level 1 cache) and the local storage
29+ /// of the user's device (level 2 cache — [level2] ) with the contents of
30+ /// [initData] .
3031 ///
31- /// It also provides a type of transaction guarantee whereby, if an error
32- /// occurs while copying [init ] to cache, it will try to revert the cached
33- /// data to its previous state before rethrowing the exception that signaled
34- /// the error. Finally, after reverting the cached data, it invokes
35- /// [onInitError] .
32+ /// This method also provides a kind of transaction guarantee whereby if an
33+ /// error occurs while copying [initData ] to the cache, it will attempt to
34+ /// revert the cache [level2] to its previous state before rethrowing the
35+ /// exception that signaled the error. Finally, after reverting the cached
36+ /// data, it will invoke [onInitError] .
3637 JsonCacheMem .init (
37- Map <String , Map <String , dynamic >?> init , {
38+ Map <String , Map <String , dynamic >?> initData , {
3839 JsonCache ? level2,
3940 OnInitError ? onInitError,
4041 }) : _level2 = level2 ?? const JsonCacheHollow (),
4142 _memory = _shrMem,
4243 _mutex = _shrMutex {
43- final copy = Map <String , Map <String , dynamic >?>.of (init);
4444 final initFut = _mutex.protectWrite (() async {
45- final writes = < String > []; // the list of written keys.
46- for (final entry in copy .entries) {
45+ final cachedKeys = < String > [];
46+ for (final entry in initData .entries) {
4747 final key = entry.key;
4848 final value = entry.value;
4949 if (value != null ) {
50- writes .add (key);
50+ cachedKeys .add (key);
5151 try {
5252 await _level2.refresh (key, value);
5353 _memory[key] = value;
5454 } catch (error) {
55- for (final write in writes ) {
56- await _level2.remove (write );
57- _memory.remove (write );
55+ for (final key in cachedKeys ) {
56+ await _level2.remove (key );
57+ _memory.remove (key );
5858 }
5959 rethrow ;
6060 }
@@ -77,7 +77,7 @@ class JsonCacheMem implements JsonCache {
7777 _level2 = level2 ?? const JsonCacheHollow (),
7878 _mutex = mutex ?? ReadWriteMutex ();
7979
80- /// Slower cache level.
80+ /// Slower level 2 cache .
8181 final JsonCache _level2;
8282
8383 /// In-memory storage.
@@ -93,6 +93,8 @@ class JsonCacheMem implements JsonCache {
9393 static final _shrMutex = ReadWriteMutex ();
9494
9595 /// Frees up storage space in both the level2 cache and in-memory cache.
96+ ///
97+ /// Throws [JsonCacheException] to indicate operation failure.
9698 @override
9799 Future <void > clear () async {
98100 await _mutex.protectWrite (() async {
@@ -101,15 +103,17 @@ class JsonCacheMem implements JsonCache {
101103 });
102104 }
103105
104- /// Updates data located at [key] in both the level2 cache and in-memory
105- /// cache.
106+ /// Updates the data located at [key] in both the _level 2_ cache and
107+ /// in-memory cache.
108+ ///
109+ /// Throws [JsonCacheException] to indicate operation failure.
106110 @override
107111 Future <void > refresh (String key, Map <String , dynamic > data) async {
108112 // ATTENTION: It is safer to copy the content of [data] before calling an
109113 // asynchronous method that will copy it to avoid data races. For example,
110114 // if the client code clears [data] right after passing it to this method,
111- // there's a high chance of having _level2 and this object with different
112- // contents.
115+ // there's a high chance of having the level 2 cache and this object with
116+ // different contents.
113117 //
114118 // In Dart, synchronous code cannot be interrupted, so there is no need to
115119 // protect it using mutual exclusion.
@@ -120,8 +124,8 @@ class JsonCacheMem implements JsonCache {
120124 });
121125 }
122126
123- /// Removes the value located at [key] from both the level2 cache and
124- /// in -memory cache .
127+ /// Removes the cached value located at [key] from both the _level 2 cache_
128+ /// and _in -memory cache_ .
125129 @override
126130 Future <void > remove (String key) async {
127131 await _mutex.protectWrite (() async {
@@ -130,33 +134,30 @@ class JsonCacheMem implements JsonCache {
130134 });
131135 }
132136
133- /// Retrieves the value at [key] or null if there is no data.
137+ /// Retrieves the value at [key] or ` null` if there is no data.
134138 @override
135139 Future <Map <String , dynamic >?> value (String key) {
136140 return _mutex.protectRead (() async {
137- final cachedL1 = _memory[key];
138- if (cachedL1 != null ) {
139- return Map <String , dynamic >.of (cachedL1 );
141+ final inMemoryValue = _memory[key];
142+ if (inMemoryValue != null ) {
143+ return Map <String , dynamic >.of (inMemoryValue );
140144 }
141- final cachedL2 = await _level2.value (key);
142- if (cachedL2 != null ) {
143- _memory[key] = cachedL2 ;
144- return Map <String , dynamic >.of (cachedL2 );
145+ final localStorageValue = await _level2.value (key);
146+ if (localStorageValue != null ) {
147+ _memory[key] = localStorageValue ;
148+ return Map <String , dynamic >.of (localStorageValue );
145149 }
146150 return null ;
147151 });
148152 }
149153
150- /// Checks whether the in-memory or the level2 chache contains cached data at
151- /// [key] .
154+ /// Checks whether the _in-memory_ or the _level 2 cache_ contains cached data
155+ /// at [key] .
152156 @override
153157 Future <bool > contains (String key) {
154158 return _mutex.protectRead (() async {
155- bool found = _memory.containsKey (key);
156- if (! found) {
157- found = await _level2.contains (key);
158- }
159- return found;
159+ final bool foundInMemory = _memory.containsKey (key);
160+ return foundInMemory ? foundInMemory : await _level2.contains (key);
160161 });
161162 }
162163}
0 commit comments