Skip to content

Commit

Permalink
chore: prefer ienumerable over dictionary + other style issues (#91)
Browse files Browse the repository at this point in the history
* Broaden `IDictionary` to `IEnumerable<KeyValuePair>`.

This commit broadens the use of `IDictionary` in `SetMulti` in the
API. `IDictionary` is overly narrow.

While here, we also removed the implementation of
CacheSet*Response. These were problematic (see previous PR).

* DRY data client.
  • Loading branch information
malandis authored Jul 29, 2022
1 parent c040694 commit c9aaec5
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 130 deletions.
8 changes: 4 additions & 4 deletions Momento/ISimpleCacheClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ public interface ISimpleCacheClient : IDisposable
/// <param name="cacheName">Name of the cache to store the items in.</param>
/// <param name="items">The items to set.</param>
/// <returns>Task object representing the result of the set operation.</returns>
public Task<CacheSetMultiResponse> SetMultiAsync(string cacheName, IDictionary<byte[], byte[]> items, uint? ttlSeconds = null);
public Task<CacheSetMultiResponse> SetMultiAsync(string cacheName, IEnumerable<KeyValuePair<byte[], byte[]>> items, uint? ttlSeconds = null);

/// <summary>
/// Sets multiple items in the cache. Overwrites existing items.
/// </summary>
/// <param name="cacheName">Name of the cache to store the items in.</param>
/// <param name="items">The items to set.</param>
/// <returns>Task object representing the result of the set operation.</returns>
public Task<CacheSetMultiResponse> SetMultiAsync(string cacheName, IDictionary<string, string> items, uint? ttlSeconds = null);
public Task<CacheSetMultiResponse> SetMultiAsync(string cacheName, IEnumerable<KeyValuePair<string, string>> items, uint? ttlSeconds = null);

/// <summary>
/// Set the value in the cache. If a value for this key is already present it will be replaced by the new value.
Expand Down Expand Up @@ -218,15 +218,15 @@ public interface ISimpleCacheClient : IDisposable
/// <param name="cacheName">Name of the cache to store the items in.</param>
/// <param name="items">The items to set.</param>
/// <returns>Result of the set operation.</returns>
public CacheSetMultiResponse SetMulti(string cacheName, IDictionary<byte[], byte[]> items, uint? ttlSeconds = null);
public CacheSetMultiResponse SetMulti(string cacheName, IEnumerable<KeyValuePair<byte[], byte[]>> items, uint? ttlSeconds = null);

/// <summary>
/// Sets multiple items in the cache. Overwrites existing items.
/// </summary>
/// <param name="cacheName">Name of the cache to store the items in.</param>
/// <param name="items">The items to set.</param>
/// <returns>Result of the set operation.</returns>
public CacheSetMultiResponse SetMulti(string cacheName, IDictionary<string, string> items, uint? ttlSeconds = null);
public CacheSetMultiResponse SetMulti(string cacheName, IEnumerable<KeyValuePair<string, string>> items, uint? ttlSeconds = null);

/// <summary>
/// Remove the key from the cache.
Expand Down
18 changes: 10 additions & 8 deletions Momento/Incubating/Internal/ScsDataClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ private CacheDictionarySetResponse SendDictionarySet(string cacheName, string di
try
{
this.grpcManager.Client.DictionarySet(request, MetadataWithCache(cacheName), deadline: CalculateDeadline());
return new CacheDictionarySetResponse();
}
catch (Exception e)
{
throw CacheExceptionMapper.Convert(e);
}
return new CacheDictionarySetResponse();
}

public async Task<CacheDictionarySetResponse> DictionarySetAsync(string cacheName, string dictionaryName, byte[] field, byte[] value, bool refreshTtl, uint? ttlSeconds = null)
Expand All @@ -74,12 +74,12 @@ private async Task<CacheDictionarySetResponse> SendDictionarySetAsync(string cac
try
{
await this.grpcManager.Client.DictionarySetAsync(request, MetadataWithCache(cacheName), deadline: CalculateDeadline());
return new CacheDictionarySetResponse();
}
catch (Exception e)
{
throw CacheExceptionMapper.Convert(e);
}
return new CacheDictionarySetResponse();
}

public CacheDictionaryGetResponse DictionaryGet(string cacheName, string dictionaryName, byte[] field)
Expand Down Expand Up @@ -162,12 +162,12 @@ public CacheDictionarySetMultiResponse SendDictionarySetMulti(string cacheName,
try
{
this.grpcManager.Client.DictionarySet(request, MetadataWithCache(cacheName), deadline: CalculateDeadline());
return new CacheDictionarySetMultiResponse();
}
catch (Exception e)
{
throw CacheExceptionMapper.Convert(e);
}
return new CacheDictionarySetMultiResponse();
}

public async Task<CacheDictionarySetMultiResponse> DictionarySetMultiAsync(string cacheName, string dictionaryName, IEnumerable<KeyValuePair<byte[], byte[]>> items, bool refreshTtl, uint? ttlSeconds = null)
Expand Down Expand Up @@ -195,12 +195,12 @@ public async Task<CacheDictionarySetMultiResponse> SendDictionarySetMultiAsync(s
try
{
await this.grpcManager.Client.DictionarySetAsync(request, MetadataWithCache(cacheName), deadline: CalculateDeadline());
return new CacheDictionarySetMultiResponse();
}
catch (Exception e)
{
throw CacheExceptionMapper.Convert(e);
}
return new CacheDictionarySetMultiResponse();
}

public CacheDictionaryGetMultiResponse DictionaryGetMulti(string cacheName, string dictionaryName, params byte[][] fields)
Expand Down Expand Up @@ -228,15 +228,16 @@ private CacheDictionaryGetMultiResponse SendDictionaryGetMulti(string cacheName,
_DictionaryGetRequest request = new() { DictionaryName = dictionaryName.ToByteString() };
request.DictionaryKeys.Add(fields);

_DictionaryGetResponse response;
try
{
var response = this.grpcManager.Client.DictionaryGet(request, MetadataWithCache(cacheName), deadline: CalculateDeadline());
return new CacheDictionaryGetMultiResponse(response);
response = this.grpcManager.Client.DictionaryGet(request, MetadataWithCache(cacheName), deadline: CalculateDeadline());
}
catch (Exception e)
{
throw CacheExceptionMapper.Convert(e);
}
return new CacheDictionaryGetMultiResponse(response);
}

public async Task<CacheDictionaryGetMultiResponse> DictionaryGetMultiAsync(string cacheName, string dictionaryName, params byte[][] fields)
Expand Down Expand Up @@ -264,15 +265,16 @@ private async Task<CacheDictionaryGetMultiResponse> SendDictionaryGetMultiAsync(
_DictionaryGetRequest request = new() { DictionaryName = dictionaryName.ToByteString() };
request.DictionaryKeys.Add(fields);

_DictionaryGetResponse response;
try
{
var response = await this.grpcManager.Client.DictionaryGetAsync(request, MetadataWithCache(cacheName), deadline: CalculateDeadline());
return new CacheDictionaryGetMultiResponse(response);
response = await this.grpcManager.Client.DictionaryGetAsync(request, MetadataWithCache(cacheName), deadline: CalculateDeadline());
}
catch (Exception e)
{
throw CacheExceptionMapper.Convert(e);
}
return new CacheDictionaryGetMultiResponse(response);
}

public CacheDictionaryGetAllResponse DictionaryGetAll(string cacheName, string dictionaryName)
Expand Down
16 changes: 8 additions & 8 deletions Momento/Incubating/SimpleCacheClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public async Task<CacheGetMultiResponse> GetMultiAsync(string cacheName, params
return await this.simpleCacheClient.GetMultiAsync(cacheName, keys);
}

public async Task<CacheSetMultiResponse> SetMultiAsync(string cacheName, IDictionary<byte[], byte[]> items, uint? ttlSeconds = null)
public async Task<CacheSetMultiResponse> SetMultiAsync(string cacheName, IEnumerable<KeyValuePair<byte[], byte[]>> items, uint? ttlSeconds = null)
{
return await this.simpleCacheClient.SetMultiAsync(cacheName, items, ttlSeconds);
}

public async Task<CacheSetMultiResponse> SetMultiAsync(string cacheName, IDictionary<string, string> items, uint? ttlSeconds = null)
public async Task<CacheSetMultiResponse> SetMultiAsync(string cacheName, IEnumerable<KeyValuePair<string, string>> items, uint? ttlSeconds = null)
{
return await this.simpleCacheClient.SetMultiAsync(cacheName, items, ttlSeconds);
}
Expand Down Expand Up @@ -146,12 +146,12 @@ public CacheGetMultiResponse GetMulti(string cacheName, params string[] keys)
return this.simpleCacheClient.GetMulti(cacheName, keys);
}

