-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdoc_examples.exs
More file actions
469 lines (384 loc) · 16.1 KB
/
doc_examples.exs
File metadata and controls
469 lines (384 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
defmodule Examples.DocExamples do
@fake_api_key_v1 "eyJhcGlfa2V5IjogImV5SjBlWEFpT2lKS1YxUWlMQ0poYkdjaU9pSklVekkxTmlKOS5leUpwYzNNaU9pS" <>
"lBibXhwYm1VZ1NsZFVJRUoxYVd4a1pYSWlMQ0pwWVhRaU9qRTJOemd6TURVNE1USXNJbVY0Y0NJNk5E" <>
"ZzJOVFV4TlRReE1pd2lZWFZrSWpvaUlpd2ljM1ZpSWpvaWFuSnZZMnRsZEVCbGVHRnRjR3hsTG1OdmJ" <>
"TSjkuOEl5OHE4NExzci1EM1lDb19IUDRkLXhqSGRUOFVDSXV2QVljeGhGTXl6OCIsICJlbmRwb2ludC" <>
"I6ICJ0ZXN0Lm1vbWVudG9ocS5jb20ifQo="
@fake_api_key_v2 "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9" <>
".eyJ0IjoiZyIsImp0aSI6InNvbWUtaWQifQ" <>
".GMr9nA6HE0ttB6llXct_2Sg5-fOKGFbJCdACZFgNbN1fhT6OPg_hVc8ThGzBrWC_RlsBpLA1nzqK3SOJDXYxAw"
@spec retrieve_api_key_from_your_secrets_manager() :: String.t()
def retrieve_api_key_from_your_secrets_manager() do
@fake_api_key_v1
end
@spec retrieve_api_key_v2_from_your_secrets_manager() :: String.t()
def retrieve_api_key_v2_from_your_secrets_manager() do
@fake_api_key_v2
end
@spec example_API_CredentialProviderFromEnvVar() :: Momento.Auth.CredentialProvider.t()
def example_API_CredentialProviderFromEnvVar() do
Momento.Auth.CredentialProvider.from_env_var!("V1_API_KEY")
end
@spec example_API_CredentialProviderFromEnvVarV2() :: Momento.Auth.CredentialProvider.t()
def example_API_CredentialProviderFromEnvVarV2() do
Momento.Auth.CredentialProvider.from_env_var_v2!("MOMENTO_API_KEY", "MOMENTO_ENDPOINT")
end
@spec example_API_CredentialProviderFromEnvVarV2Default() :: Momento.Auth.CredentialProvider.t()
def example_API_CredentialProviderFromEnvVarV2Default() do
Momento.Auth.CredentialProvider.from_env_var_v2!()
end
@spec example_API_CredentialProviderFromString() :: Momento.Auth.CredentialProvider.t()
def example_API_CredentialProviderFromString() do
api_key = retrieve_api_key_from_your_secrets_manager()
Momento.Auth.CredentialProvider.from_string!(api_key)
end
@spec example_API_CredentialProviderFromApiKeyV2() :: Momento.Auth.CredentialProvider.t()
def example_API_CredentialProviderFromApiKeyV2() do
api_key = retrieve_api_key_v2_from_your_secrets_manager()
endpoint = "cell-4-us-west-2-1.prod.a.momentohq.com"
Momento.Auth.CredentialProvider.from_api_key_v2!(api_key, endpoint)
end
@spec example_API_CredentialProviderFromDisposableToken() :: Momento.Auth.CredentialProvider.t()
def example_API_CredentialProviderFromDisposableToken() do
api_key = retrieve_api_key_from_your_secrets_manager()
Momento.Auth.CredentialProvider.from_disposable_token!(api_key)
end
@spec example_API_InstantiateCacheClient() :: Momento.CacheClient.t()
def example_API_InstantiateCacheClient() do
config = Momento.Configurations.Laptop.latest()
credential_provider = Momento.Auth.CredentialProvider.from_env_var_v2!()
default_ttl_seconds = 60.0
Momento.CacheClient.create!(config, credential_provider, default_ttl_seconds)
end
@spec example_API_ErrorHandlingHitMiss(client :: Momento.CacheClient.t()) :: any()
def example_API_ErrorHandlingHitMiss(client) do
case Momento.CacheClient.get(client, "test-cache", "test-key") do
{:ok, hit} ->
IO.puts("Retrieved value for key 'test-key': #{hit.value}")
:miss ->
IO.puts("Key 'test-key' was not found in cache 'test-cache'")
{:error, error} ->
IO.puts(
"An error occurred while attempting to get key 'test-key' from cache 'test-cache': #{error.error_code}"
)
raise error
end
end
@spec example_API_ErrorHandlingSuccess(client :: Momento.CacheClient.t()) :: any()
def example_API_ErrorHandlingSuccess(client) do
case Momento.CacheClient.set(client, "test-cache", "test-key", "test-value") do
{:ok, _} ->
IO.puts("Key 'test-key' stored successfully")
{:error, error} ->
IO.puts(
"An error occurred while attempting to store key 'test-key' in cache 'test-cache': #{error.error_code}"
)
raise error
end
end
@spec example_API_CreateCache(client :: Momento.CacheClient.t()) :: any()
def example_API_CreateCache(client) do
case Momento.CacheClient.create_cache(client, "test-cache") do
{:ok, _} ->
IO.puts("Cache 'test-cache' created")
:already_exists ->
:ok
{:error, error} ->
IO.puts(
"An error occurred while attempting to create cache 'test-cache': #{error.error_code}"
)
raise error
end
end
@spec example_API_DeleteCache(client :: Momento.CacheClient.t()) :: any()
def example_API_DeleteCache(client) do
case Momento.CacheClient.delete_cache(client, "test-cache") do
{:ok, _} ->
IO.puts("Cache 'test-cache' deleted")
{:error, error} ->
IO.puts(
"An error occurred while attempting to delete cache 'test-cache': #{error.error_code}"
)
raise error
end
end
@spec example_API_ListCaches(client :: Momento.CacheClient.t()) :: any()
def example_API_ListCaches(client) do
case Momento.CacheClient.list_caches(client) do
{:ok, result} ->
IO.puts("Caches:")
IO.inspect(result.caches)
{:error, error} ->
IO.puts("An error occurred while attempting to list caches: #{error.error_code}")
raise error
end
end
@spec example_API_Set(client :: Momento.CacheClient.t()) :: any()
def example_API_Set(client) do
case Momento.CacheClient.set(client, "test-cache", "test-key", "test-value") do
{:ok, _} ->
IO.puts("Key 'test-key' stored successfully")
{:error, error} ->
IO.puts(
"An error occurred while attempting to store key 'test-key' in cache 'test-cache': #{error.error_code}"
)
raise error
end
end
@spec example_API_Get(client :: Momento.CacheClient.t()) :: any()
def example_API_Get(client) do
case Momento.CacheClient.get(client, "test-cache", "test-key") do
{:ok, hit} ->
IO.puts("Retrieved value for key 'test-key': #{hit.value}")
:miss ->
IO.puts("Key 'test-key' was not found in cache 'test-cache'")
{:error, error} ->
IO.puts(
"An error occurred while attempting to get key 'test-key' from cache 'test-cache': #{error.error_code}"
)
raise error
end
end
@spec example_API_Delete(client :: Momento.CacheClient.t()) :: any()
def example_API_Delete(client) do
case Momento.CacheClient.delete(client, "test-cache", "test-key") do
{:ok, _} ->
IO.puts("Key 'test-key' deleted successfully")
{:error, error} ->
IO.puts(
"An error occurred while attempting to delete key 'test-key' from cache 'test-cache': #{error.error_code}"
)
raise error
end
end
@spec example_API_SortedSetPutElement(client :: Momento.CacheClient.t()) :: any()
def example_API_SortedSetPutElement(client) do
case Momento.CacheClient.sorted_set_put_element(
client,
"test-cache",
"test-sorted-set",
"test-value",
5.0
) do
{:ok, _} ->
IO.puts(
"Value 'test-value' with score '5' added successfully to sorted set 'test-sorted-set'"
)
{:error, error} ->
IO.puts(
"An error occurred while attempting to put an element into sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
end
@spec example_API_SortedSetPutElements(client :: Momento.CacheClient.t()) :: any()
def example_API_SortedSetPutElements(client) do
case Momento.CacheClient.sorted_set_put_elements(client, "test-cache", "test-sorted-set", [
{"key1", 10.0},
{"key2", 20.0}
]) do
{:ok, _} ->
IO.puts("Elements added successfully to sorted set 'test-sorted-set'")
{:error, error} ->
IO.puts(
"An error occurred while attempting to put elements into sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
end
@spec example_API_SortedSetFetchByRank(client :: Momento.CacheClient.t()) :: any()
def example_API_SortedSetFetchByRank(client) do
{:ok, _} =
Momento.CacheClient.sorted_set_put_elements(client, "test-cache", "test-sorted-set", [
{"key1", 10.0},
{"key2", 20.0}
])
case Momento.CacheClient.sorted_set_fetch_by_rank(client, "test-cache", "test-sorted-set") do
{:ok, hit} ->
IO.puts("Values from sorted set 'test-sorted-set' fetched by rank successfully:")
IO.inspect(hit.value)
:miss ->
IO.puts("Sorted Set 'test-sorted-set' was not found in cache 'test-cache'")
{:error, error} ->
IO.puts(
"An error occurred while attempting to fetch by rank on sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
end
@spec example_API_SortedSetFetchByScore(client :: Momento.CacheClient.t()) :: any()
def example_API_SortedSetFetchByScore(client) do
{:ok, _} =
Momento.CacheClient.sorted_set_put_elements(client, "test-cache", "test-sorted-set", [
{"key1", 10.0},
{"key2", 20.0}
])
case Momento.CacheClient.sorted_set_fetch_by_score(client, "test-cache", "test-sorted-set") do
{:ok, hit} ->
IO.puts("Values from sorted set 'test-sorted-set' fetched by score successfully:")
IO.inspect(hit.value)
:miss ->
IO.puts("Sorted Set 'test-sorted-set' was not found in cache 'test-cache'")
{:error, error} ->
IO.puts(
"An error occurred while attempting to fetch by score on sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
end
@spec example_API_SortedSetGetRank(client :: Momento.CacheClient.t()) :: any()
def example_API_SortedSetGetRank(client) do
{:ok, _} =
Momento.CacheClient.sorted_set_put_elements(client, "test-cache", "test-sorted-set", [
{"key1", 10.0},
{"key2", 20.0}
])
case Momento.CacheClient.sorted_set_get_rank(client, "test-cache", "test-sorted-set", "key1") do
{:ok, hit} ->
IO.puts("Element with value 'key1' has rank: #{hit.rank}")
:miss ->
IO.puts(
"Value 'key1' not found in sorted set, or sorted set 'test-sorted-set' was not found in cache 'test-cache'"
)
{:error, error} ->
IO.puts(
"An error occurred while attempting to get the rank of 'key1' in sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
end
@spec example_API_SortedSetGetScore(client :: Momento.CacheClient.t()) :: any()
def example_API_SortedSetGetScore(client) do
{:ok, _} =
Momento.CacheClient.sorted_set_put_elements(client, "test-cache", "test-sorted-set", [
{"key1", 10.0},
{"key2", 20.0}
])
case Momento.CacheClient.sorted_set_get_score(client, "test-cache", "test-sorted-set", "key1") do
{:ok, hit} ->
IO.puts("Element with value 'key1' has score: #{hit.score}")
:miss ->
IO.puts(
"Value 'key1' not found in sorted set, or sorted set 'test-sorted-set' was not found in cache 'test-cache'"
)
{:error, error} ->
IO.puts(
"An error occurred while attempting to get the score of 'key1' in sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
end
@spec example_API_SortedSetGetScores(client :: Momento.CacheClient.t()) :: any()
def example_API_SortedSetGetScores(client) do
{:ok, _} =
Momento.CacheClient.sorted_set_put_elements(client, "test-cache", "test-sorted-set", [
{"key1", 10.0},
{"key2", 20.0}
])
case Momento.CacheClient.sorted_set_get_scores(client, "test-cache", "test-sorted-set", [
"key1",
"key2"
]) do
{:ok, hit} ->
IO.puts("Element scores retrieved successfully:")
IO.inspect(hit.value)
:miss ->
IO.puts("Sorted Set 'test-sorted-set' was not found in cache 'test-cache'")
{:error, error} ->
IO.puts(
"An error occurred while attempting to get the scores of values in sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
end
@spec example_API_SortedSetIncrementScore(client :: Momento.CacheClient.t()) :: any()
def example_API_SortedSetIncrementScore(client) do
case Momento.CacheClient.sorted_set_increment_score(
client,
"test-cache",
"test-sorted-set",
"key1",
1
) do
{:ok, result} ->
IO.puts("Score for value 'key1' incremented successfully. New score: #{result.score}")
{:error, error} ->
IO.puts(
"An error occurred while attempting to increment the score of 'key1' in sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
end
@spec example_API_SortedSetRemoveElement(client :: Momento.CacheClient.t()) :: any()
def example_API_SortedSetRemoveElement(client) do
{:ok, _} =
Momento.CacheClient.sorted_set_put_element(
client,
"test-cache",
"test-sorted-set",
"key1",
10.0
)
case Momento.CacheClient.sorted_set_remove_element(
client,
"test-cache",
"test-sorted-set",
"key1"
) do
{:ok, _} ->
IO.puts("Element with value 'key1' removed successfully")
{:error, error} ->
IO.puts(
"An error occurred while attempting to remove value 'key1' from sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
end
@spec example_API_SortedSetRemoveElements(client :: Momento.CacheClient.t()) :: any()
def example_API_SortedSetRemoveElements(client) do
{:ok, _} =
Momento.CacheClient.sorted_set_put_elements(client, "test-cache", "test-sorted-set", [
{"key1", 10.0},
{"key2", 20.0}
])
case Momento.CacheClient.sorted_set_remove_elements(client, "test-cache", "test-sorted-set", [
"key1",
"key2"
]) do
{:ok, _} ->
IO.puts("Elements with value 'key1' and 'key2' removed successfully")
{:error, error} ->
IO.puts(
"An error occurred while attempting remove values from sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
end
end
System.put_env("V1_API_KEY", Examples.DocExamples.retrieve_api_key_from_your_secrets_manager())
Examples.DocExamples.example_API_CredentialProviderFromEnvVar()
Examples.DocExamples.example_API_CredentialProviderFromEnvVarV2()
Examples.DocExamples.example_API_CredentialProviderFromEnvVarV2Default()
Examples.DocExamples.example_API_CredentialProviderFromString()
Examples.DocExamples.example_API_CredentialProviderFromApiKeyV2()
Examples.DocExamples.example_API_CredentialProviderFromDisposableToken()
client = Examples.DocExamples.example_API_InstantiateCacheClient()
Examples.DocExamples.example_API_CreateCache(client)
Examples.DocExamples.example_API_DeleteCache(client)
Examples.DocExamples.example_API_CreateCache(client)
Examples.DocExamples.example_API_ErrorHandlingHitMiss(client)
Examples.DocExamples.example_API_ErrorHandlingSuccess(client)
Examples.DocExamples.example_API_ListCaches(client)
Examples.DocExamples.example_API_Set(client)
Examples.DocExamples.example_API_Get(client)
Examples.DocExamples.example_API_Delete(client)
Examples.DocExamples.example_API_SortedSetPutElement(client)
Examples.DocExamples.example_API_SortedSetPutElements(client)
Examples.DocExamples.example_API_SortedSetFetchByRank(client)
Examples.DocExamples.example_API_SortedSetFetchByScore(client)
Examples.DocExamples.example_API_SortedSetGetRank(client)
Examples.DocExamples.example_API_SortedSetGetScore(client)
Examples.DocExamples.example_API_SortedSetGetScores(client)
Examples.DocExamples.example_API_SortedSetIncrementScore(client)
Examples.DocExamples.example_API_SortedSetRemoveElement(client)
Examples.DocExamples.example_API_SortedSetRemoveElements(client)