Skip to content

Commit

Permalink
chore: Add set with string key and byte value (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
poppoerika authored Apr 1, 2022
1 parent b5f3486 commit 0185b50
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Momento/ScsDataClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ public async Task<CacheGetResponse> GetAsync(string cacheName, string key)
return new CacheGetResponse(resp);
}

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, dataClientOperationTimeoutSeconds: this.dataClientOperationTimeoutSeconds);
return new CacheSetResponse(response);
}

public async Task<CacheSetResponse> SetAsync(string cacheName, string key, byte[] value)
{
return await this.SetAsync(cacheName, key, value, defaultTtlSeconds);
}

public CacheSetResponse Set(string cacheName, byte[] key, byte[] value, uint ttlSeconds)
{
_SetResponse resp = this.SendSet(cacheName, key: Convert(key), value: Convert(value), ttlSeconds: ttlSeconds, dataClientOperationTimeoutSeconds: this.dataClientOperationTimeoutSeconds);
Expand Down Expand Up @@ -97,6 +108,17 @@ public CacheGetResponse Get(string cacheName, string key)
return new CacheGetResponse(resp);
}

public CacheSetResponse Set(string cacheName, string key, byte[] value, uint ttlSeconds)
{
_SetResponse response = this.SendSet(cacheName, key: Convert(key), value: Convert(value), ttlSeconds: ttlSeconds, dataClientOperationTimeoutSeconds: this.dataClientOperationTimeoutSeconds);
return new CacheSetResponse(response);
}

public CacheSetResponse Set(string cacheName, string key, byte[] value)
{
return this.Set(cacheName, key, value, defaultTtlSeconds);
}

private async Task<_SetResponse> SendSetAsync(string cacheName, ByteString key, ByteString value, uint ttlSeconds, uint dataClientOperationTimeoutSeconds)
{
_SetRequest request = new _SetRequest() { CacheBody = value, CacheKey = key, TtlMilliseconds = ttlSeconds * 1000 };
Expand Down
47 changes: 47 additions & 0 deletions Momento/SimpleCacheClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,28 @@ public async Task<CacheGetResponse> GetAsync(string cacheName, string key)
{
return await this.dataClient.GetAsync(cacheName, key);
}
/// <summary>
/// Sets the value in cache with a given Time To Live (TTL) seconds.
/// </summary>
/// <param name="key">The key under which the value is to be added</param>
/// <param name="value">The value to be stored</param>
/// <param name="ttlSeconds">Time to Live for the item in Cache. This ttl takes precedence over the TTL used when initializing a cache client</param>
/// <returns>Future containing the result of the set operation</returns>
public async Task<CacheSetResponse> SetAsync(string cacheName, string key, byte[] value, uint ttlSeconds)
{
return await this.dataClient.SetAsync(cacheName, key, value, ttlSeconds);
}

/// <summary>
/// Sets the value in cache with a default Time To Live (TTL) seconds used initializing a cache client
/// </summary>
/// <param name="key">The value to be stored</param>
/// <param name="value">The value to be stored</param>
/// <returns>Future containing the result of the set operation</returns>
public async Task<CacheSetResponse> SetAsync(string cacheName, string key, byte[] value)
{
return await this.dataClient.SetAsync(cacheName, key, value);
}

/// <summary>
/// Sets the value in the cache. If a value for this key is already present it will be replaced by the new value.
Expand All @@ -140,6 +162,7 @@ public async Task<CacheGetResponse> GetAsync(string cacheName, string key)
/// <param name="value">The value to be stored</param>
/// <param name="ttlSeconds">Time to Live for the item in Cache. This ttl takes precedence over the TTL used when initializing a cache client</param>
/// <returns>Result of the set operation</returns>

public CacheSetResponse Set(string cacheName, byte[] key, byte[] value, uint ttlSeconds)
{
return this.dataClient.Set(cacheName, key, value, ttlSeconds);
Expand Down Expand Up @@ -200,6 +223,30 @@ public CacheGetResponse Get(string cacheName, string key)
{
return this.dataClient.Get(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>
/// <param name="key">The key under which the value is to be added</param>
/// <param name="value">The value to be stored</param>
/// <param name="ttlSeconds">Time to Live for the item in Cache. This ttl takes precedence over the TTL used when initializing a cache client</param>
/// <returns>Result of the set operation</returns>
public CacheSetResponse Set(string cacheName, string key, byte[] value, uint ttlSeconds)
{
return this.dataClient.Set(cacheName, key, value, ttlSeconds);
}

/// <summary>
/// Sets the value in the cache. If a value for this key is already present it will be replaced by the new value.
/// The Time to Live (TTL) seconds defaults to the parameter used when initializing this Cache client
/// </summary>
/// <param name="key">The key under which the value is to be added</param>
/// <param name="value">The value to be stored</param>
/// <param name="ttlSeconds">Time to Live for the item in Cache. This ttl takes precedence over the TTL used when initializing a cache client</param>
/// <returns>Result of the set operation</returns>
public CacheSetResponse Set(string cacheName, string key, byte[] value)
{
return this.dataClient.Set(cacheName, key, value);
}

public void Dispose()
{
Expand Down
12 changes: 12 additions & 0 deletions MomentoIntegrationTest/CacheTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Xunit;
using MomentoSdk.Responses;
using MomentoSdk.Exceptions;
using System.Text;

namespace MomentoIntegrationTest
{
Expand Down Expand Up @@ -39,6 +40,17 @@ public void HappyPath()
Assert.Equal(cacheValue, stringResult);
}

[Fact]
public void HappyPathStringKeyByteValue()
{
string cacheKey = "some cache key";
byte[] cacheValue = Encoding.ASCII.GetBytes("some cache value");
client.Set(cacheName, cacheKey, cacheValue, defaultTtlSeconds);
CacheGetResponse result = client.Get(cacheName, cacheKey);
string stringResult = result.String();
Assert.Equal(Encoding.ASCII.GetString(cacheValue), stringResult);
}

[Fact]
public async void HappyPathExpiredTtl()
{
Expand Down

0 comments on commit 0185b50

Please sign in to comment.