-
Notifications
You must be signed in to change notification settings - Fork 8
Add IIconCacheProvider abstraction with disk and in-memory implementa… #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
degborta
wants to merge
5
commits into
master
Choose a base branch
from
feature-inmemorycache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
245a768
Add IIconCacheProvider abstraction with disk and in-memory implementa…
degborta 7e5a2b6
Fixes
degborta 0c791c8
Update src/Geta.Optimizely.ContentTypeIcons/Caching/IIconCacheProvide…
degborta 8b14da7
Register IIconCacheProvider as singleton so the initialized instance…
degborta 952c0d5
Replaced default disk cache provider with inmemoryiconcacheprovider t…
degborta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Binary file modified
BIN
+636 KB
(130%)
.../modules/_protected/Geta.Optimizely.ContentTypeIcons/Geta.Optimizely.ContentTypeIcons.zip
Binary file not shown.
57 changes: 57 additions & 0 deletions
57
src/Geta.Optimizely.ContentTypeIcons/Caching/DiskIconCacheProvider.cs
This file contains hidden or 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,57 @@ | ||
| using System.IO; | ||
| using EPiServer.Framework.Internal; | ||
| using Geta.Optimizely.ContentTypeIcons.Infrastructure.Configuration; | ||
| using Microsoft.Extensions.Options; | ||
| using SixLabors.ImageSharp; | ||
| using SixLabors.ImageSharp.Formats.Png; | ||
|
|
||
| namespace Geta.Optimizely.ContentTypeIcons.Caching | ||
| { | ||
| public class DiskIconCacheProvider : IIconCacheProvider | ||
| { | ||
| private readonly IPhysicalPathResolver _physicalPathResolver; | ||
| private readonly ContentTypeIconOptions _options; | ||
|
|
||
| public DiskIconCacheProvider( | ||
| IOptions<ContentTypeIconOptions> options, | ||
| IPhysicalPathResolver physicalPathResolver) | ||
| { | ||
| _physicalPathResolver = physicalPathResolver; | ||
| _options = options.Value; | ||
| } | ||
|
|
||
| public void Initialize() | ||
| { | ||
| var fullPath = _physicalPathResolver.Rebase(_options.CachePath); | ||
| if (!Directory.Exists(fullPath)) | ||
| { | ||
| Directory.CreateDirectory(fullPath); | ||
| } | ||
| } | ||
|
|
||
| public bool TryGet(string key, out Image image) | ||
| { | ||
| var fullPath = GetFullPath(key); | ||
| if (File.Exists(fullPath)) | ||
| { | ||
| image = Image.Load(fullPath); | ||
| return true; | ||
| } | ||
|
|
||
| image = null; | ||
| return false; | ||
| } | ||
|
|
||
| public void Set(string key, Image image) | ||
| { | ||
| var fullPath = GetFullPath(key); | ||
| using var fileStream = File.Create(fullPath); | ||
| image.Save(fileStream, new PngEncoder()); | ||
| } | ||
|
|
||
| private string GetFullPath(string key) | ||
| { | ||
| return _physicalPathResolver.Rebase(_options.CachePath + key); | ||
| } | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/Geta.Optimizely.ContentTypeIcons/Caching/IIconCacheProvider.cs
This file contains hidden or 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,40 @@ | ||
| using SixLabors.ImageSharp; | ||
|
|
||
| namespace Geta.Optimizely.ContentTypeIcons.Caching | ||
| { | ||
| public interface IIconCacheProvider | ||
| { | ||
| /// <summary> | ||
| /// Called once at application startup to allow the provider to perform any required initialization. | ||
| /// </summary> | ||
| void Initialize(); | ||
|
|
||
| /// <summary> | ||
| /// Tries to retrieve a cached icon image by key. | ||
| /// </summary> | ||
| /// <param name="key">The cache key for the icon image.</param> | ||
| /// <param name="image"> | ||
| /// When this method returns <c>true</c>, contains a newly created <see cref="Image"/> instance | ||
| /// representing the cached icon. Callers own the returned image and are responsible for disposing it. | ||
| /// Implementations must not return a shared or reused <see cref="Image"/> instance that may be | ||
| /// disposed or mutated elsewhere. | ||
| /// </param> | ||
| /// <returns> | ||
| /// <c>true</c> if an icon image was found for the specified key; otherwise, <c>false</c>. When this | ||
| /// method returns <c>false</c>, <paramref name="image"/> must be set to <c>null</c>. | ||
| /// </returns> | ||
| bool TryGet(string key, out Image image); | ||
|
|
||
| /// <summary> | ||
| /// Stores an icon image in the cache under the given key. | ||
| /// </summary> | ||
| /// <param name="key">The cache key under which to store the icon image.</param> | ||
| /// <param name="image"> | ||
| /// The icon image to cache. The caller will dispose this <see cref="Image"/> instance immediately | ||
| /// after <see cref="Set"/> returns, so implementations must not store or otherwise retain a reference | ||
| /// to this instance. Providers that need to keep the image must clone, serialize, or otherwise create | ||
| /// their own representation for long-term storage. | ||
| /// </param> | ||
| void Set(string key, Image image); | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
src/Geta.Optimizely.ContentTypeIcons/Caching/InMemoryIconCacheProvider.cs
This file contains hidden or 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,48 @@ | ||
| using System.IO; | ||
| using Geta.Optimizely.ContentTypeIcons.Infrastructure.Configuration; | ||
| using Microsoft.Extensions.Caching.Memory; | ||
| using Microsoft.Extensions.Options; | ||
| using SixLabors.ImageSharp; | ||
| using SixLabors.ImageSharp.Formats.Png; | ||
|
|
||
| namespace Geta.Optimizely.ContentTypeIcons.Caching | ||
| { | ||
| public class InMemoryIconCacheProvider : IIconCacheProvider | ||
| { | ||
| private const string CacheKeyPrefix = "geta.contenttypeicons.image."; | ||
|
|
||
| private readonly IMemoryCache _cache; | ||
| private readonly ContentTypeIconOptions _options; | ||
|
|
||
| public InMemoryIconCacheProvider(IMemoryCache cache, IOptions<ContentTypeIconOptions> options) | ||
| { | ||
| _cache = cache; | ||
| _options = options.Value; | ||
| } | ||
|
|
||
| public void Initialize() { } | ||
|
|
||
| public bool TryGet(string key, out Image image) | ||
| { | ||
| if (_cache.TryGetValue(CacheKeyPrefix + key, out byte[] bytes)) | ||
| { | ||
| image = Image.Load(bytes); | ||
| return true; | ||
| } | ||
|
|
||
| image = null; | ||
| return false; | ||
| } | ||
|
|
||
| public void Set(string key, Image image) | ||
| { | ||
| using var ms = new MemoryStream(); | ||
| image.Save(ms, new PngEncoder()); | ||
| var entryOptions = new MemoryCacheEntryOptions | ||
| { | ||
| SlidingExpiration = _options.InMemoryCacheSlidingExpiration | ||
| }; | ||
| _cache.Set(CacheKeyPrefix + key, ms.ToArray(), entryOptions); | ||
| } | ||
|
degborta marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.