-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: added middleware cache impl * chore: integrated entity changed function * chore: added cache provider for some transformation population calls * chore: added flyouts for some buttons * chore: updated redirect
- Loading branch information
Showing
24 changed files
with
502 additions
and
125 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
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
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 |
---|---|---|
|
@@ -107,4 +107,3 @@ export function TagsModule() { | |
</> | ||
) | ||
} | ||
|
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
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
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 |
---|---|---|
@@ -1,31 +1,102 @@ | ||
using System.Text.Json; | ||
using Microsoft.Extensions.Caching.Distributed; | ||
using StackExchange.Redis; | ||
|
||
namespace main.Configuratons; | ||
|
||
public class CacheProvider | ||
{ | ||
private readonly IDistributedCache _cache; | ||
private readonly IDatabase _redis; | ||
private readonly IServer _redisServer; | ||
public static readonly bool CacheEnabled = true; // Controls if caching is enabled or not appwide. | ||
public static readonly string CachePrefix = "mfoni-"; | ||
public static readonly TimeSpan? CacheTTL = null; | ||
|
||
public CacheProvider(IDistributedCache cache) | ||
public static readonly Dictionary<string, string> CacheEntities = new Dictionary<string, string> | ||
{ | ||
_cache = cache; | ||
// Only caching content related entities for now. | ||
{ "collections", "collections" }, | ||
{ "contents", "contents" }, | ||
{ "tags", "tags" }, | ||
|
||
// { "admins", "admins" }, | ||
// { "auth", "users" }, | ||
// { "waitlists", "waitlist" }, | ||
// { "users", "users" }, | ||
// { "creator-subscriptions", "subscriptions" }, | ||
// // TODO: update endpoint to get wallet for admins and make sure to update admins app. | ||
// { "wallet-transactions", "wallet-transactions" }, | ||
// { "wallets", "wallets" }, | ||
// { "wallet", "wallets" }, | ||
}; | ||
|
||
public CacheProvider(IConnectionMultiplexer redis) | ||
{ | ||
_redis = redis.GetDatabase(); | ||
_redisServer = redis.GetServer(redis.GetEndPoints()[0]); | ||
} | ||
|
||
public async Task<T?> GetFromCache<T>(string key) | ||
{ | ||
var redisValue = await _redis.StringGetAsync($"{CachePrefix}{key}"); | ||
return redisValue.HasValue ? JsonSerializer.Deserialize<T>(redisValue!) : default(T); | ||
} | ||
|
||
public async Task SetCache<T>(string key, T value, TimeSpan? timeSpan) | ||
{ | ||
var redisValue = JsonSerializer.Serialize(value); | ||
|
||
if (typeof(T) == typeof(string)) | ||
{ | ||
redisValue = value as string; | ||
} | ||
|
||
// TODO: lock the cache key when making updates - integrate redlock. | ||
await _redis.StringSetAsync($"{CachePrefix}{key}", redisValue, timeSpan); | ||
} | ||
|
||
public async Task ClearCache(RedisKey key) | ||
{ | ||
await _redis.KeyDeleteAsync(key); | ||
} | ||
|
||
public async Task<T> GetFromCache<T>(string key) | ||
public async Task UpdateCacheExpiry(RedisKey key, TimeSpan timeSpan) | ||
{ | ||
var cachedUsers = await _cache.GetStringAsync(key); | ||
return cachedUsers == null ? default(T) : JsonSerializer.Deserialize<T>(cachedUsers); | ||
await _redis.KeyExpireAsync($"{CachePrefix}{key}", timeSpan); | ||
} | ||
|
||
public async Task SetCache<T>(string key, T value, DistributedCacheEntryOptions options) | ||
public async Task EntityChanged(string[] Queries) | ||
{ | ||
var users = JsonSerializer.Serialize(value); | ||
await _cache.SetStringAsync(key, users, options); | ||
if (CacheEnabled) | ||
{ | ||
foreach (var query in Queries) | ||
{ | ||
var result = _redisServer.Keys(pattern: $"{CachePrefix}*{query}"); | ||
await _redis.KeyDeleteAsync(result.ToArray()); | ||
} | ||
} | ||
} | ||
|
||
public async Task ClearCache(string key) | ||
public async Task<T> ResolveCache<T>(string key, Func<Task<T>> resolver) | ||
{ | ||
await _cache.RemoveAsync(key); | ||
if (CacheEnabled) | ||
{ | ||
var cached = await GetFromCache<T>(key); | ||
if (cached != null) | ||
{ | ||
|
||
if (CacheTTL != null) | ||
{ | ||
await UpdateCacheExpiry(key, (TimeSpan)CacheTTL); | ||
} | ||
|
||
return cached; | ||
} | ||
|
||
var result = await resolver(); | ||
await SetCache(key, result, CacheTTL); | ||
return result; | ||
} | ||
|
||
return await resolver(); | ||
} | ||
} |
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
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
Oops, something went wrong.