Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add delete method. #56

Merged
merged 2 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Momento/MomentoSdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PackageReference Include="Google.Protobuf" Version="3.19.0" />
<PackageReference Include="Grpc.Net.Client" Version="2.40.0" />
<PackageReference Include="Grpc.Core" Version="2.41.1" />
<PackageReference Include="Momento" Version="0.8.0" />
<PackageReference Include="Momento" Version="0.18.0" />
<PackageReference Include="JWT" Version="8.4.2" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.16.0" />
</ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions Momento/Responses/CacheDeleteResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
namespace MomentoSdk.Responses
{
public class CacheDeleteResponse
{
public CacheDeleteResponse()
{
}
}
}
52 changes: 52 additions & 0 deletions Momento/ScsDataClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public async Task<CacheGetResponse> GetAsync(string cacheName, byte[] key)
return new CacheGetResponse(resp);
}

public async Task<CacheDeleteResponse> DeleteAsync(string cacheName, byte[] key)
{
await this.SendDeleteAsync(cacheName, Convert(key));
return new CacheDeleteResponse();
}

public async Task<CacheSetResponse> SetAsync(string cacheName, string key, string value, uint ttlSeconds)
{
_SetResponse response = await this.SendSetAsync(cacheName, key: Convert(key), value: Convert(value), ttlSeconds: ttlSeconds, dataClientOperationTimeoutMilliseconds: this.dataClientOperationTimeoutMilliseconds);
Expand All @@ -64,6 +70,12 @@ public async Task<CacheGetResponse> GetAsync(string cacheName, string key)
return new CacheGetResponse(resp);
}

public async Task<CacheDeleteResponse> DeleteAsync(string cacheName, string key)
{
await this.SendDeleteAsync(cacheName, Convert(key));
return new CacheDeleteResponse();
}

public async Task<CacheSetResponse> SetAsync(string cacheName, string key, byte[] value, uint ttlSeconds)
{
_SetResponse response = await this.SendSetAsync(cacheName, value: Convert(value), key: Convert(key), ttlSeconds: ttlSeconds, dataClientOperationTimeoutMilliseconds: this.dataClientOperationTimeoutMilliseconds);
Expand Down Expand Up @@ -137,6 +149,12 @@ public CacheGetResponse Get(string cacheName, byte[] key)
return new CacheGetResponse(resp);
}

public CacheDeleteResponse Delete(string cacheName, byte[] key)
{
this.SendDelete(cacheName, Convert(key));
return new CacheDeleteResponse();
}

public CacheSetResponse Set(string cacheName, string key, string value, uint ttlSeconds)
{
_SetResponse response = this.SendSet(cacheName, key: Convert(key), value: Convert(value), ttlSeconds: ttlSeconds, dataClientOperationTimeoutMilliseconds: this.dataClientOperationTimeoutMilliseconds);
Expand All @@ -154,6 +172,12 @@ public CacheGetResponse Get(string cacheName, string key)
return new CacheGetResponse(resp);
}

public CacheDeleteResponse Delete(string cacheName, string key)
{
this.SendDelete(cacheName, Convert(key));
return new CacheDeleteResponse();
}

public CacheSetResponse Set(string cacheName, string key, byte[] value, uint ttlSeconds)
{
_SetResponse response = this.SendSet(cacheName, key: Convert(key), value: Convert(value), ttlSeconds: ttlSeconds, dataClientOperationTimeoutMilliseconds: this.dataClientOperationTimeoutMilliseconds);
Expand Down Expand Up @@ -221,6 +245,34 @@ private _SetResponse SendSet(string cacheName, ByteString key, ByteString value,
}
}

private _DeleteResponse SendDelete(string cacheName, ByteString key)
{
_DeleteRequest request = new _DeleteRequest() { CacheKey = key };
DateTime deadline = DateTime.UtcNow.AddMilliseconds(dataClientOperationTimeoutMilliseconds);
try
{
return this.grpcManager.Client().Delete(request, new Metadata { { "cache", cacheName } }, deadline: deadline);
}
catch (Exception e)
{
throw CacheExceptionMapper.Convert(e);
}
}

private async Task<_DeleteResponse> SendDeleteAsync(string cacheName, ByteString key)
{
_DeleteRequest request = new _DeleteRequest() { CacheKey = key };
DateTime deadline = DateTime.UtcNow.AddMilliseconds(dataClientOperationTimeoutMilliseconds);
try
{
return await this.grpcManager.Client().DeleteAsync(request, new Metadata { { "cache", cacheName } }, deadline: deadline);
}
catch (Exception e)
{
throw CacheExceptionMapper.Convert(e);
}
}

private async Task<CacheMultiGetResponse> SendMultiGetAsync(string cacheName, ByteString key)
{
_GetRequest request = new _GetRequest() { CacheKey = key };
Expand Down
46 changes: 46 additions & 0 deletions Momento/SimpleCacheClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ public async Task<CacheGetResponse> GetAsync(string cacheName, byte[] key)
return await this.dataClient.GetAsync(cacheName, key);
}

/// <summary>
/// Remove the key from the cache.
/// </summary>
/// <param name="cacheName">Name of the cache to delete the key from.</param>
/// <param name="key">The key to perform a cache lookup on.</param>
/// <returns>Future containing the result of the delete operation.</returns>
public async Task<CacheDeleteResponse> DeleteAsync(string cacheName, byte[] key)
{
return await this.dataClient.DeleteAsync(cacheName, key);
}

