-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Feature/use distributed caching (#110) * Implement Redis caching * Reduce tweet attempts to just 3 (#111)
- Loading branch information
Showing
18 changed files
with
245 additions
and
111 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
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Application.Cache | ||
{ | ||
public interface ICacheService<T> | ||
{ | ||
/// <summary> | ||
/// Retrieves a collection of items from the cache. | ||
/// </summary> | ||
/// <param name="key">The key under which the items are cached.</param> | ||
/// <returns>A task that represents the asynchronous operation. The task result contains a collection of items.</returns> | ||
Task<IEnumerable<T>> GetAsync(string key); | ||
|
||
/// <summary> | ||
/// Adds an item to the cache and updates its recency. | ||
/// </summary> | ||
/// <param name="key">The key under which the item is cached.</param> | ||
/// <param name="item">The item to be added to the cache.</param> | ||
/// <returns>A task that represents the asynchronous operation.</returns> | ||
Task StackAsync(string key, T item); | ||
|
||
/// <summary> | ||
/// Removes an item from the cache. | ||
/// </summary> | ||
/// <param name="key">The key of the item to be removed.</param> | ||
/// <returns>A task that represents the asynchronous operation. The task result contains a boolean indicating whether the removal was successful.</returns> | ||
Task RemoveAsync(string key, string searchTerm); | ||
|
||
/// <summary> | ||
/// Retrieves the most popular items based on their frequency. | ||
/// </summary> | ||
/// <returns>A task that represents the asynchronous operation. The task result contains a collection of the most popular items.</returns> | ||
Task<IEnumerable<string>> GetMostPopularAsync(string key); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,7 @@ | ||
namespace Infrastructure.Configuration | ||
{ | ||
public record RedisConfig | ||
{ | ||
public int DatabaseIndex { get; set; } | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Ardalis.GuardClauses; | ||
using Infrastructure.Configuration; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using StackExchange.Redis; | ||
|
||
namespace Infrastructure.Redis | ||
{ | ||
public static class DependencyInjection | ||
{ | ||
private const string SectionName = "Redis"; | ||
|
||
public static IServiceCollection SetupRedis(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
services.Configure<RedisConfig>(configuration.GetRequiredSection(SectionName)); | ||
var redisConnectionString = Guard.Against.NullOrEmpty(configuration.GetConnectionString(SectionName)); | ||
services.AddSingleton<IConnectionMultiplexer>(ConnectionMultiplexer.Connect(redisConnectionString)); | ||
return services; | ||
} | ||
} | ||
} |
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,13 @@ | ||
using Infrastructure.Configuration; | ||
using Microsoft.Extensions.Options; | ||
using StackExchange.Redis; | ||
|
||
namespace Infrastructure.Redis | ||
{ | ||
public abstract class RedisCache( | ||
IConnectionMultiplexer connectionMultiplexer, | ||
IOptions<RedisConfig> redisConfig) | ||
{ | ||
protected readonly IDatabase _cache = connectionMultiplexer.GetDatabase(redisConfig.Value.DatabaseIndex); | ||
} | ||
} |
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,46 @@ | ||
using Core.Cache; | ||
using Infrastructure.Configuration; | ||
using Microsoft.Extensions.Options; | ||
using StackExchange.Redis; | ||
|
||
namespace Infrastructure.Redis | ||
{ | ||
public class RedisRecentIndexesCache( | ||
IConnectionMultiplexer connectionMultiplexer, | ||
IOptions<RedisConfig> redisConfig) : RedisCache(connectionMultiplexer, redisConfig), IRecentIndexesCache | ||
{ | ||
private const string Key = "recent_indexes"; | ||
private const int MaxItemsToReturn = 5; | ||
private const int MaxItemsToStore = 10; | ||
|
||
public async Task<IEnumerable<string>> Get() | ||
{ | ||
var results = await _cache.SortedSetRangeByRankAsync(Key, 0, MaxItemsToReturn - 1, Order.Descending); | ||
return results.Select(r => r.ToString()); | ||
} | ||
|
||
public async Task<bool> Remove(string item) | ||
{ | ||
var tran = _cache.CreateTransaction(); | ||
_ = tran.SortedSetRemoveAsync(Key, item); | ||
return await tran.ExecuteAsync(); | ||
} | ||
|
||
public async Task Stack(string item) | ||
{ | ||
// Use a Redis transaction to ensure atomicity of both operations | ||
var transaction = _cache.CreateTransaction(); | ||
|
||
// Add the search term to the front of the Redis list | ||
_ = transaction.SortedSetAddAsync(Key, item, DateTime.UtcNow.Ticks); | ||
_ = transaction.SortedSetRemoveRangeByRankAsync(Key, 0, -(MaxItemsToStore + 1)); | ||
|
||
// Execute the transaction | ||
bool committed = await transaction.ExecuteAsync(); | ||
if (!committed) | ||
{ | ||
throw new Exception("Redis Transaction failed"); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.