momento 0.1.0 → 0.4.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.release-please-manifest.json +3 -0
- data/.rubocop.yml +26 -1
- data/.ruby-version +1 -1
- data/.yardopts +2 -0
- data/CHANGELOG.md +119 -0
- data/CONTRIBUTING.md +6 -7
- data/Gemfile +1 -6
- data/Gemfile.lock +34 -24
- data/README.md +143 -38
- data/README.template.md +93 -0
- data/examples/.gitignore +1 -0
- data/examples/Gemfile +5 -0
- data/examples/README.md +34 -0
- data/examples/compact.rb +47 -0
- data/examples/example.rb +72 -0
- data/examples/file.rb +58 -0
- data/lib/README-generating-pb.txt +1 -1
- data/lib/momento/auth/credential_provider.rb +78 -0
- data/lib/momento/cache_client.rb +457 -0
- data/lib/momento/collection_ttl.rb +79 -0
- data/lib/momento/config/configuration.rb +16 -0
- data/lib/momento/config/configurations.rb +17 -0
- data/lib/momento/config/transport/grpc_configuration.rb +25 -0
- data/lib/momento/config/transport/static_transport_strategy.rb +12 -0
- data/lib/momento/config/transport/transport_strategy.rb +16 -0
- data/lib/momento/error/grpc_details.rb +38 -0
- data/lib/momento/error/transport_details.rb +20 -0
- data/lib/momento/error/types.rb +247 -0
- data/lib/momento/error.rb +54 -0
- data/lib/momento/error_builder.rb +50 -0
- data/lib/momento/exceptions.rb +7 -0
- data/lib/momento/generated/README.md +16 -0
- data/lib/momento/generated/auth_pb.rb +52 -0
- data/lib/momento/generated/auth_services_pb.rb +27 -0
- data/lib/momento/generated/cacheclient_pb.rb +203 -0
- data/lib/momento/generated/cacheclient_services_pb.rb +90 -0
- data/lib/momento/generated/cacheping_pb.rb +38 -0
- data/lib/momento/generated/cacheping_services_pb.rb +23 -0
- data/lib/momento/generated/cachepubsub_pb.rb +48 -0
- data/lib/momento/generated/cachepubsub_services_pb.rb +56 -0
- data/lib/momento/generated/common_pb.rb +44 -0
- data/lib/momento/generated/controlclient_pb.rb +72 -0
- data/lib/momento/generated/controlclient_services_pb.rb +35 -0
- data/lib/momento/generated/extensions_pb.rb +35 -0
- data/lib/momento/generated/generate_protos.sh +47 -0
- data/lib/momento/generated/leaderboard_pb.rb +56 -0
- data/lib/momento/generated/leaderboard_services_pb.rb +57 -0
- data/lib/momento/generated/permissionmessages_pb.rb +48 -0
- data/lib/momento/generated/token_pb.rb +43 -0
- data/lib/momento/generated/token_services_pb.rb +23 -0
- data/lib/momento/generated/webhook_pb.rb +49 -0
- data/lib/momento/generated/webhook_services_pb.rb +32 -0
- data/lib/momento/{create_cache_response.rb → response/control/create_cache_response.rb} +33 -24
- data/lib/momento/{delete_cache_response.rb → response/control/delete_cache_response.rb} +28 -21
- data/lib/momento/response/control/list_caches_response.rb +80 -0
- data/lib/momento/response/delete_response.rb +45 -0
- data/lib/momento/response/error.rb +10 -3
- data/lib/momento/response/get_response.rb +107 -0
- data/lib/momento/response/response.rb +67 -0
- data/lib/momento/response/response_builder.rb +18 -0
- data/lib/momento/response/set_response.rb +59 -0
- data/lib/momento/response/sort_order.rb +11 -0
- data/lib/momento/response/sorted_set/sorted_set_fetch_response.rb +107 -0
- data/lib/momento/response/sorted_set/sorted_set_put_element_response.rb +44 -0
- data/lib/momento/response/sorted_set/sorted_set_put_elements_response.rb +44 -0
- data/lib/momento/ttl.rb +48 -0
- data/lib/momento/version.rb +2 -1
- data/lib/momento.rb +6 -1
- data/momento.gemspec +6 -3
- data/release-please-config.json +15 -0
- data/sig/momento/auth/credential_provider.rbs +11 -0
- data/sig/momento/cache_client.rbs +12 -0
- data/sig/momento/collection_ttl.rbs +22 -0
- data/sig/momento/config/configuration.rbs +9 -0
- data/sig/momento/config/configurations.rbs +9 -0
- data/sig/momento/config/transport/grpc_configuration.rbs +7 -0
- data/sig/momento/config/transport/transport_strategy.rbs +7 -0
- data/sig/momento/list_caches_response.rbs +7 -0
- data/sig/momento/sorted_set_fetch_response.rbs +13 -0
- data/sig/momento/sorted_set_put_element_response.rbs +5 -0
- data/sig/momento/sorted_set_put_elements_response.rbs +5 -0
- metadata +127 -28
- data/examples/basic.rb +0 -45
- data/lib/momento/cacheclient_pb.rb +0 -332
- data/lib/momento/cacheclient_services_pb.rb +0 -42
- data/lib/momento/controlclient_pb.rb +0 -71
- data/lib/momento/controlclient_services_pb.rb +0 -29
- data/lib/momento/delete_response.rb +0 -33
- data/lib/momento/get_response.rb +0 -80
- data/lib/momento/list_caches_response.rb +0 -53
- data/lib/momento/response.rb +0 -17
- data/lib/momento/set_response.rb +0 -39
- data/lib/momento/simple_cache_client.rb +0 -204
data/lib/momento/set_response.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
require 'grpc'
|
2
|
-
require 'momento/cacheclient_pb'
|
3
|
-
|
4
|
-
module Momento
|
5
|
-
# Responses specific to set.
|
6
|
-
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
|
-
|
23
|
-
def success?
|
24
|
-
false
|
25
|
-
end
|
26
|
-
|
27
|
-
# The item was set.
|
28
|
-
class Success < SetResponse
|
29
|
-
def success?
|
30
|
-
true
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
# There was an error setting the item.
|
35
|
-
class Error < SetResponse
|
36
|
-
include Momento::Response::Error
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
@@ -1,204 +0,0 @@
|
|
1
|
-
require 'jwt'
|
2
|
-
require 'momento/cacheclient_services_pb'
|
3
|
-
require 'momento/controlclient_services_pb'
|
4
|
-
require 'momento/response'
|
5
|
-
|
6
|
-
module Momento
|
7
|
-
# A simple client for Momento.
|
8
|
-
#
|
9
|
-
# @example
|
10
|
-
# client = Momento::SimpleCacheClient.new(
|
11
|
-
# auth_token: jwt,
|
12
|
-
# default_ttl: 10_000
|
13
|
-
# )
|
14
|
-
#
|
15
|
-
# response = client.get("my_cache", "key")
|
16
|
-
# if response.hit?
|
17
|
-
# puts "We got #{response}"
|
18
|
-
# elsif response.miss?
|
19
|
-
# puts "It's not in the cache"
|
20
|
-
# elsif response.error?
|
21
|
-
# puts "The front fell off."
|
22
|
-
# end
|
23
|
-
class SimpleCacheClient
|
24
|
-
VERSION = Momento::VERSION
|
25
|
-
CACHE_CLIENT_STUB_CLASS = CacheClient::Scs::Stub
|
26
|
-
CONTROL_CLIENT_STUB_CLASS = ControlClient::ScsControl::Stub
|
27
|
-
|
28
|
-
# The default time to live, in milliseconds.
|
29
|
-
attr_accessor :default_ttl
|
30
|
-
|
31
|
-
# @param auth_token [String] the JWT for your Momento account
|
32
|
-
# @param default_ttl [Integer]
|
33
|
-
def initialize(auth_token:, default_ttl:)
|
34
|
-
@auth_token = auth_token
|
35
|
-
@default_ttl = default_ttl
|
36
|
-
load_endpoints_from_token
|
37
|
-
end
|
38
|
-
|
39
|
-
# Get a value in a cache.
|
40
|
-
#
|
41
|
-
# Momento only stores bytes; the returned value will be encoded as ASCII-8BIT.
|
42
|
-
#
|
43
|
-
# @param cache_name [String]
|
44
|
-
# @param key [String] must only contain ASCII characters
|
45
|
-
# @return [Momento::GetResponse]
|
46
|
-
def get(cache_name, key)
|
47
|
-
return GetResponse.from_block do
|
48
|
-
cache_stub.get(
|
49
|
-
CacheClient::GetRequest.new(cache_key: to_bytes(key)),
|
50
|
-
metadata: { cache: cache_name }
|
51
|
-
)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
# Set a value in a cache.
|
56
|
-
#
|
57
|
-
# If ttl is not set, it will use the default_ttl.
|
58
|
-
#
|
59
|
-
# @param cache_name [String]
|
60
|
-
# @param key [String] must only contain ASCII characters
|
61
|
-
# @param value [String] the value to cache
|
62
|
-
# @param ttl [Integer] time to live, in milliseconds.
|
63
|
-
# @return [Momento::SetResponse]
|
64
|
-
def set(cache_name, key, value, ttl: default_ttl)
|
65
|
-
return SetResponse.from_block do
|
66
|
-
req = CacheClient::SetRequest.new(
|
67
|
-
cache_key: to_bytes(key),
|
68
|
-
cache_body: to_bytes(value),
|
69
|
-
ttl_milliseconds: ttl
|
70
|
-
)
|
71
|
-
|
72
|
-
cache_stub.set(req, metadata: { cache: cache_name })
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
# Delete a key in a cache.
|
77
|
-
#
|
78
|
-
# @param cache_name [String]
|
79
|
-
# @param key [String] must only contain ASCII characters
|
80
|
-
# @return [Momento::DeleteResponse]
|
81
|
-
def delete(cache_name, key)
|
82
|
-
return DeleteResponse.from_block do
|
83
|
-
cache_stub.delete(
|
84
|
-
CacheClient::DeleteRequest.new(cache_key: to_bytes(key)),
|
85
|
-
metadata: { cache: cache_name }
|
86
|
-
)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
# Create a new Momento cache.
|
91
|
-
#
|
92
|
-
# @param name [String] the name of the cache to create.
|
93
|
-
# @return [Momento::CreateCacheResponse] the response from Momento.
|
94
|
-
def create_cache(name)
|
95
|
-
return CreateCacheResponse.from_block do
|
96
|
-
control_stub.create_cache(
|
97
|
-
ControlClient::CreateCacheRequest.new(cache_name: name)
|
98
|
-
)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
# Delete an existing Momento cache.
|
103
|
-
#
|
104
|
-
# @param name [String] the name of the cache to delete.
|
105
|
-
# @return [Momento::DeleteCacheResponse] the response from Momento.
|
106
|
-
def delete_cache(name)
|
107
|
-
return DeleteCacheResponse.from_block do
|
108
|
-
control_stub.delete_cache(
|
109
|
-
ControlClient::DeleteCacheRequest.new(cache_name: name)
|
110
|
-
)
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
# List a page of your caches.
|
115
|
-
#
|
116
|
-
# The next_token indicates which page to fetch.
|
117
|
-
# If nil or "" it will fetch the first page. Default is to fetch the first page.
|
118
|
-
#
|
119
|
-
# @params next_token [String, nil] the token of the page to request
|
120
|
-
# @return [Momento::ListCachesResponse]
|
121
|
-
def list_caches(next_token: "")
|
122
|
-
return ListCachesResponse.from_block do
|
123
|
-
control_stub.list_caches(
|
124
|
-
ControlClient::ListCachesRequest.new(next_token: next_token)
|
125
|
-
)
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
# Lists the names of all your caches.
|
130
|
-
#
|
131
|
-
# @return [Enumerator::Lazy<String>] the cache names
|
132
|
-
# @raise [GRPC::BadStatus]
|
133
|
-
# rubocop:disable Metrics/MethodLength
|
134
|
-
def caches
|
135
|
-
Enumerator.new do |yielder|
|
136
|
-
next_token = ""
|
137
|
-
|
138
|
-
loop do
|
139
|
-
response = list_caches(next_token: next_token)
|
140
|
-
raise response.grpc_exception if response.is_a? Momento::Response::Error
|
141
|
-
|
142
|
-
response.cache_names.each do |name|
|
143
|
-
yielder << name
|
144
|
-
end
|
145
|
-
|
146
|
-
break if response.next_token == ''
|
147
|
-
|
148
|
-
next_token = response.next_token
|
149
|
-
end
|
150
|
-
end.lazy
|
151
|
-
end
|
152
|
-
# rubocop:enable Metrics/MethodLength
|
153
|
-
|
154
|
-
private
|
155
|
-
|
156
|
-
def cache_stub
|
157
|
-
@cache_stub ||= CACHE_CLIENT_STUB_CLASS.new(@cache_endpoint, combined_credentials)
|
158
|
-
end
|
159
|
-
|
160
|
-
def control_stub
|
161
|
-
@control_stub ||= CONTROL_CLIENT_STUB_CLASS.new(@control_endpoint, combined_credentials)
|
162
|
-
end
|
163
|
-
|
164
|
-
def combined_credentials
|
165
|
-
@combined_credentials ||= make_combined_credentials
|
166
|
-
end
|
167
|
-
|
168
|
-
def load_endpoints_from_token
|
169
|
-
claim = JWT.decode(@auth_token, nil, false).first
|
170
|
-
|
171
|
-
@control_endpoint = claim["cp"]
|
172
|
-
@cache_endpoint = claim["c"]
|
173
|
-
end
|
174
|
-
|
175
|
-
def make_combined_credentials
|
176
|
-
# :nocov:
|
177
|
-
auth_proc = proc do
|
178
|
-
{ authorization: @auth_token, agent: "ruby:#{VERSION}" }
|
179
|
-
end
|
180
|
-
# :nocov:
|
181
|
-
|
182
|
-
call_creds = GRPC::Core::CallCredentials.new(auth_proc)
|
183
|
-
|
184
|
-
return GRPC::Core::ChannelCredentials.new.compose(call_creds)
|
185
|
-
end
|
186
|
-
|
187
|
-
# Ruby uses String for bytes. GRPC wants a String encoded as ASCII.
|
188
|
-
# GRPC will re-encode a String, but treats it as characters; GRPC will
|
189
|
-
# raise if you pass a String with non-ASCII characters.
|
190
|
-
# So we do the re-encoding ourselves in a way that treats the String as
|
191
|
-
# bytes and will not raise. The data is not changed.
|
192
|
-
#
|
193
|
-
# A duplicate String is returned, but since Ruby is copy-on-write it
|
194
|
-
# does not copy the data.
|
195
|
-
#
|
196
|
-
# @param string [String] the string to make safe for GRPC bytes
|
197
|
-
# @return [String] a duplicate safe to use as GRPC bytes
|
198
|
-
def to_bytes(string)
|
199
|
-
# dup in case the value is frozen and to avoid changing the value's encoding
|
200
|
-
# for the caller.
|
201
|
-
return string.dup.force_encoding(Encoding::ASCII_8BIT)
|
202
|
-
end
|
203
|
-
end
|
204
|
-
end
|