Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
2 parents 074536e + e00c218 commit 1dfab11
Show file tree
Hide file tree
Showing 69 changed files with 867 additions and 368 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ jobs:
working-directory: ./examples/MomentoApplication
run: dotnet run

- name: Run MomentoWeb example
id: validation-momento-web
continue-on-error: true
working-directory: ./examples/MomentoWeb
run: dotnet run

- name: Run doc API snippets
id: validation-docexampleapis
continue-on-error: true
Expand All @@ -78,7 +84,7 @@ jobs:
- name: Test example failure
id: test-example-failure
run: |
if [[ "${{ steps.validation-momentoapplication.outcome }}" == "failure" || "${{ steps.validation-docexampleapis.outcome }}" == "failure" ]]
if [[ "${{ steps.validation-momentoapplication.outcome }}" == "failure" || "${{ steps.validation-momento-web.outcome }}" == "failure" || "${{ steps.validation-docexampleapis.outcome }}" == "failure" ]]
then
echo "failure=true" >> $GITHUB_OUTPUT
else
Expand Down
2 changes: 1 addition & 1 deletion examples/DictionaryExample/DictionaryExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Momento.Sdk" Version="1.30.1" />
<PackageReference Include="Momento.Sdk" Version="1.36.1" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion examples/DisposableTokens/DisposableTokens.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Momento.Sdk" Version="1.30.1" />
<PackageReference Include="Momento.Sdk" Version="1.36.1" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion examples/DocExampleApis/DocExampleApis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Momento.Sdk" Version="1.30.1" />
<PackageReference Include="Momento.Sdk" Version="1.36.1" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion examples/MomentoApplication/MomentoApplication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Momento.Sdk" Version="1.30.1" />
<PackageReference Include="Momento.Sdk" Version="1.36.1" />
</ItemGroup>

<ItemGroup>
Expand Down
30 changes: 11 additions & 19 deletions examples/MomentoApplication/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Momento.Sdk;
using Momento.Sdk;
using Momento.Sdk.Auth;
using Momento.Sdk.Config;
using Momento.Sdk.Exceptions;
Expand All @@ -17,7 +14,7 @@
await AdvancedExamples.ListCachesExample(client);
await AdvancedExamples.SetGetDeleteExample(client);
await AdvancedExamples.DeleteCacheExample(client);
AdvancedExamples.EagerConnectionExample();
await AdvancedExamples.EagerConnectionExample();


