momento 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 28402c3809544906082ae277798d4ffd2f82a20bf45fa9869edca7e3adf9bd9a
4
- data.tar.gz: 4c8bac6308cc7a5ef23f025d909b6572907c4c1fc1c6386775b87006edee1a4c
3
+ metadata.gz: 6ae73ad26d383968c4b0335be41cc9aa1b95078cdbff65b012d54f0309573df3
4
+ data.tar.gz: c7717a54a117809b0c0c2cc1fbb2576a1f8046c3577627922055ec89093f38de
5
5
  SHA512:
6
- metadata.gz: 9fad68ffd8d9dd81deee23655464af02014ddc3348bd07f0c95d9534d2acf7c9cac858f175a62489d121f3ebce9cd1c641f27dfdafac1665239697b5dcd326dd
7
- data.tar.gz: a38ac7f169d9917e015b3da3647ec01950fb1b769a11166c16df6a1013e398c3b2e910e292c6721e204c9b3a04a5237be25bfea58e3ad5ebfed43866a990ee48
6
+ metadata.gz: 4723e7ebb12ae06ffd1a0c7fe75c5c5eb516bec1e9474c8283b1f4412d319a9c2e402262b4141c6bd90840f74856915182f85ae082c7b1615d85eba397aec4cf
7
+ data.tar.gz: 5dffb98bf9c697786ef1bd5bb2c92bd406738c1c4564cdf5e09a76f760254abd4409eadd8fc0862870b1e1a8f71af6d483315f010a1e432539d1842396a86daf
@@ -0,0 +1,3 @@
1
+ {
2
+ ".": "0.2.0"
3
+ }
data/.rubocop.yml CHANGED
@@ -11,13 +11,22 @@ AllCops:
11
11
  - 'lib/**/*_pb.rb'
12
12
  - 'vendor/**/*'
13
13
 
14
+ Layout/ArgumentAlignment:
15
+ EnforcedStyle: with_fixed_indentation
16
+
14
17
  Layout/MultilineMethodCallIndentation:
15
18
  EnforcedStyle: indented
16
19
 
20
+ Layout/MultilineMethodCallBraceLayout:
21
+ EnforcedStyle: new_line
22
+
17
23
  Lint/AmbiguousBlockAssociation:
18
24
  # For rspec
19
25
  AllowedPatterns: ['change']
20
26
 
27
+ Metrics/MethodLength:
28
+ Max: 15
29
+
21
30
  # The rspec defaults are way too tight.
22
31
  RSpec/ExampleLength:
23
32
  Max: 15
@@ -25,6 +34,9 @@ RSpec/MultipleExpectations:
25
34
  Max: 4
26
35
  RSpec/MultipleMemoizedHelpers:
27
36
  Max: 10
37
+ # I disagree with this one. It should be clear you're using the subject.
38
+ RSpec/NamedSubject:
39
+ Enabled: false
28
40
  RSpec/NestedGroups:
29
41
  Max: 4
30
42
 
data/.yardopts ADDED
@@ -0,0 +1,2 @@
1
+ --no-private
2
+ --exclude _pb.rb$
data/CHANGELOG.md ADDED
@@ -0,0 +1,28 @@
1
+ # Changelog
2
+
3
+ ## [0.2.0](https://github.com/momentohq/client-sdk-ruby/compare/momento-v0.1.0...momento/v0.2.0) (2022-12-07)
4
+
5
+ ### Breaking Changes
6
+
7
+ * TTLs are now in seconds.
8
+ * `response.value` was split into `response.value_bytes` and `response.value_string`
9
+ * Stringifying a Momento::GetResponse no longer gives you the value.
10
+ * `response.exception` is now `response.error.cause`
11
+
12
+ ### Features
13
+
14
+ * Momento::Responses now stringfy with useful information.
15
+ * `response.error` is a real Exception and can be raised
16
+ * `response.error` is now a Momento::Error with expanded error information.
17
+ * nil and non-String keys, values, and cache names are now TypeErrors.
18
+ * Non-ASCII cache names are now InvalidArgument responses.
19
+ * An invalid TTL will raise ArgumentError.
20
+ * All response methods are discoverable in an IDE.
21
+ * Improve the README
22
+ * Complete YARD class and method documentation
23
+ * More examples, and instructions how to run them.
24
+ * Document how error handling works.
25
+
26
+ ## 0.1.0 (2022-11-21)
27
+
28
+ First release.
data/CONTRIBUTING.md CHANGED
@@ -36,7 +36,7 @@ This will install both the runtime and development dependencies.
36
36
 
