Skip to content

Commit

Permalink
Merge branch 'dev' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
BrammyS committed Aug 7, 2021
2 parents 8baafd4 + b1cecf3 commit 70270dc
Show file tree
Hide file tree
Showing 117 changed files with 2,689 additions and 420 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ jobs:

- name: Push Color-Chan.Discord.Commands nuget.org
run: dotnet nuget push Color-Chan.Discord.Commands.${VERSION}.nupkg --api-key ${{ secrets.NUGET_TOKEN }} --source https://api.nuget.org/v3/index.json --skip-duplicate

- name: Push Color-Chan.Discord.Caching github
run: dotnet nuget push Color-Chan.Discord.Caching.${VERSION}.nupkg --source https://nuget.pkg.github.com/brammys/index.json --api-key ${{ secrets.PAT }} --skip-duplicate

- name: Push Color-Chan.Discord.Caching nuget.org
run: dotnet nuget push Color-Chan.Discord.Caching.${VERSION}.nupkg --api-key ${{ secrets.NUGET_TOKEN }} --source https://api.nuget.org/v3/index.json --skip-duplicate
14 changes: 14 additions & 0 deletions Color-Chan.Discord.sln
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RoleManager", "samples\Role
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Color-Chan.Discord.Tests", "tests\Color-Chan.Discord.Tests\Color-Chan.Discord.Tests.csproj", "{F2CDB742-F853-404A-BE68-C54BCB080405}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Color-Chan.Discord.Caching", "src\Color-Chan.Discord.Caching\Color-Chan.Discord.Caching.csproj", "{6409B606-0EE1-4462-BDFF-CCE69BBB78ED}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Color-Chan.Discord.Caching.Tests", "tests\Color-Chan.Discord.Caching.Tests\Color-Chan.Discord.Caching.Tests.csproj", "{A0B1D8ED-FA1F-4706-BC82-B98EA47C077D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -103,6 +107,14 @@ Global
{F2CDB742-F853-404A-BE68-C54BCB080405}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F2CDB742-F853-404A-BE68-C54BCB080405}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F2CDB742-F853-404A-BE68-C54BCB080405}.Release|Any CPU.Build.0 = Release|Any CPU
{6409B606-0EE1-4462-BDFF-CCE69BBB78ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6409B606-0EE1-4462-BDFF-CCE69BBB78ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6409B606-0EE1-4462-BDFF-CCE69BBB78ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6409B606-0EE1-4462-BDFF-CCE69BBB78ED}.Release|Any CPU.Build.0 = Release|Any CPU
{A0B1D8ED-FA1F-4706-BC82-B98EA47C077D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A0B1D8ED-FA1F-4706-BC82-B98EA47C077D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0B1D8ED-FA1F-4706-BC82-B98EA47C077D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0B1D8ED-FA1F-4706-BC82-B98EA47C077D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -124,6 +136,8 @@ Global
{D7877668-436C-4017-8282-AC23AF441F36} = {BE3999FC-A454-4BB3-9F5B-FF0A6DBF9A1D}
{697DDCAB-0DDD-486F-B70B-64ECF0C01CDC} = {B2D862F3-8ED1-4D09-8248-8D4B5EB2737A}
{F2CDB742-F853-404A-BE68-C54BCB080405} = {1BA385BC-A756-40AE-9892-D9C172455B76}
{6409B606-0EE1-4462-BDFF-CCE69BBB78ED} = {3355E69A-02D6-49CA-877E-4D9594222CE4}
{A0B1D8ED-FA1F-4706-BC82-B98EA47C077D} = {1BA385BC-A756-40AE-9892-D9C172455B76}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AC84D902-ABC1-47C3-9993-F769F483E5CE}
Expand Down
2 changes: 1 addition & 1 deletion samples/Pong/Commands/PongCommands.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Drawing;
using System.Threading.Tasks;
using Color_Chan.Discord.Builders;
using Color_Chan.Discord.Commands;
using Color_Chan.Discord.Commands.Attributes;
using Color_Chan.Discord.Commands.Modules;
using Color_Chan.Discord.Core.Common.Models.Interaction;
Expand Down
2 changes: 1 addition & 1 deletion samples/Pong/Pong.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Color-Chan.Discord\Color-Chan.Discord.csproj" />
<ProjectReference Include="..\..\src\Color-Chan.Discord\Color-Chan.Discord.csproj"/>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion samples/RoleManager/Commands/RoleCommands.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using Color_Chan.Discord.Builders;
using Color_Chan.Discord.Commands;
using Color_Chan.Discord.Commands.Attributes;
using Color_Chan.Discord.Commands.Modules;
using Color_Chan.Discord.Core.Common.API.DataModels.Application;
Expand Down
2 changes: 1 addition & 1 deletion samples/RoleManager/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"applicationUrl": "https://localhost:5003;http://localhost:5002",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down
2 changes: 1 addition & 1 deletion samples/RoleManager/RoleManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Color-Chan.Discord\Color-Chan.Discord.csproj" />
<ProjectReference Include="..\..\src\Color-Chan.Discord\Color-Chan.Discord.csproj"/>
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions samples/RoleManager/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://localhost:5002"
}
}
},
"Logging": {
"LogLevel": {
"Default": "Warning",
Expand Down
58 changes: 58 additions & 0 deletions src/Color-Chan.Discord.Caching/Color-Chan.Discord.Caching.csproj
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>
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; }
}
}
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 src/Color-Chan.Discord.Caching/Results/CacheErrorResult.cs
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")
{
}
}
}
83 changes: 83 additions & 0 deletions src/Color-Chan.Discord.Caching/Services/ICacheService.cs
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;
}
}
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>();
}
}
Loading

0 comments on commit 70270dc

Please sign in to comment.