Skip to content

Commit

Permalink
Added the ability to hash the key. Can be used for long keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
kubagdynia committed May 8, 2024
1 parent 89725b0 commit 3c33e47
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 4 deletions.
17 changes: 17 additions & 0 deletions CacheDrive/Services/HashString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Security.Cryptography;

namespace CacheDrive.Services;

internal static class HashString
{
public static string HashKey(string key)
{
// Convert the string to a byte array first, to be processed
var textBytes = System.Text.Encoding.UTF8.GetBytes(key);
var hashBytes = SHA256.HashData(textBytes);

// Convert back to a string, removing the '-' that BitConverter adds
return BitConverter.ToString(hashBytes).Replace("-", string.Empty);
}
}
77 changes: 77 additions & 0 deletions CacheDrive/Services/ICacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public interface ICacheService
/// <returns>true if the key was found in the cache, otherwise, false.</returns>
bool HasItem(string key);

/// <summary>
/// Returns whether an object with the specified key exists in the cache.
/// </summary>
/// <param name="key">The key of the value to check.</param>
/// <param name="hashKey">Whether the key should be hashed. Can be used for long keys.</param>
/// <returns>true if the key was found in the cache, otherwise, false.</returns>
bool HasItem(string key, bool hashKey);

/// <summary>
/// Attempts to get the value associated with the specified key from the cache.
/// </summary>
Expand All @@ -37,6 +45,19 @@ public interface ICacheService
/// <returns>true if the key was found in the cache, otherwise, false.</returns>
bool TryGetValue<T>(string key, out T value);

/// <summary>
/// Attempts to get the value associated with the specified key from the cache.
/// </summary>
/// <param name="key">The key of the value to get.</param>
/// <param name="hashKey">Whether the key should be hashed. Can be used for long keys.</param>
/// <param name="value">
/// When this method returns, value contains the object from
/// the cache with the specified key or the default value of
/// <typeparamref name="T"/>, if the operation failed.
/// </param>
/// <returns>true if the key was found in the cache, otherwise, false.</returns>
bool TryGetValue<T>(string key, bool hashKey, out T value);

/// <summary>
/// Get the value associated with the specified key from the cache.
/// </summary>
Expand All @@ -45,6 +66,16 @@ public interface ICacheService
/// key or the default value of T, if the operation failed.
/// </returns>
T Get<T>(string key);

/// <summary>
/// Get the value associated with the specified key from the cache.
/// </summary>
/// <param name="key">The key of the value to get.</param>
/// <param name="hashKey">Whether the key should be hashed. Can be used for long keys.</param>
/// <returns>The value contains the object from the cache with the specified
/// key or the default value of T, if the operation failed.
/// </returns>
T Get<T>(string key, bool hashKey);

/// <summary>
/// Get the value associated with the specified key from the cache.
Expand All @@ -55,6 +86,16 @@ public interface ICacheService
/// </returns>
Task<T> GetAsync<T>(string key);

/// <summary>
/// Get the value associated with the specified key from the cache.
/// </summary>
/// <param name="key">The key of the value to get.</param>
/// <param name="hashKey">Whether the key should be hashed. Can be used for long keys.</param>
/// <returns>The value contains the object from the cache with the specified
/// key or the default value of T, if the operation failed.
/// </returns>
Task<T> GetAsync<T>(string key, bool hashKey);

/// <summary>
/// Adds a value to the cache if the key does not already exist, or updates if the key already exists.
/// </summary>
Expand All @@ -63,6 +104,16 @@ public interface ICacheService
/// <param name="expirySeconds">After how many seconds a given value will expire in the cache. Optional parameter.
/// By default, the value is taken from the configuration.</param>
void Set<T>(string key, T value, int expirySeconds = 0);

/// <summary>
/// Adds a value to the cache if the key does not already exist, or updates if the key already exists.
/// </summary>
/// <param name="key">The key to be added or whose value should be updated.</param>
/// <param name="value">The value to add or update.</param>
/// <param name="hashKey">Whether the key should be hashed. Can be used for long keys.</param>
/// <param name="expirySeconds">After how many seconds a given value will expire in the cache. Optional parameter.
/// By default, the value is taken from the configuration.</param>
void Set<T>(string key, T value, bool hashKey, int expirySeconds = 0);

/// <summary>
/// Adds a value to the cache if the key does not already exist, or updates if the key already exists.
Expand All @@ -73,12 +124,30 @@ public interface ICacheService
/// By default, the value is taken from the configuration.</param>
Task SetAsync<T>(string key, T value, int expirySeconds = 0);

/// <summary>
/// Adds a value to the cache if the key does not already exist, or updates if the key already exists.
/// </summary>
/// <param name="key">The key to be added or whose value should be updated.</param>
/// <param name="value">The value to add or update.</param>
/// <param name="hashKey">Whether the key should be hashed. Can be used for long keys.</param>
/// <param name="expirySeconds">After how many seconds a given value will expire in the cache. Optional parameter.
/// By default, the value is taken from the configuration.</param>
Task SetAsync<T>(string key, T value, bool hashKey, int expirySeconds = 0);