37
37
  ## Trying it out
38
38
 
39
- You can run code from the repository's `lib/` directory by adding `-Ilib` to your ruby commands. For example, `ruby -Ilib examples/basic.rb` or `irb -Ilib`. This will use the repository's code even if you have the gem installed.
39
+ You can run code from the repository's `lib/` directory by adding `-Ilib` to your ruby commands. For example, `ruby -Ilib examples/example.rb` or `irb -Ilib`. This will use the repository's code even if you have the gem installed.
40
40
 
41
41
  You can also build a gem from local sources and install it. Run `bundle exec rake install`.
42
42
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- momento (0.1.0)
4
+ momento (0.2.0)
5
5
  grpc (~> 1)
6
6
  jwt (~> 2)
7
7
 
@@ -23,8 +23,8 @@ GEM
23
23
  activesupport (>= 5.0.0)
24
24
  faker (3.0.0)
25
25
  i18n (>= 1.8.11, < 2)
26
- google-protobuf (3.21.9-x86_64-darwin)
27
- google-protobuf (3.21.9-x86_64-linux)
26
+ google-protobuf (3.21.10-x86_64-darwin)
27
+ google-protobuf (3.21.10-x86_64-linux)
28
28
  googleapis-common-protos-types (1.4.0)
29
29
  google-protobuf (~> 3.14)
30
30
  grpc (1.50.0-x86_64-darwin)
@@ -94,6 +94,9 @@ GEM
94
94
  tzinfo (2.0.5)
95
95
  concurrent-ruby (~> 1.0)
96
96
  unicode-display_width (2.3.0)
97
+ webrick (1.7.0)
98
+ yard (0.9.28)
99
+ webrick (~> 1.7.0)
97
100
 
98
101
  PLATFORMS
99
102
  x86_64-darwin-20
@@ -113,6 +116,7 @@ DEPENDENCIES
113
116
  rubocop-rake (~> 0.6.0)
114
117
  rubocop-rspec (~> 2.15)
115
118
  simplecov (~> 0.21)
119
+ yard (~> 0.9)
116
120
 
117
121
  BUNDLED WITH
118
122
  2.3.24
data/README.md CHANGED
@@ -1,80 +1,175 @@
1
- # Momento
1
+ <head>
2
+ <meta name="Momento Ruby Client Library Documentation" content="Ruby client software development kit for Momento Serverless Cache">
3
+ </head>
4
+ <img src="https://docs.momentohq.com/img/logo.svg" alt="logo" width="400"/>
2
5
 
