Skip to content

Commit

Permalink
Added optional animated emoji pre-load.
Browse files Browse the repository at this point in the history
  • Loading branch information
NTDLS committed Aug 30, 2024
1 parent eb15f36 commit e82ede7
Show file tree
Hide file tree
Showing 19 changed files with 95 additions and 20 deletions.
7 changes: 7 additions & 0 deletions TightWiki.Caching/IWikiCacheKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TightWiki.Caching
{
public interface IWikiCacheKey
{
public string Key { get; set; }
}
}
2 changes: 1 addition & 1 deletion TightWiki.Caching/TightWiki.Caching.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>2.17.2</Version>
<Version>2.17.3</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
14 changes: 10 additions & 4 deletions TightWiki.Caching/WikiCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static void Initialize(int cacheMemoryLimitMB, int defaultCacheSeconds)
/// <typeparam name="T"></typeparam>
/// <param name="cacheKey"></param>
/// <returns></returns>
public static T? Get<T>(WikiCacheKeyFunction cacheKey)
public static T? Get<T>(IWikiCacheKey cacheKey)
{
CacheGets++;
var result = (T)MemCache.Get(cacheKey.Key);
Expand All @@ -65,7 +65,7 @@ public static void Initialize(int cacheMemoryLimitMB, int defaultCacheSeconds)
/// <typeparam name="T"></typeparam>
/// <param name="cacheKey"></param>
/// <returns></returns>
public static bool Contains(WikiCacheKeyFunction cacheKey)
public static bool Contains(IWikiCacheKey cacheKey)
{
CacheGets++;
if (MemCache.Contains(cacheKey.Key))
Expand All @@ -84,7 +84,7 @@ public static bool Contains(WikiCacheKeyFunction cacheKey)
/// <typeparam name="T"></typeparam>
/// <param name="cacheKey"></param>
/// <returns></returns>
public static bool TryGet<T>(WikiCacheKeyFunction cacheKey, [NotNullWhen(true)] out T result)
public static bool TryGet<T>(IWikiCacheKey cacheKey, [NotNullWhen(true)] out T result)
{
CacheGets++;
if ((result = (T)MemCache.Get(cacheKey.Key)) == null)
Expand All @@ -103,7 +103,7 @@ public static bool TryGet<T>(WikiCacheKeyFunction cacheKey, [NotNullWhen(true)]
/// <param name="cacheKey"></param>
/// <param name="value"></param>
/// <param name="seconds"></param>
public static void Put(WikiCacheKeyFunction cacheKey, object value, int? seconds = null)
public static void Put(IWikiCacheKey cacheKey, object value, int? seconds = null)
{
CachePuts++;

Expand All @@ -120,6 +120,12 @@ public static void Put(WikiCacheKeyFunction cacheKey, object value, int? seconds
MemCache.Add(cacheKey.Key, value, policy);
}

public static void Put(IWikiCacheKey cacheKey, object value, CacheItemPolicy policy)
{
CachePuts++;
MemCache.Add(cacheKey.Key, value, policy);
}

/// <summary>
/// Removes all entries from the cache.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion TightWiki.Caching/WikiCacheKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace TightWiki.Caching
/// Contains a verbatim cache key.
/// </summary>
/// <param name="key"></param>
public class WikiCacheKey(string key)
public class WikiCacheKey(string key) : IWikiCacheKey
{
public string Key { get; set; } = key;

Expand Down
2 changes: 1 addition & 1 deletion TightWiki.Caching/WikiCacheKeyFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace TightWiki.Caching
/// Contains a verbatim cache key which also includes the calling function name.
/// </summary>
/// <param name="key"></param>
public class WikiCacheKeyFunction(string key)
public class WikiCacheKeyFunction(string key) : IWikiCacheKey
{
public string Key { get; set; } = key;

Expand Down
2 changes: 1 addition & 1 deletion TightWiki.Email/TightWiki.Email.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>2.17.2</Version>
<Version>2.17.3</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
2 changes: 1 addition & 1 deletion TightWiki.Engine.Function/TightWiki.Engine.Function.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>2.17.2</Version>
<Version>2.17.3</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>2.17.2</Version>
<Version>2.17.3</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
2 changes: 1 addition & 1 deletion TightWiki.Engine.Library/TightWiki.Engine.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>2.17.2</Version>
<Version>2.17.3</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
2 changes: 1 addition & 1 deletion TightWiki.Engine/TightWiki.Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>2.17.2</Version>
<Version>2.17.3</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
2 changes: 1 addition & 1 deletion TightWiki.Library/TightWiki.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>2.17.2</Version>
<Version>2.17.3</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions TightWiki.Models/GlobalConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static class GlobalConfiguration
public static int PageCacheSeconds { get; set; }
public static int CacheMemoryLimitMB { get; set; }
public static int DefaultProfileRecentlyModifiedCount { get; set; }
public static bool PreLoadAnimatedEmojis { get; set; } = true;
public static bool RecordCompilationMetrics { get; set; }
public static bool ShowLastModifiedOnPageFooter { get; set; }
public static string DefaultTimeZone { get; set; } = string.Empty;
Expand Down
2 changes: 1 addition & 1 deletion TightWiki.Models/TightWiki.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>2.17.2</Version>
<Version>2.17.3</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
58 changes: 58 additions & 0 deletions TightWiki.Repository/ConfigurationRepository.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using NTDLS.Helpers;
using SixLabors.ImageSharp;
using System.Data;
using System.Diagnostics;
using System.Runtime.Caching;
using TightWiki.Caching;
using TightWiki.Library;
using TightWiki.Models;
using TightWiki.Models.DataModels;
using static System.Formats.Asn1.AsnWriter;

namespace TightWiki.Repository
{
Expand Down Expand Up @@ -321,6 +324,60 @@ public static void ReloadEmojis()
{
WikiCache.ClearCategory(WikiCache.Category.Emoji);
GlobalConfiguration.Emojis = EmojiRepository.GetAllEmojis();

if (GlobalConfiguration.PreLoadAnimatedEmojis)
{
new Thread(() =>
{
var parallelOptions = new ParallelOptions
{
MaxDegreeOfParallelism = Environment.ProcessorCount / 2 < 2 ? 2 : Environment.ProcessorCount / 2
};

Parallel.ForEach(GlobalConfiguration.Emojis, parallelOptions, emoji =>
{
if (emoji.MimeType.Equals("image/gif", StringComparison.InvariantCultureIgnoreCase))
{
var imageCacheKey = WikiCacheKey.Build(WikiCache.Category.Emoji, [emoji.Shortcut]);
emoji.ImageData = EmojiRepository.GetEmojiByName(emoji.Name)?.ImageData;

if (emoji.ImageData != null)
{
var scaledImageCacheKey = WikiCacheKey.Build(WikiCache.Category.Emoji, [emoji.Shortcut, "100"]);
var decompressedImageBytes = Utility.Decompress(emoji.ImageData);
var img = Image.Load(new MemoryStream(decompressedImageBytes));

int customScalePercent = 100;

var (Width, Height) = Utility.ScaleToMaxOf(img.Width, img.Height, GlobalConfiguration.DefaultEmojiHeight);

//Adjust to any specified scaling.
Height = (int)(Height * (customScalePercent / 100.0));
Width = (int)(Width * (customScalePercent / 100.0));

//Adjusting by a ratio (and especially after applying additional scaling) may have caused one
// dimension to become very small (or even negative). So here we will check the height and width
// to ensure they are both at least n pixels and adjust both dimensions.
if (Height < 16)
{
Height += 16 - Height;
Width += 16 - Height;
}
if (Width < 16)
{
Height += 16 - Width;
Width += 16 - Width;
}

//These are hard to generate, so just keep it forever.
var resized = Images.ResizeGifImage(decompressedImageBytes, Width, Height);
var itemCache = new ImageCacheItem(resized, "image/gif");
WikiCache.Put(scaledImageCacheKey, itemCache, new CacheItemPolicy());
}
}
});
}).Start();
}
}

public static void ReloadEverything()
Expand Down Expand Up @@ -353,6 +410,7 @@ public static void ReloadEverything()
GlobalConfiguration.FixedMenuPosition = customizationConfig.Value("Fixed Header Menu Position", false);
GlobalConfiguration.AllowSignup = membershipConfig.Value("Allow Signup", false);
GlobalConfiguration.DefaultProfileRecentlyModifiedCount = performanceConfig.Value<int>("Default Profile Recently Modified Count");
GlobalConfiguration.PreLoadAnimatedEmojis = performanceConfig.Value<bool>("Pre-Load Animated Emojis");
GlobalConfiguration.SystemTheme = GetAllThemes().Single(o => o.Name == themeName);
GlobalConfiguration.DefaultEmojiHeight = customizationConfig.Value<int>("Default Emoji Height");
GlobalConfiguration.AllowGoogleAuthentication = membershipConfig.Value<bool>("Allow Google Authentication");
Expand Down
2 changes: 1 addition & 1 deletion TightWiki.Repository/TightWiki.Repository.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>2.17.2</Version>
<Version>2.17.3</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
2 changes: 1 addition & 1 deletion TightWiki.Security/TightWiki.Security.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>2.17.2</Version>
<Version>2.17.3</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
9 changes: 6 additions & 3 deletions TightWiki/Controllers/FileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Mvc;
using NTDLS.Helpers;
using SixLabors.ImageSharp;
using System.Runtime.Caching;
using System.Web;
using TightWiki.Caching;
using TightWiki.Library;
Expand Down Expand Up @@ -395,13 +396,13 @@ public ActionResult Emoji(string givenPageNavigation)
if (emoji != null)
{
//Do we have this scale cached already?
var scaledImageCacheKey = WikiCacheKeyFunction.Build(WikiCache.Category.Emoji, [shortcut, scale]);
var scaledImageCacheKey = WikiCacheKey.Build(WikiCache.Category.Emoji, [shortcut, scale]);
if (WikiCache.TryGet<ImageCacheItem>(scaledImageCacheKey, out var cachedEmoji))
{
return File(cachedEmoji.Bytes, cachedEmoji.ContentType);
}

var imageCacheKey = WikiCacheKeyFunction.Build(WikiCache.Category.Emoji, [shortcut]);
var imageCacheKey = WikiCacheKey.Build(WikiCache.Category.Emoji, [shortcut]);
emoji.ImageData = WikiCache.Get<byte[]>(imageCacheKey);
if (emoji.ImageData == null)
{
Expand Down Expand Up @@ -453,7 +454,9 @@ public ActionResult Emoji(string givenPageNavigation)
var resized = ResizeGifImage(decompressedImageBytes, Width, Height);

var itemCache = new ImageCacheItem(resized, "image/gif");
WikiCache.Put(scaledImageCacheKey, itemCache);

//These are hard to generate, so just keep it forever.
WikiCache.Put(scaledImageCacheKey, itemCache, new CacheItemPolicy());

return File(itemCache.Bytes, itemCache.ContentType);
}
Expand Down
Binary file modified TightWiki/Data/config.db
Binary file not shown.
2 changes: 1 addition & 1 deletion TightWiki/TightWiki.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>2.17.2</Version>
<Version>2.17.3</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit e82ede7

Please sign in to comment.