From 4749ddf015db2a28e9f3f281b906c7f43fcc2015 Mon Sep 17 00:00:00 2001 From: kubagdynia Date: Thu, 9 May 2024 12:04:27 +0200 Subject: [PATCH] Version 0.2.0. Key hashing capability added. Can be used for long keys. --- CacheDrive/CacheDrive.csproj | 4 +- CacheDrive/Configuration/CacheSettings.cs | 3 +- LICENSE | 2 +- README.md | 132 ++++++++++++++++++++-- 4 files changed, 127 insertions(+), 14 deletions(-) diff --git a/CacheDrive/CacheDrive.csproj b/CacheDrive/CacheDrive.csproj index 69beb52..0b49eca 100644 --- a/CacheDrive/CacheDrive.csproj +++ b/CacheDrive/CacheDrive.csproj @@ -5,13 +5,13 @@ CacheDrive Simple in-memory caching provider with the ability to store objects in files. cache memory-cache file-cahce - Update libraries. + Key hashing capability added. Can be used for long keys. https://github.com/kubagdynia/CacheDrive true https://github.com/kubagdynia/CacheDrive MIT 2023-2024 - 0.1.2 + 0.2.0 Jakub Kurłowicz CacheDrive.png diff --git a/CacheDrive/Configuration/CacheSettings.cs b/CacheDrive/Configuration/CacheSettings.cs index 5b67d3b..a69b4c4 100644 --- a/CacheDrive/Configuration/CacheSettings.cs +++ b/CacheDrive/Configuration/CacheSettings.cs @@ -47,7 +47,8 @@ public class CacheSettings public bool FlushOnExit { get; set; } = true; /// - /// + /// Salt, which will be added to the key hash. + /// Default value is an empty string, which means that adding salt is disabled. /// public string HashKeySalt { get; set; } = ""; } \ No newline at end of file diff --git a/LICENSE b/LICENSE index 16ccdeb..6d73fce 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Jakub Kurłowicz +Copyright (c) 2023-2024 Jakub Kurłowicz Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 05cd26c..25514af 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ dotnet add package CacheDrive or just copy into the project file to reference the package ``` - + ``` @@ -60,7 +60,8 @@ serviceProvider.Dispose(); "CacheExpiration": 60, "CacheType": "MemoryAndFile", "InitializeOnStartup": true, - "FlushOnExit": true + "FlushOnExit": true, + "HashKeySalt": "Secret123Secret" } } ``` @@ -74,7 +75,8 @@ services.RegisterCacheDrive(settings: new CacheSettings CacheExpiration = 60, CacheType = CacheType.MemoryAndFile, InitializeOnStartup = true, - FlushOnExit = true + FlushOnExit = true, + HashKeySalt = "Secret123Secret" }); ``` - Then by injecting ICacheService you can start using write and read from the cache @@ -92,19 +94,46 @@ public class App { // SetAsync, GetAsync and TryGetValue string cacheKey1 = "testKey"; - await _cacheService.SetAsync(cacheKey1, "test text..."); - var cachedValue1 = await _cacheService.GetAsync(cacheKey1); + await cacheService.SetAsync(cacheKey1, "test text..."); + var cachedValue1 = await cacheService.GetAsync(cacheKey1); Console.WriteLine($"GetAsync - cached value: {cachedValue1}"); - Console.WriteLine(_cacheService.TryGetValue(cacheKey1, out string cachedValue2) + Console.WriteLine(cacheService.TryGetValue(cacheKey1, out string cachedValue2) ? $"TryGetValue OK - cached value: {cachedValue2}" : $"TryGetValue NOK - cached value: {cachedValue2}"); - + + Console.WriteLine(); + // Set, Get - string cacheKey2 = "testKey2"; - _cacheService.Set(cacheKey2, 1234567); - int cachedValue3 = _cacheService.Get(cacheKey2); + string cacheKey3 = "testKey2"; + cacheService.Set(cacheKey3, 1234567); + int cachedValue3 = cacheService.Get(cacheKey3); Console.WriteLine($"Get - cached value: {cachedValue3} "); + + // ************ with hashKey + + Console.WriteLine(); + Console.WriteLine("with hashKey"); + Console.WriteLine(); + + // SetAsync, GetAsync and TryGetValue with hashKey + string cacheKey2 = "testKey"; + await cacheService.SetAsync(cacheKey2, "test text...", hashKey: true); + var cachedValueWithHashKey1 = await cacheService.GetAsync(cacheKey2, hashKey: true); + Console.WriteLine($"GetAsync with hashKey - cached value: {cachedValueWithHashKey1}"); + + Console.WriteLine(cacheService.TryGetValue(cacheKey2, hashKey: true, out string cachedValueWithHashKey2) + ? $"TryGetValue with haskkey OK - cached value: {cachedValueWithHashKey2}" + : $"TryGetValue with haskkey NOK - cached value: {cachedValueWithHashKey2}"); + + // Set, Get with hashKey + + Console.WriteLine(); + + string cacheKey4 = "testKey2"; + cacheService.Set(cacheKey4, 1234567, hashKey: true); + int cachedValueWithHashKey3 = cacheService.Get(cacheKey4, hashKey: true); + Console.WriteLine($"Get with hashKey - cached value: {cachedValueWithHashKey3} "); } } ``` @@ -136,6 +165,14 @@ public interface ICacheService /// true if the key was found in the cache, otherwise, false. bool HasItem(string key); + /// + /// Returns whether an object with the specified key exists in the cache. + /// + /// The key of the value to check. + /// Whether the key should be hashed. Can be used for long keys. + /// true if the key was found in the cache, otherwise, false. + bool HasItem(string key, bool hashKey); + /// /// Attempts to get the value associated with the specified key from the cache. /// @@ -148,6 +185,19 @@ public interface ICacheService /// true if the key was found in the cache, otherwise, false. bool TryGetValue(string key, out T value); + /// + /// Attempts to get the value associated with the specified key from the cache. + /// + /// The key of the value to get. + /// Whether the key should be hashed. Can be used for long keys. + /// + /// When this method returns, value contains the object from + /// the cache with the specified key or the default value of + /// , if the operation failed. + /// + /// true if the key was found in the cache, otherwise, false. + bool TryGetValue(string key, bool hashKey, out T value); + /// /// Get the value associated with the specified key from the cache. /// @@ -156,6 +206,16 @@ public interface ICacheService /// key or the default value of T, if the operation failed. /// T Get(string key); + + /// + /// Get the value associated with the specified key from the cache. + /// + /// The key of the value to get. + /// Whether the key should be hashed. Can be used for long keys. + /// The value contains the object from the cache with the specified + /// key or the default value of T, if the operation failed. + /// + T Get(string key, bool hashKey); /// /// Get the value associated with the specified key from the cache. @@ -166,6 +226,16 @@ public interface ICacheService /// Task GetAsync(string key); + /// + /// Get the value associated with the specified key from the cache. + /// + /// The key of the value to get. + /// Whether the key should be hashed. Can be used for long keys. + /// The value contains the object from the cache with the specified + /// key or the default value of T, if the operation failed. + /// + Task GetAsync(string key, bool hashKey); + /// /// Adds a value to the cache if the key does not already exist, or updates if the key already exists. /// @@ -174,6 +244,16 @@ public interface ICacheService /// After how many seconds a given value will expire in the cache. Optional parameter. /// By default, the value is taken from the configuration. void Set(string key, T value, int expirySeconds = 0); + + /// + /// Adds a value to the cache if the key does not already exist, or updates if the key already exists. + /// + /// The key to be added or whose value should be updated. + /// The value to add or update. + /// Whether the key should be hashed. Can be used for long keys. + /// After how many seconds a given value will expire in the cache. Optional parameter. + /// By default, the value is taken from the configuration. + void Set(string key, T value, bool hashKey, int expirySeconds = 0); /// /// Adds a value to the cache if the key does not already exist, or updates if the key already exists. @@ -184,12 +264,30 @@ public interface ICacheService /// By default, the value is taken from the configuration. Task SetAsync(string key, T value, int expirySeconds = 0); + /// + /// Adds a value to the cache if the key does not already exist, or updates if the key already exists. + /// + /// The key to be added or whose value should be updated. + /// The value to add or update. + /// Whether the key should be hashed. Can be used for long keys. + /// After how many seconds a given value will expire in the cache. Optional parameter. + /// By default, the value is taken from the configuration. + Task SetAsync(string key, T value, bool hashKey, int expirySeconds = 0); + /// /// Deletes the object associated with the given key. /// /// The key of the element to be remove. /// true if an object was removed successfully; otherwise, false. bool Delete(string key); + + /// + /// Deletes the object associated with the given key. + /// + /// The key of the element to be remove. + /// Whether the key should be hashed. Can be used for long keys. + /// true if an object was removed successfully; otherwise, false. + bool Delete(string key, bool hashKey); /// /// Deletes the object associated with the given key. @@ -198,6 +296,14 @@ public interface ICacheService /// true if an object was removed successfully; otherwise, false. Task DeleteAsync(string key); + /// + /// Deletes the object associated with the given key. + /// + /// The key of the element to be remove. + /// Whether the key should be hashed. Can be used for long keys. + /// true if an object was removed successfully; otherwise, false. + Task DeleteAsync(string key, bool hashKey); + /// /// Returns the number of objects in the cache. /// @@ -266,6 +372,12 @@ public class CacheSettings /// Default value is true. /// public bool FlushOnExit { get; set; } = true; + + /// + /// Salt, which will be added to the key hash. + /// Default value is an empty string, which means that adding salt is disabled. + /// + public string HashKeySalt { get; set; } = ""; } ```