3
- Client for Momento serverless caching service.
6
+ [![project status](https://momentohq.github.io/standards-and-practices/badges/project-status-official.svg)](https://github.com/momentohq/standards-and-practices/blob/main/docs/momento-on-github.md)
7
+ [![project stability](https://momentohq.github.io/standards-and-practices/badges/project-stability-alpha.svg)](https://github.com/momentohq/standards-and-practices/blob/main/docs/momento-on-github.md)
4
8
 
5
- ## Installation
9
+ # Momento Ruby Client Library
10
+
11
+
12
+ :warning: Alpha SDK :warning:
13
+
14
+ This is an official Momento SDK, but the API is in an alpha stage and may be subject to backward-incompatible
15
+ changes. For more info, click on the alpha badge above.
16
+
17
+
18
+ Ruby client SDK for Momento Serverless Cache: a fast, simple, pay-as-you-go caching solution without
19
+ any of the operational overhead required by traditional caching solutions!
20
+
21
+
22
+
23
+ ## Getting Started :running:
24
+
25
+ ### Requirements
26
+
27
+ - A Momento Auth Token is required, you can generate one using
28
+ the [Momento CLI](https://github.com/momentohq/momento-cli).
29
+ - Ruby 2.7 or newer.
30
+
31
+ An IDE with good Ruby support, such as [RubyMine](https://www.jetbrains.com/ruby/), is recommended.
32
+
33
+ ### Examples
34
+
35
+ You can find this example code and more in [the examples directory](https://github.com/momentohq/client-sdk-ruby/tree/main/examples) of this repository.
36
+
37
+ ### Installation
6
38
 
7
39
  Install the gem and add to an application's Gemfile by executing:
8
40
 
9
- $ bundle add momento
41
+ ```sh
42
+ bundle add momento
43
+ ```
10
44
 
11
45
  If bundler is not being used to manage dependencies, install the gem by executing:
12
46
 
13
- $ gem install momento
47
+ ```sh
48
+ gem install momento
49
+ ```
14
50
 
15
- ### M1 or M2 Macs
51
+ #### Note: M1 or M2 Macs
16
52
 
17
- If you're using an M1 or M2 Mac, [you may have trouble installing the `grpc` gem](https://github.com/grpc/grpc/issues/31215).
53
+ If you're using an M1 or M2 Mac, you may have trouble installing the `grpc` gem; [see this issue for more information](https://github.com/grpc/grpc/issues/31215).
18
54
 
19
55
  [A work around](https://github.com/grpc/grpc/pull/31151#issuecomment-1310321807) is to run `bundle config build.grpc --with-ldflags="-Wl,-undefined,dynamic_lookup"` before doing `bundle install`.
20
56
 
21
- ## Requirements
22
-
23
- * A Momento Auth Token is required, you can generate one using the Momento CLI
24
- * Ruby >= 2.7
57
+ ### Usage
25
58
 
26
- ## Usage
59
+ ```ruby
60
+ # An example of the basic functionality of
61
+ # Momento::SimpleCacheClient.
27
62
 
28
- ```
29
63
  require 'momento'
30
64
 
31
65
  # Get your Momento token from an environment variable.
32
- token = ENV.fetch('MOMENTO_AUTH_TOKEN')
66
+ TOKEN = ENV.fetch('MOMENTO_AUTH_TOKEN')
33
67
 
34
- # Cached items will be deleted after 30 seconds.
35
- ttl = 30_000
68
+ # Cached items will be deleted after 12.5 seconds.
69
+ TTL_SECONDS = 12.5
70
+
71
+ # The name of the cache to create *and delete*
72
+ CACHE_NAME = ENV.fetch('MOMENTO_CACHE_NAME')
36
73
 
37
74
  # Instantiate a Momento client.
38
75
  client = Momento::SimpleCacheClient.new(
39
- auth_token: token,
40
- default_ttl: ttl
76
+ auth_token: TOKEN,
77
+ default_ttl: TTL_SECONDS
41
78
  )
42
79
 
43
- # Create a cache named "test_cache" to play with.
44
- response = client.create_cache("test_cache")
45
- if response.success? || response.already_exists?
46
- puts "Created the cache, or it already exists."
80
+ # Create a cache to play with.
81
+ response = client.create_cache(CACHE_NAME)
82
+ if response.success?
83
+ puts "Created the cache."
84
+ elsif response.already_exists?
85
+ puts "Cache already exists."
47
86
  elsif response.error?
48
- raise "Couldn't create a cache: #{response}"
49
- else
50
- raise
87
+ raise "Couldn't create a cache: #{response.error}"
51
88
  end
52
89
 
90
+ # List our caches.
91
+ puts "Caches: #{client.caches.to_a.join(", ")}"
92
+
53
93
  # Put an item in the cache.
54
- response = client.set("test_cache", "key", "You cached something!")
94
+ response = client.set(CACHE_NAME, "key", "You cached something!")
55
95
  if response.success?
56
96
  puts "Set an item in the cache."
57
97
  elsif response.error?
58
- raise "Couldn't set an item in the cache: #{response}"
59
- else
60
- raise
98
+ raise "Couldn't set an item in the cache: #{response.error}"
61
99
  end
62
100
 
63
101
  # And get it back.
64
- response = client.get("test_cache", "key")
102
+ response = client.get(CACHE_NAME, "key")
65
103
  if response.hit?
66
- puts "Cache returned: #{response.value}"
104
+ puts "Cache returned: #{response.value_string}"
67
105
  elsif response.miss?
68
106
  puts "The item wasn't found in the cache."
69
107
  elsif response.error?
70
- raise "Couldn't get an item from the cache: #{response}"
71
- else
72
- raise
108
+ raise "Couldn't get an item from the cache: #{response.error}"
109
+ end
110
+
111
+ # Now delete it.
112
+ response = client.delete(CACHE_NAME, "key")
113
+ if response.success?
114
+ puts "Key/value deleted."
115
+ elsif response.error?
116
+ raise "Couldn't delete an item from the cache: #{response.error}"
117
+ end
118
+
119
+ # And delete our test cache.
120
+ response = client.delete_cache(CACHE_NAME)
121
+ if response.success?
122
+ puts "Deleted the cache."
123
+ elsif response.error?
124
+ raise "Couldn't create a cache: #{response.error}"
125
+ end
126
+
127
+ ```
128
+
129
+ ### Error Handling
130
+
131
+ [Momento::SimpleCacheClient](https://github.com/momentohq/client-sdk-ruby/blob/main/lib/momento/simple_cache_client.rb) follows the philosophy that when working with a service,
132
+ [exceptions are bugs](https://www.gomomento.com/blog/exceptions-are-bugs). Minor outages are a fact of life; they are normal rather than exceptional.
133
+
134
+ When there is a problem, Momento::SimpleCacheClient methods return an error response, the same as any other response. This makes errors more visible, allows your IDE to be more helpful in ensuring that you've handled the responses you care about.
135
+
136
+ Check if a response is an error with `response.error?`, get the error with `response.error`, and it can be raised as an exception with `raise response.error`. Generally, printing `response.error` tell you what you need to know, but you might want more details. Here's a contrived example.
137
+
138
+ ```ruby
139
+ # This is an invalid cache name. They must be ASCII-only.
140
+ cache_name = 'çåché nåme'
141
+ response = client.create_cache(cache_name)
142
+ if response.success?
143
+ puts "Created the cache"
144
+ elsif response.error?
145
+ error = response.error
146
+ puts "Creating the cache failed: #{error}"
147
+ case error
148
+ when Momento::Error::LimitExceededError
149
+ puts "We'll have to slow down"
150
+ when Momento::Error::PermissionError
151
+ puts "We'll have to fix our auth token"
152
+ when Momento::Error::InvalidArgumentError
153
+ puts "We can't make a cache named #{cache_name}"
154
+ end
73
155
  end
74
156
  ```
75
157
 
76
- You can run this code from [examples/basic.rb](examples/basic.rb).
158
+ Momento::SimpleCacheClient *will* raise exceptions for programmer mistakes such as passing the wrong type, typically an ArgumentError or TypeError. The exceptions are documented for each method.
159
+
160
+ See [Momento::Response](https://github.com/momentohq/client-sdk-ruby/blob/main/lib/momento/response.rb) for more about working with with error responses, and [Momento::Error](https://github.com/momentohq/client-sdk-ruby/blob/main/lib/momento/error.rb) for more about using errors.
161
+
162
+ ### Tuning
163
+
164
+ Coming soon.
165
+
166
+ ## Issues
167
+
168
+ Please report any bugs, issues, requests, and feedback via this repo's [issue tracker](https://github.com/momentohq/client-sdk-ruby/issues).
77
169
 
78
170
  ## Contributing
79
171
 
80
- See our [instructions for CONTRIBUTING](./CONTRIBUTING.md).
172
+ See our [instructions for CONTRIBUTING](https://github.com/momentohq/client-sdk-ruby/blob/main/CONTRIBUTING.md).
173
+
174
+ ----------------------------------------------------------------------------------------
175
+ For more info, visit our website at [https://gomomento.com](https://gomomento.com)!
@@ -0,0 +1,88 @@
1
+ {{ ossHeader }}
2
+
3
+ ## Getting Started :running:
4
+
5
+ ### Requirements
6
+
7
+ - A Momento Auth Token is required, you can generate one using
8
+ the [Momento CLI](https://github.com/momentohq/momento-cli).
9
+ - Ruby 2.7 or newer.
10
+
11
+ An IDE with good Ruby support, such as [RubyMine](https://www.jetbrains.com/ruby/), is recommended.
12
+
13
+ ### Examples
14
+
15
+ You can find this example code and more in [the examples directory](https://github.com/momentohq/client-sdk-ruby/tree/main/examples) of this repository.
16
+
17
+ ### Installation
18
+
19
+ Install the gem and add to an application's Gemfile by executing:
20
+
21
+ ```sh
22
+ bundle add momento
23
+ ```
24
+
25
+ If bundler is not being used to manage dependencies, install the gem by executing:
26
+
27
+ ```sh
28
+ gem install momento
29
+ ```
30
+
31
+ #### Note: M1 or M2 Macs
32
+
33
+ If you're using an M1 or M2 Mac, you may have trouble installing the `grpc` gem; [see this issue for more information](https://github.com/grpc/grpc/issues/31215).
34
+
35
+ [A work around](https://github.com/grpc/grpc/pull/31151#issuecomment-1310321807) is to run `bundle config build.grpc --with-ldflags="-Wl,-undefined,dynamic_lookup"` before doing `bundle install`.
36
+
37
+ ### Usage
38
+
39
+ ```ruby
40
+ {{ usageExampleCode }}
41
+ ```
42
+
43
+ ### Error Handling
44
+
45
+ [Momento::SimpleCacheClient](https://github.com/momentohq/client-sdk-ruby/blob/main/lib/momento/simple_cache_client.rb) follows the philosophy that when working with a service,
46
+ [exceptions are bugs](https://www.gomomento.com/blog/exceptions-are-bugs). Minor outages are a fact of life; they are normal rather than exceptional.
47
+
48
+ When there is a problem, Momento::SimpleCacheClient methods return an error response, the same as any other response. This makes errors more visible, allows your IDE to be more helpful in ensuring that you've handled the responses you care about.
49
+
50
+ Check if a response is an error with `response.error?`, get the error with `response.error`, and it can be raised as an exception with `raise response.error`. Generally, printing `response.error` tell you what you need to know, but you might want more details. Here's a contrived example.
51
+
52
+ ```ruby
53
+ # This is an invalid cache name. They must be ASCII-only.
54
+ cache_name = 'çåché nåme'
55
+ response = client.create_cache(cache_name)
56
+ if response.success?
57
+ puts "Created the cache"
58
+ elsif response.error?
59
+ error = response.error
60
+ puts "Creating the cache failed: #{error}"
61
+ case error
62
+ when Momento::Error::LimitExceededError
63
+ puts "We'll have to slow down"
64
+ when Momento::Error::PermissionError
65
+ puts "We'll have to fix our auth token"
66
+ when Momento::Error::InvalidArgumentError
67
+ puts "We can't make a cache named #{cache_name}"
68
+ end
69
+ end
70
+ ```
71
+
72
+ Momento::SimpleCacheClient *will* raise exceptions for programmer mistakes such as passing the wrong type, typically an ArgumentError or TypeError. The exceptions are documented for each method.
73
+
74
+ See [Momento::Response](https://github.com/momentohq/client-sdk-ruby/blob/main/lib/momento/response.rb) for more about working with with error responses, and [Momento::Error](https://github.com/momentohq/client-sdk-ruby/blob/main/lib/momento/error.rb) for more about using errors.
75
+
76
+ ### Tuning
77
+
78
+ Coming soon.
79
+
80
+ ## Issues
81
+
82
+ Please report any bugs, issues, requests, and feedback via this repo's [issue tracker](https://github.com/momentohq/client-sdk-ruby/issues).
83
+
84
+ ## Contributing
85
+
86
+ See our [instructions for CONTRIBUTING](https://github.com/momentohq/client-sdk-ruby/blob/main/CONTRIBUTING.md).
87
+
88
+ {{ ossFooter }}
@@ -0,0 +1 @@
1
+ test_copy.jpg
data/examples/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem 'momento', '~> 0.1.0'
@@ -0,0 +1,34 @@
1
+ <img src="https://docs.momentohq.com/img/logo.svg" alt="Momento logo" width="400"/>
2
+
3
+ # Momento Ruby Client Examples
4
+
5
+ ## Requirements
6
+
7
+ See the [project's README](https://github.com/momentohq/client-sdk-ruby/blob/main/README.md).
8
+
9
+ ## Running the examples
10
+
11
+ First, install the gem and its dependencies.
12
+
13
+ ```sh
14
+ bundle
15
+ ```
16
+
17
+ Then, set the required environment variables:
18
+
19
+ ```bash
20
+ export MOMENTO_AUTH_TOKEN=<YOUR_AUTH_TOKEN>
21
+ export CACHE_NAME=<YOUR_CACHE_NAME>
22
+ ```
23
+
24
+ And now you can run the example programs.
25
+
26
+ * example.rb - shows off all the basic functionality
27
+ * compact.rb - the same, written in a compact style
28
+ * file.rb - demonstrates how to cache a file
29
+
30
+ If you wish to use the version of momento in this repository, include the lib directory when you run the examples.
31
+
32
+ ```sh
33
+ ruby -I../lib example.rb
34
+ ```
@@ -0,0 +1,43 @@
1
+ # This does the same work as in example.rb, but demonstrates a more compact style.
2
+
3
+ require 'momento'
4
+
5
+ # Get your Momento token from an environment variable.
6
+ TOKEN = ENV.fetch('MOMENTO_AUTH_TOKEN')
7
+
8
+ # Cached items will be deleted after 12.5 seconds.
9
+ TTL_SECONDS = 12.5
10
+
11
+ # The name of the cache to create *and delete*
12
+ CACHE_NAME = ENV.fetch('MOMENTO_CACHE_NAME')
13
+
14
+ # Instantiate a Momento client.
15
+ client = Momento::SimpleCacheClient.new(
16
+ auth_token: TOKEN,
17
+ default_ttl: TTL_SECONDS
18
+ )
19
+
20
+ # Create a cache to play with.
21
+ # It might be created, or it might already exist.
22
+ response = client.create_cache(CACHE_NAME)
23
+ raise response.error if response.error?
24
+
25
+ # List our caches.
26
+ puts "Caches: #{client.caches.to_a.join(", ")}"
27
+
28
+ # Put an item in the cache.
29
+ response = client.set(CACHE_NAME, "key", "You cached something!")
30
+ raise response.error if response.error?
31
+
32
+ # And get it back.
33
+ response = client.get(CACHE_NAME, "key")
34
+ puts response.value_string || "The item wasn't found in the cache."
35
+ raise response.error if response.error?
36
+
37
+ # Now delete it.
38
+ response = client.delete(CACHE_NAME, "key")
39
+ raise response.error if response.error?
40
+
41
+ # And delete our test cache.
42
+ response = client.delete_cache(CACHE_NAME)
43
+ raise response.error if response.error?
@@ -0,0 +1,66 @@
1
+ # An example of the basic functionality of
2
+ # Momento::SimpleCacheClient.
3
+
4
+ require 'momento'
5
+
6
+ # Get your Momento token from an environment variable.
7
+ TOKEN = ENV.fetch('MOMENTO_AUTH_TOKEN')
8
+
9
+ # Cached items will be deleted after 12.5 seconds.
10
+ TTL_SECONDS = 12.5
11
+
12
+ # The name of the cache to create *and delete*
13
+ CACHE_NAME = ENV.fetch('MOMENTO_CACHE_NAME')
14
+
15
+ # Instantiate a Momento client.
16
+ client = Momento::SimpleCacheClient.new(
17
+ auth_token: TOKEN,
18
+ default_ttl: TTL_SECONDS
19
+ )
20
+
21
+ # Create a cache to play with.
22
+ response = client.create_cache(CACHE_NAME)
23
+ if response.success?
24
+ puts "Created the cache."
25
+ elsif response.already_exists?
26
+ puts "Cache already exists."
27
+ elsif response.error?
28
+ raise "Couldn't create a cache: #{response.error}"
29
+ end
30
+
31
+ # List our caches.
32
+ puts "Caches: #{client.caches.to_a.join(", ")}"
33
+
34
+ # Put an item in the cache.
35
+ response = client.set(CACHE_NAME, "key", "You cached something!")
36
+ if response.success?
37
+ puts "Set an item in the cache."
38
+ elsif response.error?
39
+ raise "Couldn't set an item in the cache: #{response.error}"
40
+ end
41
+
42
+ # And get it back.
43
+ response = client.get(CACHE_NAME, "key")
44
+ if response.hit?
45
+ puts "Cache returned: #{response.value_string}"
46
+ elsif response.miss?
47
+ puts "The item wasn't found in the cache."
48
+ elsif response.error?
49
+ raise "Couldn't get an item from the cache: #{response.error}"
50
+ end
51
+
52
+ # Now delete it.
53
+ response = client.delete(CACHE_NAME, "key")
54
+ if response.success?
55
+ puts "Key/value deleted."
56
+ elsif response.error?
57
+ raise "Couldn't delete an item from the cache: #{response.error}"
58
+ end
59
+
60
+ # And delete our test cache.
61
+ response = client.delete_cache(CACHE_NAME)
62
+ if response.success?
63
+ puts "Deleted the cache."
64
+ elsif response.error?
65
+ raise "Couldn't create a cache: #{response.error}"
66
+ end
data/examples/file.rb ADDED
@@ -0,0 +1,57 @@
1
+ # This is an example of storing binary data.
2
+ #
3
+ # It will cache a test image, read it back,
4
+ # and write it to disk as test_copy.jpg.
5
+
6
+ require 'momento'
7
+
8
+ # Get your Momento token from an environment variable.
9
+ TOKEN = ENV.fetch('MOMENTO_AUTH_TOKEN')
10
+
11
+ # Cached items will be deleted after 12.5 seconds.
12
+ TTL_SECONDS = 12.5
13
+
14
+ # The name of the cache to create *and delete*
15
+ CACHE_NAME = ENV.fetch('MOMENTO_CACHE_NAME')
16
+
17
+ # So it can be run from the top of the repo
18
+ # or from the examples directory.
19
+ FILE_LOCATIONS = [
20
+ "spec/support/assets/test.jpg",
21
+ "../spec/support/assets/test.jpg"
22
+ ].freeze
23
+
24
+ # Instantiate a Momento client.
25
+ client = Momento::SimpleCacheClient.new(
26
+ auth_token: TOKEN,
27
+ default_ttl: TTL_SECONDS
28
+ )
29
+
30
+ # Create a cache for testing, or use an already existing one.
31
+ response = client.create_cache(CACHE_NAME)
32
+ raise response.error if response.error?
33
+
34
+ # Read an image file.
35
+ file = FILE_LOCATIONS.find { |f| File.exist?(f) }
36
+ contents = File.read(file)
37
+
38
+ # Add the image to the cache.
39
+ puts "Caching #{file}"
40
+ client.set(CACHE_NAME, "test.jpg", contents)
41
+ raise response.error if response.error?
42
+
43
+ puts "Retrieving the image"
44
+ response = client.get(CACHE_NAME, "test.jpg")
45
+ raise "Cache image was not found!" if response.miss?
46
+ raise response.error if response.error?
47
+
48
+ puts "Writing the image as test_copy.jpg"
49
+ f = File.open("test_copy.jpg", "wb")
50
+ f.write(response.value_bytes)
51
+ f.close
52
+
53
+ # Delete our test cache.
54
+ response = client.delete_cache(CACHE_NAME)
55
+ raise response.error if response.error?
56
+
57
+ puts "Now open test_copy.jpg"
@@ -1,3 +1,5 @@
1
+ # @private
2
+
1
3
  # Generated by the protocol buffer compiler. DO NOT EDIT!
2
4
  # source: cacheclient.proto
3
5
 
@@ -1,3 +1,5 @@
1
+ # @private
2
+
1
3
  # Generated by the protocol buffer compiler. DO NOT EDIT!
2
4
  # Source: cacheclient.proto for package 'cache_client'
3
5
 
@@ -1,3 +1,5 @@
1
+ # @private
2
+
1
3
  # Generated by the protocol buffer compiler. DO NOT EDIT!
2
4
  # source: controlclient.proto
3
5
 
@@ -1,3 +1,5 @@
1
+ # @private
2
+
1
3
  # Generated by the protocol buffer compiler. DO NOT EDIT!
2
4
  # Source: controlclient.proto for package 'control_client'
3
5