momento 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.release-please-manifest.json +3 -0
  3. data/.rubocop.yml +12 -0
  4. data/.yardopts +2 -0
  5. data/CHANGELOG.md +28 -0
  6. data/CONTRIBUTING.md +1 -1
  7. data/Gemfile.lock +7 -3
  8. data/README.md +131 -36
  9. data/README.template.md +88 -0
  10. data/examples/.gitignore +1 -0
  11. data/examples/Gemfile +5 -0
  12. data/examples/README.md +34 -0
  13. data/examples/compact.rb +43 -0
  14. data/examples/example.rb +66 -0
  15. data/examples/file.rb +57 -0
  16. data/lib/momento/cacheclient_pb.rb +2 -0
  17. data/lib/momento/cacheclient_services_pb.rb +2 -0
  18. data/lib/momento/controlclient_pb.rb +2 -0
  19. data/lib/momento/controlclient_services_pb.rb +2 -0
  20. data/lib/momento/create_cache_response.rb +9 -24
  21. data/lib/momento/create_cache_response_builder.rb +27 -0
  22. data/lib/momento/delete_cache_response.rb +6 -21
  23. data/lib/momento/delete_cache_response_builder.rb +25 -0
  24. data/lib/momento/delete_response.rb +6 -15
  25. data/lib/momento/delete_response_builder.rb +23 -0
  26. data/lib/momento/error/grpc_details.rb +38 -0
  27. data/lib/momento/error/transport_details.rb +20 -0
  28. data/lib/momento/error/types.rb +232 -0
  29. data/lib/momento/error.rb +54 -0
  30. data/lib/momento/error_builder.rb +50 -0
  31. data/lib/momento/exceptions.rb +7 -0
  32. data/lib/momento/get_response.rb +37 -40
  33. data/lib/momento/get_response_builder.rb +37 -0
  34. data/lib/momento/list_caches_response.rb +45 -21
  35. data/lib/momento/list_caches_response_builder.rb +25 -0
  36. data/lib/momento/response/error.rb +10 -3
  37. data/lib/momento/response.rb +54 -1
  38. data/lib/momento/response_builder.rb +18 -0
  39. data/lib/momento/set_response.rb +21 -21
  40. data/lib/momento/set_response_builder.rb +25 -0
  41. data/lib/momento/simple_cache_client.rb +163 -31
  42. data/lib/momento/ttl.rb +48 -0
  43. data/lib/momento/version.rb +2 -1
  44. data/momento.gemspec +1 -0
  45. data/release-please-config.json +15 -0
  46. metadata +44 -6
  47. data/examples/basic.rb +0 -45
@@ -1,49 +1,43 @@
1
- require 'grpc'
2
- require 'momento/cacheclient_pb'
1
+ require_relative 'response/error'
3
2
 
4
3
  module Momento
5
- # Responses specific to get.
4
+ # A response containing the value retrieved from a cache.
6
5
  class GetResponse < Response
7
- class << self
8
- # Build a Momento::GetResponse from a block of code
9
- # which returns a Momento::ControlClient::GetResponse.
10
- #
11
- # @return [Momento::GetResponse]
12
- # @raise [StandardError] when the exception is not recognized.
13
- # @raise [TypeError] when the response is not recognized.
14
- def from_block
15
- response = yield
16
- rescue GRPC::BadStatus => e
17
- Error.new(grpc_exception: e)
18
- else
19
- from_response(response)
20
- end
21
-
22
- private
23
-
24
- def from_response(response)
25
- raise TypeError unless response.is_a?(Momento::CacheClient::GetResponse)
26
-
27
- case response.result
28
- when :Hit
29
- Hit.new(grpc_response: response)
30
- when :Miss
31
- Miss.new
32
- else
33
- raise "Unknown get result: #{response.result}"
34
- end
35
- end
36
- end
37
-
6
+ # There was a value for the key.
7
+ # @return [Boolean]
38
8
  def hit?
39
9
  false
40
10
  end
41
11
 
12
+ # There was no value for the key.
13
+ # @return [Boolean]
42
14
  def miss?
43
15
  false
44
16
  end
45
17
 