Console.WriteLine("\nProgram has completed successfully.");
Expand Down Expand Up @@ -103,7 +100,7 @@ public static async Task SetGetDeleteExample(ICacheClient client) {
var deleteKeyResponse = await client.DeleteAsync(CACHE_NAME, KEY);
if (deleteKeyResponse is CacheDeleteResponse.Error deleteKeyError)
{
// Also not considred fatal.
// Also not considered fatal.
Console.WriteLine($"Error deleting key: {deleteKeyError.Message}!");
}
}
Expand All @@ -115,7 +112,7 @@ public static async Task DeleteCacheExample(ICacheClient client) {
if (deleteCacheResponse is DeleteCacheResponse.Error deleteCacheError)
{
// Report fatal error and exit
Console.WriteLine("Error deleting cache: {deleteCacheError.Message}. Exiting.");
Console.WriteLine($"Error deleting cache: {deleteCacheError.Message}. Exiting.");
Environment.Exit(1);
}
}
Expand All @@ -126,22 +123,17 @@ public static async Task DeleteCacheExample(ICacheClient client) {
/// when it is instantiated, and to specify a distinct timeout for the
/// connection to be established.
/// </summary>
public static void EagerConnectionExample()
public static async Task EagerConnectionExample()
{
ICredentialProvider authProvider = new EnvMomentoTokenProvider("MOMENTO_API_KEY");
TimeSpan defaultTtl = TimeSpan.FromSeconds(60);
var authProvider = new EnvMomentoTokenProvider("MOMENTO_API_KEY");
var defaultTtl = TimeSpan.FromSeconds(60);
var config = Configurations.Laptop.V1();
var eagerConnectionConfig = config.WithTransportStrategy(config.TransportStrategy.WithEagerConnectionTimeout(TimeSpan.FromSeconds(10)));
var eagerConnectionTimeout = TimeSpan.FromSeconds(10);

Console.WriteLine("Creating a momento client with eager connection");
using (CacheClient client = new CacheClient(eagerConnectionConfig, authProvider, defaultTtl)) {
using (var client = await CacheClient.CreateAsync(config, authProvider, defaultTtl, eagerConnectionTimeout))
{
Console.WriteLine("Successfully created a momento client with eager connection");
}

var grpcConfig = config.TransportStrategy.GrpcConfig.WithMinNumGrpcChannels(4);
var transportStrategy = config.TransportStrategy
.WithEagerConnectionTimeout(TimeSpan.FromSeconds(20))
.WithGrpcConfig(grpcConfig);
config = config.WithTransportStrategy(transportStrategy);

}
}
6 changes: 3 additions & 3 deletions examples/MomentoApplication/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ MOMENTO_API_KEY=<YOUR API KEY> dotnet run

## Error Handling

Errors that occur in calls to SimpleCacheClient methods are surfaced to developers as part of the return values of
Errors that occur in calls to CacheClient methods are surfaced to developers as part of the return values of
the calls, as opposed to by throwing exceptions. This makes them more visible, and allows your IDE to be more
helpful in ensuring that you've handled the ones you care about. (For more on our philosophy about this, see our
blog post on why [Exceptions are bugs](https://www.gomomento.com/blog/exceptions-are-bugs). And send us any
feedback you have!)

The preferred way of interpreting the return values from SimpleCacheClient methods is using [Pattern matching](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching). Here's a quick example:
The preferred way of interpreting the return values from CacheClient methods is using [Pattern matching](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching). Here's a quick example:

```csharp
CacheGetResponse getResponse = await client.GetAsync(CACHE_NAME, KEY);
Expand Down Expand Up @@ -68,5 +68,5 @@ if (getResponse is CacheGetResponse.Error errorResponse)
}
```

Note that, outside of SimpleCacheClient responses, exceptions can occur and should be handled as usual. For example, trying to instantiate a SimpleCacheClient with an invalid authentication token will result in an
Note that, outside of CacheClient responses, exceptions can occur and should be handled as usual. For example, trying to instantiate a CacheClient with an invalid authentication token will result in an
IllegalArgumentException being thrown.
6 changes: 6 additions & 0 deletions examples/MomentoExamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DisposableTokens", "Disposa
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TopicExample", "TopicExample\TopicExample.csproj", "{24E1BEE9-8D9D-44B8-B9D5-EBBF08534934}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MomentoWeb", "MomentoWeb\MomentoWeb.csproj", "{DE3FDC58-9342-4B93-A4DB-C4039A235BA3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -67,6 +69,10 @@ Global
{24E1BEE9-8D9D-44B8-B9D5-EBBF08534934}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24E1BEE9-8D9D-44B8-B9D5-EBBF08534934}.Release|Any CPU.ActiveCfg = Release|Any CPU
{24E1BEE9-8D9D-44B8-B9D5-EBBF08534934}.Release|Any CPU.Build.0 = Release|Any CPU
{DE3FDC58-9342-4B93-A4DB-C4039A235BA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE3FDC58-9342-4B93-A4DB-C4039A235BA3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE3FDC58-9342-4B93-A4DB-C4039A235BA3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE3FDC58-9342-4B93-A4DB-C4039A235BA3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Momento.Sdk" Version="1.30.1" />
<PackageReference Include="Momento.Sdk" Version="1.36.1" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion examples/MomentoLoadGen/MomentoLoadGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Momento.Sdk" Version="1.30.1" />
<PackageReference Include="Momento.Sdk" Version="1.36.1" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion examples/MomentoUsage/MomentoUsage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Momento.Sdk" Version="1.30.1" />
<PackageReference Include="Momento.Sdk" Version="1.36.1" />
</ItemGroup>
</Project>
6 changes: 3 additions & 3 deletions examples/MomentoUsage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ MOMENTO_API_KEY=<YOUR API KEY> dotnet run

## Error Handling

Errors that occur in calls to SimpleCacheClient methods are surfaced to developers as part of the return values of
Errors that occur in calls to CacheClient methods are surfaced to developers as part of the return values of
the calls, as opposed to by throwing exceptions. This makes them more visible, and allows your IDE to be more
helpful in ensuring that you've handled the ones you care about. (For more on our philosophy about this, see our
blog post on why [Exceptions are bugs](https://www.gomomento.com/blog/exceptions-are-bugs). And send us any
feedback you have!)

The preferred way of interpreting the return values from SimpleCacheClient methods is using [Pattern matching](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching). Here's a quick example:
The preferred way of interpreting the return values from CacheClient methods is using [Pattern matching](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching). Here's a quick example:

```csharp
CacheGetResponse getResponse = await client.GetAsync(CACHE_NAME, KEY);
Expand Down Expand Up @@ -64,5 +64,5 @@ if (getResponse is CacheGetResponse.Error errorResponse)
}
```

Note that, outside of SimpleCacheClient responses, exceptions can occur and should be handled as usual. For example, trying to instantiate a SimpleCacheClient with an invalid authentication token will result in an
Note that, outside of CacheClient responses, exceptions can occur and should be handled as usual. For example, trying to instantiate a CacheClient with an invalid authentication token will result in an
IllegalArgumentException being thrown.
19 changes: 19 additions & 0 deletions examples/MomentoWeb/MomentoWeb.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Momento.Sdk.Web" Version="1.36.1" />
</ItemGroup>

<ItemGroup>
<None Remove="Microsoft.Extensions.Logging.Console" />
</ItemGroup>

</Project>
138 changes: 138 additions & 0 deletions examples/MomentoWeb/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using Momento.Sdk;
using Momento.Sdk.Auth;
using Momento.Sdk.Config;
using Momento.Sdk.Exceptions;
using Momento.Sdk.Responses;

var authProvider = new EnvMomentoTokenProvider("MOMENTO_API_KEY");

var defaultTtl = TimeSpan.FromSeconds(60);

using (ICacheClient client = new CacheClient(Configurations.Laptop.V1(), authProvider, defaultTtl))
{
await AdvancedExamples.CreateCacheExample(client);
await AdvancedExamples.ListCachesExample(client);
await AdvancedExamples.SetGetDeleteExample(client);
await AdvancedExamples.DeleteCacheExample(client);
await AdvancedExamples.EagerConnectionExample();

Console.WriteLine("\nProgram has completed successfully.");
}


public class AdvancedExamples {
private const string CacheName = "momento-example";
private const string Key = "MyKey";
private const string Value = "MyData";

public static async Task CreateCacheExample(ICacheClient client)
{

Console.WriteLine($"Creating cache {CacheName}");
var createCacheResponse = await client.CreateCacheAsync(CacheName);
// Check the create response for an error and handle as appropriate.
if (createCacheResponse is CreateCacheResponse.Error createCacheError)
{
if (createCacheError.ErrorCode == MomentoErrorCode.LIMIT_EXCEEDED_ERROR)
{
Console.WriteLine("Error: cache limit exceeded. We need to talk to [email protected]! Exiting.");
}
else
{
Console.WriteLine($"Error creating cache: {createCacheError.Message}. Exiting.");
}
// Any error is considered fatal.
Environment.Exit(1);
}
// If there's already a cache by this name, alert the user.
if (createCacheResponse is CreateCacheResponse.CacheAlreadyExists)
{
Console.WriteLine($"A cache with the name {CacheName} already exists");
}
}

public static async Task ListCachesExample(ICacheClient client) {
Console.WriteLine("\nListing caches:");
ListCachesResponse listCachesResponse = await client.ListCachesAsync();
if (listCachesResponse is ListCachesResponse.Success listCachesSuccess)
{
foreach (CacheInfo cacheInfo in listCachesSuccess.Caches)
{
Console.WriteLine($"- {cacheInfo.Name}");
}
}
else if (listCachesResponse is ListCachesResponse.Error listCachesError)
{
// We do not consider this a fatal error, so we just report it.
Console.WriteLine($"Error listing caches: {listCachesError.Message}");
}
}

public static async Task SetGetDeleteExample(ICacheClient client) {
Console.WriteLine($"\nSetting key: {Key} with value: {Value}");
var setResponse = await client.SetAsync(CacheName, Key, Value);
if (setResponse is CacheSetResponse.Error setError)
{
// Warn the user of the error and exit.
Console.WriteLine($"Error setting value: {setError.Message}. Exiting.");
Environment.Exit(1);
}

Console.WriteLine($"\nGetting value for key: {Key}");
CacheGetResponse getResponse = await client.GetAsync(CacheName, Key);
if (getResponse is CacheGetResponse.Hit getHit)
{
Console.WriteLine($"Looked up value: {getHit.ValueString}, Stored value: {Value}");
}
else if (getResponse is CacheGetResponse.Miss)
{
// This shouldn't be fatal but should be reported.
Console.WriteLine($"Error: got a cache miss for {Key}!");
}
else if (getResponse is CacheGetResponse.Error getError)
{
// Also not considered fatal.
Console.WriteLine($"Error getting value: {getError.Message}!");
}

Console.WriteLine($"\nDeleting key {Key}");
var deleteKeyResponse = await client.DeleteAsync(CacheName, Key);
if (deleteKeyResponse is CacheDeleteResponse.Error deleteKeyError)
{
// Also not considered fatal.
Console.WriteLine($"Error deleting key: {deleteKeyError.Message}!");
}
}

public static async Task DeleteCacheExample(ICacheClient client) {

Console.WriteLine($"\nDeleting cache {CacheName}");
var deleteCacheResponse = await client.DeleteCacheAsync(CacheName);
if (deleteCacheResponse is DeleteCacheResponse.Error deleteCacheError)
{
// Report fatal error and exit
Console.WriteLine($"Error deleting cache: {deleteCacheError.Message}. Exiting.");
Environment.Exit(1);
}
}

/// <summary>
/// By default, cache clients will connect lazily when the first request is
/// issued. This example shows how to configure a client to connect eagerly
/// when it is instantiated, and to specify a distinct timeout for the
/// connection to be established.
/// </summary>
public static async Task EagerConnectionExample()
{
var authProvider = new EnvMomentoTokenProvider("MOMENTO_API_KEY");
var defaultTtl = TimeSpan.FromSeconds(60);
var config = Configurations.Laptop.V1();
var eagerConnectionTimeout = TimeSpan.FromSeconds(10);

Console.WriteLine("Creating a momento client with eager connection");
using (var client = await CacheClient.CreateAsync(config, authProvider, defaultTtl, eagerConnectionTimeout))
{
Console.WriteLine("Successfully created a momento client with eager connection");
}
}
}
Loading

0 comments on commit 1dfab11

Please sign in to comment.