/// <summary>
/// Sets the value in cache with a given Time To Live (TTL) seconds.
/// </summary>
Expand Down Expand Up @@ -133,6 +144,18 @@ public async Task<CacheGetResponse> GetAsync(string cacheName, string key)
{
return await this.dataClient.GetAsync(cacheName, key);
}

/// <summary>
/// Remove the key from the cache.
/// </summary>
/// <param name="cacheName">Name of the cache to delete the key from.</param>
/// <param name="key">The key to perform a cache lookup on.</param>
/// <returns>Future containing the result of the delete operation.</returns>
public async Task<CacheDeleteResponse> DeleteAsync(string cacheName, string key)
{
return await this.dataClient.DeleteAsync(cacheName, key);
}

/// <summary>
/// Sets the value in cache with a given Time To Live (TTL) seconds.
/// </summary>
Expand Down Expand Up @@ -220,6 +243,17 @@ public CacheGetResponse Get(string cacheName, byte[] key)
return this.dataClient.Get(cacheName, key);
}

/// <summary>
/// Remove the key from the cache.
/// </summary>
/// <param name="cacheName">Name of the cache to delete the key from.</param>
/// <param name="key">The key to perform a cache lookup on.</param>
/// <returns>Future containing the result of the delete operation.</returns>
public CacheDeleteResponse Delete(string cacheName, byte[] key)
{
return this.dataClient.Delete(cacheName, key);
}

/// <summary>
/// Sets the value in cache with a given Time To Live (TTL) seconds. If a value for this key is already present it will be replaced by the new value.
/// </summary>
Expand Down Expand Up @@ -254,6 +288,18 @@ public CacheGetResponse Get(string cacheName, string key)
{
return this.dataClient.Get(cacheName, key);
}

/// <summary>
/// Remove the key from the cache.
/// </summary>
/// <param name="cacheName">Name of the cache to delete the key from.</param>
/// <param name="key">The key to perform a cache lookup on.</param>
/// <returns>Future containing the result of the delete operation.</returns>
public CacheDeleteResponse Delete(string cacheName, string key)
{
return this.dataClient.Delete(cacheName, key);
}

/// <summary>
/// Sets the value in cache with a given Time To Live (TTL) seconds. If a value for this key is already present it will be replaced by the new value.
/// </summary>
Expand Down
72 changes: 72 additions & 0 deletions MomentoIntegrationTest/CacheTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,77 @@ public void SetThrowsNotFoundExceptionNonExistentCache()
SimpleCacheClient client = new SimpleCacheClient(authKey, defaultTtlSeconds);
Assert.Throws<NotFoundException>(() => client.Set("non-existent-cache", Guid.NewGuid().ToString(), Guid.NewGuid().ToString()));
}

[Fact]
public void Delete_KeyIsByteArray_HappyPath()
{
// Set a key to then delete
byte[] key = new byte[] { 0x01, 0x02, 0x03, 0x04 };
byte[] value = new byte[] { 0x05, 0x06, 0x07, 0x08 };
client.Set(cacheName, key, value, ttlSeconds: 60);
CacheGetResponse getResponse = client.Get(cacheName, key);
Assert.Equal(CacheGetStatus.HIT, getResponse.Status);

// Delete
client.Delete(cacheName, key);

// Check deleted
getResponse = client.Get(cacheName, key);
Assert.Equal(CacheGetStatus.MISS, getResponse.Status);
}

[Fact]
public async Task DeleteAsync_KeyIsByteArray_HappyPath()
{
// Set a key to then delete
byte[] key = new byte[] { 0x01, 0x02, 0x03, 0x04 };
byte[] value = new byte[] { 0x05, 0x06, 0x07, 0x08 };
await client.SetAsync(cacheName, key, value, ttlSeconds: 60);
CacheGetResponse getResponse = await client.GetAsync(cacheName, key);
Assert.Equal(CacheGetStatus.HIT, getResponse.Status);

// Delete
await client.DeleteAsync(cacheName, key);

// Check deleted
getResponse = await client.GetAsync(cacheName, key);
Assert.Equal(CacheGetStatus.MISS, getResponse.Status);
}

[Fact]
public void Delete_KeyIsString_HappyPath()
{
// Set a key to then delete
string key = "key";
string value = "value";
client.Set(cacheName, key, value, ttlSeconds: 60);
CacheGetResponse getResponse = client.Get(cacheName, key);
Assert.Equal(CacheGetStatus.HIT, getResponse.Status);

// Delete
client.Delete(cacheName, key);

// Check deleted
getResponse = client.Get(cacheName, key);
Assert.Equal(CacheGetStatus.MISS, getResponse.Status);
}

[Fact]
public async Task DeleteAsync_KeyIsString_HappyPath()
{
// Set a key to then delete
string key = "key";
string value = "value";
await client.SetAsync(cacheName, key, value, ttlSeconds: 60);
CacheGetResponse getResponse = await client.GetAsync(cacheName, key);
Assert.Equal(CacheGetStatus.HIT, getResponse.Status);

// Delete
await client.DeleteAsync(cacheName, key);

// Check deleted
getResponse = await client.GetAsync(cacheName, key);
Assert.Equal(CacheGetStatus.MISS, getResponse.Status);
}
}
}