/// <summary>
/// Deletes the object associated with the given key.
/// </summary>
/// <param name="key">The key of the element to be remove.</param>
/// <returns>true if an object was removed successfully; otherwise, false.</returns>
bool Delete<T>(string key);

/// <summary>
/// Deletes the object associated with the given key.
/// </summary>
/// <param name="key">The key of the element to be remove.</param>
/// <param name="hashKey">Whether the key should be hashed. Can be used for long keys.</param>
/// <returns>true if an object was removed successfully; otherwise, false.</returns>
bool Delete<T>(string key, bool hashKey);

/// <summary>
/// Deletes the object associated with the given key.
Expand All @@ -87,6 +156,14 @@ public interface ICacheService
/// <returns>true if an object was removed successfully; otherwise, false.</returns>
Task<bool> DeleteAsync<T>(string key);

/// <summary>
/// Deletes the object associated with the given key.
/// </summary>
/// <param name="key">The key of the element to be remove.</param>
/// <param name="hashKey">Whether the key should be hashed. Can be used for long keys.</param>
/// <returns>true if an object was removed successfully; otherwise, false.</returns>
Task<bool> DeleteAsync<T>(string key, bool hashKey);

/// <summary>
/// Returns the number of objects in the cache.
/// </summary>
Expand Down
32 changes: 28 additions & 4 deletions CacheDrive/Services/MemoryCacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public bool HasItem(string key)

return false;
}


public bool HasItem(string key, bool hashKey)
=> HasItem(hashKey ? HashString.HashKey(key) : key);

public bool TryGetValue<T>(string key, out T value)
{
if (!CacheSettings.CacheEnabled)
Expand Down Expand Up @@ -75,6 +78,9 @@ public bool TryGetValue<T>(string key, out T value)
return false;
}

public bool TryGetValue<T>(string key, bool hashKey, out T value)
=> TryGetValue(hashKey ? HashString.HashKey(key) : key, out value);

private bool TryGetCacheableValue<T>(string key, out T value) where T : ICacheable
{
string cacheKey = T.GetCacheKey(key);
Expand Down Expand Up @@ -112,7 +118,10 @@ public T Get<T>(string key)

return cachedItem.Value;
}


public T Get<T>(string key, bool hashKey)
=> Get<T>(hashKey ? HashString.HashKey(key) : key);

public async Task<T> GetAsync<T>(string key)
{
if (!CacheSettings.CacheEnabled)
Expand All @@ -130,6 +139,9 @@ public async Task<T> GetAsync<T>(string key)
return await Task.FromResult(cachedItem.Value);
}

public Task<T> GetAsync<T>(string key, bool hashKey)
=> GetAsync<T>(hashKey ? HashString.HashKey(key) : key);

private async Task<T> GetCacheableAsync<T>(string key) where T : ICacheable
{
string cacheKey = T.GetCacheKey(key);
Expand Down Expand Up @@ -175,7 +187,10 @@ public void Set<T>(string key, T value, int expirySeconds = 0)

SetCacheableItem(CacheableItem<T>.Create(key, value), expirySeconds);
}


public void Set<T>(string key, T value, bool hashKey, int expirySeconds = 0)
=> Set(hashKey ? HashString.HashKey(key) : key, value, expirySeconds);

public Task SetAsync<T>(string key, T value, int expirySeconds = 0)
{
if (!CacheSettings.CacheEnabled)
Expand All @@ -185,7 +200,10 @@ public Task SetAsync<T>(string key, T value, int expirySeconds = 0)

return SetAsync(CacheableItem<T>.Create(key, value), expirySeconds);
}


public Task SetAsync<T>(string key, T value, bool hashKey, int expirySeconds = 0)
=> SetAsync(hashKey ? HashString.HashKey(key) : key, value, expirySeconds);

private void SetCacheableItem<T>(T item, int expirySeconds = 0) where T : ICacheable
{
TimeSpan length = GetExpirationLength(expirySeconds);
Expand Down Expand Up @@ -253,6 +271,9 @@ public bool Delete<T>(string key)
return key is not null && Storage.TryRemove(CacheableItem<T>.GetCacheKey(key), out _);
}

public bool Delete<T>(string key, bool hashKey)
=> Delete<T>(hashKey ? HashString.HashKey(key) : key);

public Task<bool> DeleteAsync<T>(string key)
{
if (!CacheSettings.CacheEnabled)
Expand All @@ -263,6 +284,9 @@ public Task<bool> DeleteAsync<T>(string key)
return key is null ? Task.FromResult(false) : DeleteAsync(CacheableItem<T>.GetCacheKey(key));
}

public Task<bool> DeleteAsync<T>(string key, bool hashKey)
=> DeleteAsync<T>(hashKey ? HashString.HashKey(key) : key);

private Task<bool> DeleteAsync(string key)
=> key is null ? Task.FromResult(false) : Task.FromResult(Storage.TryRemove(key, out _));

Expand Down

0 comments on commit 3c33e47

Please sign in to comment.