46
- # Successfully got an item from the cache.
18
+ # The gotten value, if any, as binary data: an ASCII_8BIT encoded frozen String.
19
+ #
20
+ # @return [String,nil] the value, if any, frozen and ASCII_8BIT encoded
21
+ def value_bytes
22
+ nil
23
+ end
24
+
25
+ # The gotten value, if any, as a string using your default encoding or specified one.
26
+ #
27
+ # @param encoding [Encoding] defaults to Encoding.default_external
28
+ # @return [String,nil] the value, if any, re-encoded
29
+ # rubocop:disable Lint/UnusedMethodArgument
30
+ def value_string(encoding = Encoding.default_external)
31
+ nil
32
+ end
33
+ # rubocop:enable Lint/UnusedMethodArgument
34
+
35
+ # @!method to_s
36
+ # Displays the response and the value, if any.
37
+ # A long value will be truncated.
38
+ # @return [String]
39
+
40
+ # @private
47
41
  class Hit < GetResponse
48
42
  # rubocop:disable Lint/MissingSuper
49
43
  def initialize(grpc_response:)
@@ -55,24 +49,27 @@ module Momento
55
49
  true
56
50
  end
57
51
 
58
- # @return [String] the value from the cache
59
- def value
52
+ def value_bytes
60
53
  @grpc_response.cache_body
61
54
  end
62
55
 
56
+ def value_string(encoding = Encoding.default_external)
57
+ value_bytes.dup.force_encoding(encoding)
58
+ end
59
+
63
60
  def to_s
64
- value
61
+ "#{super}: #{display_string(value_string)}"
65
62
  end
66
63
  end
67
64
 
68
- # The key had no value stored in the cache.
65
+ # @private
69
66
  class Miss < GetResponse
70
67
  def miss?
71
68
  true
72
69
  end
73
70
  end
74
71
 
75
- # There was a problem getting the value from the cache.
72
+ # @private
76
73
  class Error < GetResponse
77
74
  include Momento::Response::Error
78
75
  end
@@ -0,0 +1,37 @@
1
+ require_relative 'response_builder'
2
+ require_relative 'cacheclient_pb'
3
+ require_relative 'get_response'
4
+
5
+ module Momento
6
+ # @private
7
+ class GetResponseBuilder < ResponseBuilder
8
+ # Build a Momento::GetResponse from a block of code
9
+ # which returns a Momento::ControlClient::GetResponse.
10
+ #
11
+ # @return [Momento::GetResponse]
12
+ # @raise [StandardError] when the exception is not recognized.
13
+ # @raise [TypeError] when the response is not recognized.
14
+ def from_block
15
+ response = yield
16
+ rescue *RESCUED_EXCEPTIONS => e
17
+ GetResponse::Error.new(exception: e, context: context)
18
+ else
19
+ from_response(response)
20
+ end
21
+
22
+ private
23
+
24
+ def from_response(response)
25
+ raise TypeError unless response.is_a?(Momento::CacheClient::GetResponse)
26
+
27
+ case response.result
28
+ when :Hit
29
+ GetResponse::Hit.new(grpc_response: response)
30
+ when :Miss
31
+ GetResponse::Miss.new
32
+ else
33
+ raise "Unknown get result: #{response.result}"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,31 +1,39 @@
1
- require 'grpc'
2
- require 'momento/controlclient_pb'
1
+ require_relative 'response/error'
3
2
 
4
3
  module Momento
5
- # Responses specific to list_caches.
4
+ # A response from listing the caches.
5
+ #
6
+ # Each response is a single page of caches, there may be additional pages.
7
+ # Use Momento::SimpleCacheClient#caches to efficiently get the whole list.
6
8
  class ListCachesResponse < Response
7
- # Build a Momento::ListCachesResponse from a block of code
8
- # which returns a Momento::ControlClient::ListCachesResponse.
9
- #
10
- # @return [Momento::ListCachesResponse]
11
- # @raise [StandardError] when the exception is not recognized.
12
- # @raise [TypeError] when the response is not recognized.
13
- def self.from_block
14
- response = yield
15
- rescue GRPC::BadStatus => e
16
- Error.new(grpc_exception: e)
17
- else
18
- raise TypeError unless response.is_a?(Momento::ControlClient::ListCachesResponse)
19
-
20
- return Success.new(grpc_response: response)
21
- end
22
-
9
+ # Did it get a page of caches?
10
+ # @return [Boolean]
23
11
  def success?
24
12
  false
25
13
  end
26
14
 
