-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
117 changed files
with
2,689 additions
and
420 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
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
58 changes: 58 additions & 0 deletions
58
src/Color-Chan.Discord.Caching/Color-Chan.Discord.Caching.csproj
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,58 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
<RootNamespace>Color_Chan.Discord.Caching</RootNamespace> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0"/> | ||
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="5.0.1"/> | ||
<PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0"/> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<Title>Color-Chan.Discord.Chaching</Title> | ||
<Authors>BrammyS</Authors> | ||
<Copyright>BrammyS 2021</Copyright> | ||
<Description>A Discord library made for slash commands. Using Discord webhooks and .NET 5.</Description> | ||
|
||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance> | ||
|
||
<IncludeSymbols>true</IncludeSymbols> | ||
<IncludeSource>true</IncludeSource> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<EmbedUntrackedSources>true</EmbedUntrackedSources> | ||
<EmbedAllSources>true</EmbedAllSources> | ||
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild> | ||
<SymbolPackageFormat>snupkg</SymbolPackageFormat> | ||
|
||
<RepositoryType>git</RepositoryType> | ||
<PackageProjectUrl>https://www.nuget.org/packages/Color-Chan.Discord.Chaching</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/Color-Chan/Color-Chan.Discord</RepositoryUrl> | ||
<RepositoryBranch>main</RepositoryBranch> | ||
<RepositoryUrl>https://github.com/Color-Chan/Color-Chan.Discord</RepositoryUrl> | ||
<PackageTags>slashcommands;discord;library;api</PackageTags> | ||
<PackageIconUrl>https://cdn.colorchan.com/pfp/pfp3/Color-Chan03_256x.png</PackageIconUrl> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> | ||
<DocumentationFile>bin\Debug\Color-Chan.Discord.Chaching.xml</DocumentationFile> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<DebugSymbols>false</DebugSymbols> | ||
<DocumentationFile>bin\Release\Color-Chan.Discord.Chaching.xml</DocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<SourceRoot Include="$(MSBuildThisFileDirectory)/"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Color-Chan.Discord.Core\Color-Chan.Discord.Core.csproj"/> | ||
</ItemGroup> | ||
|
||
</Project> |
21 changes: 21 additions & 0 deletions
21
src/Color-Chan.Discord.Caching/Configurations/CacheConfiguration.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,21 @@ | ||
using System; | ||
|
||
namespace Color_Chan.Discord.Caching.Configurations | ||
{ | ||
/// <summary> | ||
/// Holds the configurations for a cached value. | ||
/// </summary> | ||
public class CacheConfiguration | ||
{ | ||
/// <summary> | ||
/// Gets or sets an absolute expiration time, relative to now. | ||
/// </summary> | ||
public TimeSpan AbsoluteExpiration { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets how long a cache entry can be inactive (e.g. not accessed) before it will be removed. | ||
/// This will not extend the entry lifetime beyond the absolute expiration (if set). | ||
/// </summary> | ||
public TimeSpan SlidingExpiration { get; set; } | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/Color-Chan.Discord.Caching/Extensions/ServiceCollectionExtensions.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,59 @@ | ||
using System; | ||
using Color_Chan.Discord.Caching.Configurations; | ||
using Color_Chan.Discord.Caching.Services; | ||
using Color_Chan.Discord.Caching.Services.Implementations; | ||
using Color_Chan.Discord.Core.Extensions; | ||
using Microsoft.Extensions.Caching.StackExchangeRedis; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Color_Chan.Discord.Caching.Extensions | ||
{ | ||
/// <summary> | ||
/// Contains all the extension methods for <see cref="IServiceCollection" />. | ||
/// </summary> | ||
public static class ServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Add the dependencies for Color-Chan.Discord.Caching to the <see cref="IServiceCollection" />. | ||
/// </summary> | ||
/// <param name="services">The <see cref="IServiceCollection" />.</param> | ||
/// <param name="defaultCacheConfig"> | ||
/// The default cache configurations. | ||
/// Leave this null to use the default expiration values. | ||
/// </param> | ||
/// <param name="redisCacheOptions"> | ||
/// The cache options for the redis cache. | ||
/// Leave this null if you want to use a local cache. | ||
/// </param> | ||
/// <returns> | ||
/// The updated <see cref="IServiceCollection" />. | ||
/// </returns> | ||
public static IServiceCollection AddColorChanCache(this IServiceCollection services, Action<CacheConfiguration>? defaultCacheConfig = null, Action<RedisCacheOptions>? redisCacheOptions = null) | ||
{ | ||
// Set the default config if none provided. | ||
defaultCacheConfig ??= configuration => | ||
{ | ||
configuration.AbsoluteExpiration = TimeSpan.FromSeconds(30); | ||
configuration.SlidingExpiration = TimeSpan.FromSeconds(15); | ||
}; | ||
|
||
services.Configure(defaultCacheConfig); | ||
|
||
if (redisCacheOptions is not null) | ||
{ | ||
services.AddStackExchangeRedisCache(redisCacheOptions); | ||
services.AddSingleton<ICacheService, DistributedCacheService>(); | ||
} | ||
else | ||
{ | ||
services.AddMemoryCache(); | ||
services.AddSingleton<ICacheService, LocalCacheService>(); | ||
} | ||
|
||
services.AddSingleton<ITypeCacheConfigurationService, TypeCacheConfigurationService>(); | ||
services.AddColorChanDiscordCore(); | ||
|
||
return services; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Color-Chan.Discord.Caching/Results/CacheErrorResult.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,18 @@ | ||
using Color_Chan.Discord.Core.Results; | ||
|
||
namespace Color_Chan.Discord.Caching.Results | ||
{ | ||
/// <summary> | ||
/// A cache error result. | ||
/// </summary> | ||
public record CacheErrorResult : ErrorResult | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of <see cref="CacheErrorResult" />. | ||
/// </summary> | ||
/// <param name="key">The key of the cached value.</param> | ||
public CacheErrorResult(string key) : base($"{key} does not exist in the cache") | ||
{ | ||
} | ||
} | ||
} |
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,83 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Color_Chan.Discord.Core.Results; | ||
|
||
namespace Color_Chan.Discord.Caching.Services | ||
{ | ||
/// <summary> | ||
/// Handles all the caching for Color-Chan.Discord. | ||
/// </summary> | ||
public interface ICacheService | ||
{ | ||
/// <summary> | ||
/// Get or creates a cached value. | ||
/// Returns the cached value if it is cached. | ||
/// It will get the value from <paramref name="getValue" /> and cache it if the value is not current cached. | ||
/// </summary> | ||
/// <param name="key">The key of the cached value.</param> | ||
/// <param name="getValue">The task that will be executed if the cached value does not exist.</param> | ||
/// <typeparam name="TValue">The type of the cached value.</typeparam> | ||
/// <returns> | ||
/// Return a <see cref="Result{T}" /> with the <typeparamref name="TValue"/>that was cached. | ||
/// </returns> | ||
Task<Result<TValue>> GetOrCreateValueAsync<TValue>(string key, Func<Task<TValue>> getValue) where TValue : notnull; | ||
|
||
/// <summary> | ||
/// Tries to get a value from the cache. | ||
/// </summary> | ||
/// <param name="key">The key of the cached value.</param> | ||
/// <typeparam name="TValue">The type of the cached value.</typeparam> | ||
/// <returns> | ||
/// Return a <see cref="Result{T}" /> with the <typeparamref name="TValue"/> if the value was cached. | ||
/// </returns> | ||
Task<Result<TValue>> GetValueAsync<TValue>(string key) where TValue : notnull; | ||
|
||
/// <summary> | ||
/// Removes a value from the cache. | ||
/// </summary> | ||
/// <param name="key">The key of the cached value.</param> | ||
Task RemoveValueAsync(string key); | ||
|
||
/// <summary> | ||
/// Caches a value. | ||
/// </summary> | ||
/// <param name="key">The key of the value that will be cached.</param> | ||
/// <param name="cachedValue">The value that will be cached.</param> | ||
/// <typeparam name="TValue">The type of the cached value.</typeparam> | ||
Task CacheValueAsync<TValue>(string key, TValue cachedValue) where TValue : notnull; | ||
|
||
/// <summary> | ||
/// Caches a value. | ||
/// </summary> | ||
/// <param name="key">The key of the value that will be cached.</param> | ||
/// <param name="cachedValue">The value that will be cached.</param> | ||
/// <param name="slidingExpirationOverwrite"> | ||
/// How long a cache entry can be inactive (e.g. not accessed) before it will be | ||
/// removed. | ||
/// </param> | ||
/// <param name="absoluteExpirationOverwrite">The absolute expiration time, relative to now.</param> | ||
/// <typeparam name="TValue">The type of the cached value.</typeparam> | ||
Task CacheValueAsync<TValue>(string key, TValue cachedValue, TimeSpan slidingExpirationOverwrite, TimeSpan absoluteExpirationOverwrite) where TValue : notnull; | ||
|
||
/// <summary> | ||
/// Caches a value. | ||
/// </summary> | ||
/// <param name="key">The key of the value that will be cached.</param> | ||
/// <param name="cachedValue">The value that will be cached.</param> | ||
/// <typeparam name="TValue">The type of the cached value.</typeparam> | ||
void CacheValue<TValue>(string key, TValue cachedValue) where TValue : notnull; | ||
|
||
/// <summary> | ||
/// Caches a value. | ||
/// </summary> | ||
/// <param name="key">The key of the value that will be cached.</param> | ||
/// <param name="cachedValue">The value that will be cached.</param> | ||
/// <param name="slidingExpirationOverwrite"> | ||
/// How long a cache entry can be inactive (e.g. not accessed) before it will be | ||
/// removed. | ||
/// </param> | ||
/// <param name="absoluteExpirationOverwrite">The absolute expiration time, relative to now.</param> | ||
/// <typeparam name="TValue">The type of the cached value.</typeparam> | ||
void CacheValue<TValue>(string key, TValue cachedValue, TimeSpan slidingExpirationOverwrite, TimeSpan absoluteExpirationOverwrite) where TValue : notnull; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Color-Chan.Discord.Caching/Services/ITypeCacheConfigurationService.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,33 @@ | ||
using System; | ||
using Color_Chan.Discord.Caching.Configurations; | ||
using Color_Chan.Discord.Caching.Services.Implementations; | ||
|
||
namespace Color_Chan.Discord.Caching.Services | ||
{ | ||
/// <summary> | ||
/// Manages and contains all the <see cref="CacheConfiguration" /> for all kind of value types. | ||
/// </summary> | ||
public interface ITypeCacheConfigurationService | ||
{ | ||
/// <summary> | ||
/// Adds a <see cref="CacheConfiguration" /> for a specific type. | ||
/// </summary> | ||
/// <param name="slidingExpiration">The absolute expiration time, relative to now.</param> | ||
/// <param name="absoluteExpiration">How long a cache entry can be inactive (e.g. not accessed) before it will be removed.</param> | ||
/// <typeparam name="TValueType">The type of the value.</typeparam> | ||
/// <returns> | ||
/// The <see cref="TypeCacheConfigurationService" /> containing the added type cache config. | ||
/// </returns> | ||
TypeCacheConfigurationService AddCacheConfig<TValueType>(TimeSpan slidingExpiration, TimeSpan absoluteExpiration); | ||
|
||
/// <summary> | ||
/// Get the cache config for a specific value type. | ||
/// </summary> | ||
/// <typeparam name="TValueType">The value type.</typeparam> | ||
/// <returns> | ||
/// The <see cref="CacheConfiguration" /> for the specified type if one was found. | ||
/// Return the default <see cref="CacheConfiguration" /> if none were found. | ||
/// </returns> | ||
CacheConfiguration GetCacheConfig<TValueType>(); | ||
} | ||
} |
Oops, something went wrong.