public CacheSetMultiResponse SetMulti(string cacheName, IDictionary<byte[], byte[]> items, uint? ttlSeconds = null)
public CacheSetMultiResponse SetMulti(string cacheName, IEnumerable<KeyValuePair<byte[], byte[]>> items, uint? ttlSeconds = null)
{
return this.simpleCacheClient.SetMulti(cacheName, items, ttlSeconds);
}

public CacheSetMultiResponse SetMulti(string cacheName, IDictionary<string, string> items, uint? ttlSeconds = null)
public CacheSetMultiResponse SetMulti(string cacheName, IEnumerable<KeyValuePair<string, string>> items, uint? ttlSeconds = null)
{
return this.simpleCacheClient.SetMulti(cacheName, items, ttlSeconds);
}
Expand Down Expand Up @@ -353,7 +353,7 @@ public CacheDictionarySetMultiResponse DictionarySetMulti(string cacheName, stri
Utils.ArgumentNotNull(cacheName, nameof(cacheName));
Utils.ArgumentNotNull(dictionaryName, nameof(dictionaryName));
Utils.ArgumentNotNull(items, nameof(items));
Utils.ValuesNotNull(items, nameof(items));
Utils.KeysAndValuesNotNull(items, nameof(items));

return this.dataClient.DictionarySetMulti(cacheName, dictionaryName, items, refreshTtl, ttlSeconds);
}
Expand All @@ -377,7 +377,7 @@ public CacheDictionarySetMultiResponse DictionarySetMulti(string cacheName, stri
Utils.ArgumentNotNull(cacheName, nameof(cacheName));
Utils.ArgumentNotNull(dictionaryName, nameof(dictionaryName));
Utils.ArgumentNotNull(items, nameof(items));
Utils.ValuesNotNull(items, nameof(items));
Utils.KeysAndValuesNotNull(items, nameof(items));

return this.dataClient.DictionarySetMulti(cacheName, dictionaryName, items, refreshTtl, ttlSeconds);
}
Expand All @@ -401,7 +401,7 @@ public async Task<CacheDictionarySetMultiResponse> DictionarySetMultiAsync(strin
Utils.ArgumentNotNull(cacheName, nameof(cacheName));
Utils.ArgumentNotNull(dictionaryName, nameof(dictionaryName));
Utils.ArgumentNotNull(items, nameof(items));
Utils.ValuesNotNull(items, nameof(items));
Utils.KeysAndValuesNotNull(items, nameof(items));

return await this.dataClient.DictionarySetMultiAsync(cacheName, dictionaryName, items, refreshTtl, ttlSeconds);
}
Expand All @@ -425,7 +425,7 @@ public async Task<CacheDictionarySetMultiResponse> DictionarySetMultiAsync(strin
Utils.ArgumentNotNull(cacheName, nameof(cacheName));
Utils.ArgumentNotNull(dictionaryName, nameof(dictionaryName));
Utils.ArgumentNotNull(items, nameof(items));
Utils.ValuesNotNull(items, nameof(items));
Utils.KeysAndValuesNotNull(items, nameof(items));

return await this.dataClient.DictionarySetMultiAsync(cacheName, dictionaryName, items, refreshTtl, ttlSeconds);
}
Expand Down
Loading

0 comments on commit c9aaec5

Please sign in to comment.