27
- # A Momento resposne with a page of caches.
15
+ # The names of the caches in this page.
16
+ # @return [Array,nil]
17
+ def cache_names
18
+ nil
19
+ end
20
+
21
+ # A token to fetch the next page.
22
+ # The last page will have a blank token.
23
+ # @return [String,nil]
24
+ def next_token
25
+ nil
26
+ end
27
+
28
+ # @!method to_s
29
+ # Displays the response and the list of cache names.
30
+ # The list of cache names will be truncated.
31
+ # @return [String]
32
+
33
+ # @private
28
34
  class Success < ListCachesResponse
35
+ CACHE_NAMES_TO_DISPLAY = 5
36
+
29
37
  # rubocop:disable Lint/MissingSuper
30
38
  def initialize(grpc_response:)
31
39
  @grpc_response = grpc_response
@@ -43,9 +51,25 @@ module Momento
43
51
  def next_token
44
52
  @grpc_response.next_token
45
53
  end
54
+
55
+ def to_s
56
+ "#{super}: #{display_cache_names}"
57
+ end
58
+
59
+ private
60
+
61
+ def display_cache_names
62
+ display = cache_names.first(CACHE_NAMES_TO_DISPLAY).join(", ")
63
+
64
+ if cache_names.size > CACHE_NAMES_TO_DISPLAY
65
+ "#{display}, ..."
66
+ else
67
+ display
68
+ end
69
+ end
46
70
  end
47
71
 
48
- # There was an error listing the caches.
72
+ # @private
49
73
  class Error < ListCachesResponse
50
74
  include ::Momento::Response::Error
51
75
  end
@@ -0,0 +1,25 @@
1
+ require 'grpc'
2
+ require_relative 'controlclient_pb'
3
+
4
+ module Momento
5
+ # @private
6
+ class ListCachesResponseBuilder < ResponseBuilder
7
+ # Build a Momento::ListCachesResponse from a block of code
8
+ # which returns a Momento::ControlClient::ListCachesResponse..
9
+ #
10
+ # @return [Momento::ListCachesResponse]
11
+ # @raise [StandardError] when the exception is not recognized.
12
+ # @raise [TypeError] when the response is not recognized.
13
+ def from_block
14
+ response = yield
15
+ rescue GRPC::BadStatus => e
16
+ ListCachesResponse::Error.new(
17
+ exception: e, context: context
18
+ )
19
+ else
20
+ raise TypeError unless response.is_a?(::Momento::ControlClient::ListCachesResponse)
21
+
22
+ return ListCachesResponse::Success.new(grpc_response: response)
23
+ end
24
+ end
25
+ end
@@ -1,16 +1,23 @@
1
1
  module Momento
2
2
  class Response
3
3
  # A module for responses which contain errors.
4
+ # @private
4
5
  module Error
5
- attr_accessor :grpc_exception
6
+ attr_reader :error
6
7
 
7
- def initialize(grpc_exception:)
8
- @grpc_exception = grpc_exception
8
+ def initialize(exception:, context: {})
9
+ @error = Momento::ErrorBuilder.from_exception(
10
+ exception, context: context
11
+ ).freeze
9
12
  end
10
13
 
11
14
  def error?
12
15
  true
13
16
  end
17
+
18
+ def to_s
19
+ "#{super}: #{error.message}"
20
+ end
14
21
  end
15
22
  end
16
23
  end
@@ -1,17 +1,70 @@
1
1
  require 'grpc'
2
2
  require_relative 'response/error'
3
+ require_relative 'error'
4
+ require_relative 'error/grpc_details'
5
+ require_relative 'error/transport_details'
6
+ require_relative 'error_builder'
7
+ require_relative 'response_builder'
3
8
  require_relative 'create_cache_response'
9
+ require_relative 'create_cache_response_builder'
4
10
  require_relative 'delete_response'
11
+ require_relative 'delete_response_builder'
5
12
  require_relative 'delete_cache_response'
13
+ require_relative 'delete_cache_response_builder'
6
14
  require_relative 'get_response'
15
+ require_relative 'get_response_builder'
7
16
  require_relative 'list_caches_response'
17
+ require_relative 'list_caches_response_builder'
8
18
  require_relative 'set_response'
19
+ require_relative 'set_response_builder'
9
20
 
10
21
  module Momento
11
- # A superclass for all Momento responses.
22
+ # The response from a Momento service request.
23
+ #
24
+ # {Momento::SimpleCacheClient} returns a response for both success
25
+ # and error, as well as other states. See the documenation for each
26
+ # type of response for more.
27
+ #
28
+ # You can always check for an error response with
29
+ # `response.error?` and get the error itself with `response.error`.
30
+ #
31
+ # `response.error` is an Exception and can be raised. It contains
32
+ # additional information about the error, see {Momento::Error} for
33
+ # more information.
34
+ #
35
+ # @see Momento::Error
12
36
  class Response
