Skip to content

Commit d566ef6

Browse files
authored
Fix multiple commands in redis cache. (#142)
* Fix multiple commands in redis cache. * Using future boundary to add stream.
1 parent 6c1d468 commit d566ef6

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

neat_cache/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v2.0.1
2+
* Fixed issue when adding multiple commands without waiting for them to complete first.
3+
14
## v2.0.0
25
* Migrated to null-safety, dropping dependency on `package:dartis`.
36

neat_cache/lib/src/providers/resp.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class RespClient {
6565

6666
final _ByteStreamScanner _input;
6767
final StreamSink<List<int>> _output;
68+
Future _pendingStream = Future.value(null);
6869

6970
final _pending = Queue<Completer<Object?>>();
7071

@@ -177,7 +178,8 @@ class RespClient {
177178
final c = Completer<Object?>();
178179
_pending.addLast(c);
179180
try {
180-
await _output.addStream(Stream.value(out.toBytes()));
181+
_pendingStream = _pendingStream
182+
.then((value) => _output.addStream(Stream.value(out.toBytes())));
181183
} on Exception catch (e, st) {
182184
await _abort(e, st);
183185
}

neat_cache/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: neat_cache
2-
version: 2.0.0
2+
version: 2.0.1
33
description: |
44
A neat cache abstraction for wrapping in-memory or redis caches.
55
homepage: https://github.com/google/dart-neats/tree/master/neat_cache

neat_cache/test/cacheprovider_test.dart

+30
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,42 @@ void testCacheProvider({
3636
expect(r, isNull);
3737
});
3838

39+
test('get multiple keys at the same time', () async {
40+
await cache.purge('test-key');
41+
final list = await Future.wait([
42+
cache.get('test-empty-key-1'),
43+
cache.get('test-empty-key-2'),
44+
cache.get('test-empty-key-3'),
45+
cache.get('test-empty-key-4'),
46+
cache.get('test-empty-key-1'),
47+
cache.get('test-empty-key-2'),
48+
cache.get('test-empty-key-3'),
49+
cache.get('test-empty-key-4'),
50+
]);
51+
expect(list, isNotEmpty);
52+
expect(list.where((e) => e != null), isEmpty);
53+
});
54+
3955
test('get/set key', () async {
4056
await cache.set('test-key-2', 'hello-world-42');
4157
final r = await cache.get('test-key-2');
4258
expect(r, equals('hello-world-42'));
4359
});
4460

61+
test('get/set multiple keys', () async {
62+
await Future.wait([
63+
cache.set('test-multi-key-1', 'hello-world-1'),
64+
cache.set('test-multi-key-2', 'hello-world-2'),
65+
cache.set('test-multi-key-3', 'hello-world-3'),
66+
]);
67+
final values = await Future.wait([
68+
cache.get('test-multi-key-1'),
69+
cache.get('test-multi-key-2'),
70+
cache.get('test-multi-key-3'),
71+
]);
72+
expect(values, ['hello-world-1', 'hello-world-2', 'hello-world-3']);
73+
});
74+
4575
test('set key (overwrite)', () async {
4676
await cache.set('test-key-3', 'hello-once');
4777
final r = await cache.get('test-key-3');

0 commit comments

Comments
 (0)