-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add unary data client with get/set support #35
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5750e1b
feat: add unary data client with get/set support
bruuuuuuuce 7b23207
chore: fix flutter analyze
bruuuuuuuce 8190092
chore: export cache client from sdk
bruuuuuuuce 84abbd6
Merge branch 'main' into feat/unaryGetSet
bruuuuuuuce 1f9cb8e
chore: set timeout at client instantiation
bruuuuuuuce 87fb164
chore: ttl should be duration not int
bruuuuuuuce File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import 'package:client_sdk_dart/src/config/cache_configuration.dart'; | ||
import 'package:client_sdk_dart/src/internal/data_client.dart'; | ||
import 'package:client_sdk_dart/src/messages/responses/cache/data/scalar/get_response.dart'; | ||
import 'package:client_sdk_dart/src/messages/responses/cache/data/scalar/set_response.dart'; | ||
import 'package:logging/logging.dart'; | ||
|
||
import 'auth/credential_provider.dart'; | ||
import 'config/logger.dart'; | ||
import 'messages/values.dart'; | ||
|
||
abstract class ICacheClient { | ||
Future<GetResponse> get(String cacheName, Value value); | ||
|
||
Future<SetResponse> set(String cacheName, Value key, Value value, | ||
{Duration? ttl}); | ||
} | ||
|
||
class CacheClient implements ICacheClient { | ||
final DataClient _dataClient; | ||
final Logger _logger = Logger('MomentoCacheClient'); | ||
|
||
CacheClient(CredentialProvider credentialProvider, | ||
CacheConfiguration configuration, Duration defaultTtl) | ||
: _dataClient = | ||
DataClient(credentialProvider, configuration, defaultTtl) { | ||
_logger.level = determineLoggerLevel(configuration.logLevel); | ||
_logger.finest("initializing cache client"); | ||
} | ||
|
||
@override | ||
Future<GetResponse> get(String cacheName, Value value) { | ||
return _dataClient.get(cacheName, value); | ||
} | ||
|
||
@override | ||
Future<SetResponse> set(String cacheName, Value key, Value value, | ||
{Duration? ttl}) { | ||
return _dataClient.set(cacheName, key, value, ttl: ttl); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import 'package:client_sdk_dart/generated/cacheclient.pbgrpc.dart'; | ||
import 'package:client_sdk_dart/src/config/cache_configuration.dart'; | ||
import 'package:client_sdk_dart/src/errors/errors.dart'; | ||
import 'package:client_sdk_dart/src/messages/responses/cache/data/scalar/get_response.dart'; | ||
import 'package:client_sdk_dart/src/messages/responses/cache/data/scalar/set_response.dart'; | ||
import 'package:fixnum/fixnum.dart'; | ||
import 'package:grpc/grpc.dart'; | ||
|
||
import '../auth/credential_provider.dart'; | ||
import '../messages/values.dart'; | ||
|
||
abstract class AbstractDataClient { | ||
Future<GetResponse> get(String cacheName, Value key); | ||
|
||
Future<SetResponse> set(String cacheName, Value key, Value value, | ||
{Duration? ttl}); | ||
} | ||
|
||
class DataClient implements AbstractDataClient { | ||
late ClientChannel _channel; | ||
late ScsClient _client; | ||
late CacheConfiguration _configuration; | ||
late Duration _defaultTtl; | ||
|
||
DataClient(CredentialProvider credentialProvider, | ||
CacheConfiguration configuration, Duration defaultTtl) { | ||
_channel = ClientChannel(credentialProvider.cacheEndpoint); | ||
_client = ScsClient(_channel, | ||
options: CallOptions(metadata: { | ||
'authorization': credentialProvider.apiKey, | ||
'agent': 'dart:0.1.0', | ||
}, timeout: _configuration.transportStrategy.grpcConfig.deadline)); | ||
_configuration = configuration; | ||
_defaultTtl = defaultTtl; | ||
} | ||
|
||
@override | ||
Future<GetResponse> get(String cacheName, Value key) async { | ||
var request = GetRequest_(); | ||
request.cacheKey = key.toBinary(); | ||
try { | ||
var resp = await _client.get(request, | ||
options: CallOptions(metadata: { | ||
'cacheName': cacheName, | ||
})); | ||
|
||
switch (resp.result) { | ||
case ECacheResult.Miss: | ||
return GetMiss(); | ||
case ECacheResult.Hit: | ||
return GetHit(resp.cacheBody); | ||
default: | ||
return GetError(UnknownException( | ||
"unknown cache get error ${resp.result}", null, null)); | ||
} | ||
} catch (e) { | ||
if (e is SdkException) { | ||
return GetError(e); | ||
} else { | ||
return GetError(UnknownException("Unexpected error: $e", null, null)); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
Future<SetResponse> set(String cacheName, Value key, Value value, | ||
{Duration? ttl}) async { | ||
var request = SetRequest_(); | ||
request.cacheKey = key.toBinary(); | ||
request.cacheBody = value.toBinary(); | ||
request.ttlMilliseconds = (ttl != null | ||
? ttl.inMilliseconds | ||
: _defaultTtl.inMilliseconds) as Int64; | ||
try { | ||
await _client.set(request, | ||
options: CallOptions(metadata: { | ||
'cacheName': cacheName, | ||
})); | ||
return SetSuccess(); | ||
} catch (e) { | ||
if (e is SdkException) { | ||
return SetError(e); | ||
} else { | ||
return SetError(UnknownException("Unexpected error: $e", null, null)); | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
lib/src/messages/responses/cache/data/scalar/get_response.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:client_sdk_dart/src/messages/responses/responses_base.dart'; | ||
|
||
sealed class GetResponse {} | ||
|
||
class GetMiss implements GetResponse {} | ||
|
||
class GetError extends ErrorResponseBase implements GetResponse { | ||
GetError(super.exception); | ||
} | ||
|
||
class GetHit implements GetResponse { | ||
GetHit(this._value); | ||
|
||
final List<int> _value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I played around with this being our |
||
|
||
String get value => utf8.decode(_value); | ||
List<int> get binaryValue => _value; | ||
} |
9 changes: 9 additions & 0 deletions
9
lib/src/messages/responses/cache/data/scalar/set_response.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import 'package:client_sdk_dart/src/messages/responses/responses_base.dart'; | ||
|
||
sealed class SetResponse {} | ||
|
||
class SetSuccess implements SetResponse {} | ||
|
||
class SetError extends ErrorResponseBase implements SetResponse { | ||
SetError(super.exception); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,38 @@ | ||
sealed class Value {} | ||
import 'dart:convert'; | ||
|
||
sealed class Value { | ||
List<int> toBinary(); | ||
String toUtf8(); | ||
} | ||
|
||
class StringValue implements Value { | ||
final String _value; | ||
StringValue(String v) : _value = v; | ||
String get value => _value; | ||
|
||
@override | ||
toBinary() { | ||
return utf8.encode(_value); | ||
} | ||
|
||
@override | ||
toUtf8() { | ||
return _value; | ||
} | ||
} | ||
|
||
class BinaryValue implements Value { | ||
final List<int> _value; | ||
BinaryValue(List<int> v) : _value = v; | ||
List<int> get value => _value; | ||
|
||
@override | ||
List<int> toBinary() { | ||
return _value; | ||
} | ||
|
||
@override | ||
String toUtf8() { | ||
return utf8.decode(_value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably not a huge deal, but will we want to change the filename to
momento
in a future pr so it can say we're importing from momento? or is the package name the repo name?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmmm yea good call, ill make a ticket for this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#37