37
+ MAX_STRING_DISPLAY_LENGTH = 32
38
+ private_constant :MAX_STRING_DISPLAY_LENGTH
39
+
40
+ # Returns the error portion of the response, if any.
41
+ #
42
+ # @return [Momento::Error, nil]
43
+ def error
44
+ nil
45
+ end
46
+
47
+ # Is the response an error?
48
+ #
49
+ # @return [Boolean]
13
50
  def error?
14
51
  false
15
52
  end
53
+
54
+ # Displays the type of response and additional info, if any.
55
+ # @return [String]
56
+ def to_s
57
+ self.class.to_s
58
+ end
59
+
60
+ protected
61
+
62
+ def display_string(string, max_length: MAX_STRING_DISPLAY_LENGTH)
63
+ if string.length < max_length
64
+ string
65
+ else
66
+ "#{string[0, max_length]}..."
67
+ end
68
+ end
16
69
  end
17
70
  end
@@ -0,0 +1,18 @@
1
+ module Momento
2
+ # A superclass for building responses.
3
+ #
4
+ # @private
5
+ class ResponseBuilder
6
+ attr_accessor :context
7
+
8
+ RESCUED_EXCEPTIONS = ErrorBuilder::EXCEPTION_MAP.keys.freeze
9
+
10
+ def initialize(context: {})
11
+ @context = context
12
+ end
13
+
14
+ def from_block
15
+ raise NotImplementedError
16
+ end
17
+ end
18
+ end
@@ -1,37 +1,37 @@
1
- require 'grpc'
2
- require 'momento/cacheclient_pb'
1
+ require_relative 'response/error'
3
2
 
4
3
  module Momento
5
- # Responses specific to set.
4
+ # A response from setting a key.
6
5
  class SetResponse < Response
7
- # Build a Momento::SetResponse from a block of code
8
- # which returns a Momento::ControlClient::SetResponse.
9
- #
10
- # @return [Momento::SetResponse]
11
- # @raise [StandardError] when the exception is not recognized.
12
- # @raise [TypeError] when the response is not recognized.
13
- def self.from_block
14
- response = yield
15
- rescue GRPC::BadStatus => e
16
- Error.new(grpc_exception: e)
17
- else
18
- raise TypeError unless response.is_a?(Momento::CacheClient::SetResponse)
19
-
20
- Success.new
21
- end
22
-
6
+ # Was the key/value pair added to the cache?
7
+ # @return [Boolean]
23
8
  def success?
24
9
  false
25
10
  end
26
11
 
27
- # The item was set.
12
+ # @private
28
13
  class Success < SetResponse
14
+ attr_accessor :key, :value
15
+
16
+ # rubocop:disable Lint/MissingSuper
17
+ def initialize(key:, value:)
18
+ @key = key
19
+ @value = value
20
+
21
+ return
22
+ end
23
+ # rubocop:enable Lint/MissingSuper
24
+
29
25
  def success?
30
26
  true
31
27
  end
28
+
29
+ def to_s
30
+ "#{super}: '#{display_string(key)}' = '#{display_string(value)}'"
31
+ end
32
32
  end
33
33
 
34
- # There was an error setting the item.
34
+ # @private
35
35
  class Error < SetResponse
36
36
  include Momento::Response::Error
37
37
  end
@@ -0,0 +1,25 @@
1
+ require 'grpc'
2
+ require_relative 'cacheclient_pb'
3
+
4
+ module Momento
5
+ # Builds SetResponses
6
+ #
7
+ # @private
8
+ class SetResponseBuilder < ResponseBuilder
9
+ # Build a Momento::SetResponse from a block of code
10
+ # which returns a Momento::CacheClient::SetResponse..
11
+ #
12
+ # @return [Momento::SetResponse]
13
+ # @raise [StandardError] when the exception is not recognized.
14
+ # @raise [TypeError] when the response is not recognized.
15
+ def from_block
16
+ response = yield
17
+ rescue *RESCUED_EXCEPTIONS => e
18
+ SetResponse::Error.new(exception: e, context: context)
19
+ else
20
+ raise TypeError unless response.is_a?(::Momento::CacheClient::SetResponse)
21
+
22
+ SetResponse::Success.new(key: context[:key], value: context[:value])
23
+ end
24
+ end
25
+ end