-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32a79fd
commit 9fdfe04
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...nsExtensions/AbstractionsExtensions.Library/CacheManagement/Repositories/ICacheService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace AbstractionsExtensions.Library.CacheManagement.Repositories; | ||
|
||
public interface ICacheService | ||
{ | ||
/// <summary> | ||
/// Get Data using key | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="key"></param> | ||
/// <returns></returns> | ||
T GetData<T>(string key); | ||
/// <summary> | ||
/// Set Data with Value and Expiration Time of Key | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="key"></param> | ||
/// <param name="value"></param> | ||
/// <param name="expirationTime"></param> | ||
/// <returns></returns> | ||
bool SetData<T>(string key, T value, DateTimeOffset expirationTime); | ||
/// <summary> | ||
/// Remove Data | ||
/// </summary> | ||
/// <param name="key"></param> | ||
/// <returns></returns> | ||
object RemoveData(string key); | ||
} |
42 changes: 42 additions & 0 deletions
42
...actionsExtensions/AbstractionsExtensions.Library/CacheManagement/Services/CacheService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using AbstractionsExtensions.Library.CacheManagement.Repositories; | ||
using Newtonsoft.Json; | ||
using StackExchange.Redis; | ||
|
||
namespace AbstractionsExtensions.Library.CacheManagement.Services; | ||
|
||
public class CacheService : ICacheService | ||
{ | ||
private IDatabase _db; | ||
public CacheService() | ||
{ | ||
ConfigureRedis(); | ||
} | ||
private void ConfigureRedis() | ||
{ | ||
//_db = ConnectionHelper.Connection.GetDatabase(); | ||
} | ||
public T GetData<T>(string key) | ||
{ | ||
var value = _db.StringGet(key); | ||
if (!string.IsNullOrEmpty(value)) | ||
{ | ||
return JsonConvert.DeserializeObject<T>(value); | ||
} | ||
return default; | ||
} | ||
public bool SetData<T>(string key, T value, DateTimeOffset expirationTime) | ||
{ | ||
TimeSpan expiryTime = expirationTime.DateTime.Subtract(DateTime.Now); | ||
var isSet =_db.StringSet(key, JsonConvert.SerializeObject(value), expiryTime); | ||
return isSet; | ||
} | ||
public object RemoveData(string key) | ||
{ | ||
bool _isKeyExist = _db.KeyExists(key); | ||
if (_isKeyExist == true) | ||
{ | ||
return _db.KeyDelete(key); | ||
} | ||
return false; | ||
} | ||
} |