Skip to content

Commit

Permalink
Merge pull request #187 from SixLabors/bp/updatesharedinfra
Browse files Browse the repository at this point in the history
Update Shared infrastructure
  • Loading branch information
JimBobSquarePants committed Oct 10, 2021
2 parents a5b0da1 + cebf1ee commit f73155d
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ indent_style = tab

[*.{cs,csx,cake,vb,vbx}]
# Default Severity for all .NET Code Style rules below
dotnet_analyzer_diagnostic.severity = warning
dotnet_analyzer_diagnostic.category-style.severity = warning

##########################################
# Language Rules
Expand Down
1 change: 0 additions & 1 deletion src/ImageSharp.Web/Caching/CacheHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Security.Cryptography;
using System.Text;
using Microsoft.Extensions.Options;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Web.Middleware;

namespace SixLabors.ImageSharp.Web.Caching
Expand Down
4 changes: 2 additions & 2 deletions src/ImageSharp.Web/Caching/ICacheHash.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Six Labors.
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

namespace SixLabors.ImageSharp.Web.Caching
Expand All @@ -16,4 +16,4 @@ public interface ICacheHash
/// <returns>The <see cref="string"/>.</returns>
string Create(string value, uint length);
}
}
}
4 changes: 2 additions & 2 deletions src/ImageSharp.Web/Commands/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Six Labors.
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System.Collections.Generic;
Expand All @@ -24,4 +24,4 @@ public static TValue GetValueOrDefault<TValue, TKey>(this IDictionary<TKey, TVal
return result;
}
}
}
}
4 changes: 2 additions & 2 deletions src/ImageSharp.Web/Commands/IRequestParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Six Labors.
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System.Collections.Generic;
Expand All @@ -18,4 +18,4 @@ public interface IRequestParser
/// <returns>The <see cref="IDictionary{TKey,TValue}"/>.</returns>
IDictionary<string, string> ParseRequestCommands(HttpContext context);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Six Labors.
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using Microsoft.AspNetCore.Builder;
Expand All @@ -23,4 +23,4 @@ public static IApplicationBuilder UseImageSharp(this IApplicationBuilder app)
return app.UseMiddleware<ImageSharpMiddleware>();
}
}
}
}
9 changes: 3 additions & 6 deletions src/ImageSharp.Web/DependencyInjection/ImageSharpBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Six Labors.
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using Microsoft.Extensions.DependencyInjection;
Expand All @@ -14,12 +14,9 @@ internal class ImageSharpBuilder : IImageSharpBuilder
/// Initializes a new instance of the <see cref="ImageSharpBuilder"/> class.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
public ImageSharpBuilder(IServiceCollection services)
{
this.Services = services;
}
public ImageSharpBuilder(IServiceCollection services) => this.Services = services;

/// <inheritdoc/>
public IServiceCollection Services { get; }
}
}
}
17 changes: 8 additions & 9 deletions src/ImageSharp.Web/Middleware/ImageSharpMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ public class ImageSharpMiddleware
/// The write worker used for limiting identical requests.
/// </summary>
private static readonly ConcurrentDictionary<string, Task<ImageWorkerResult>> WriteWorkers
= new ConcurrentDictionary<string, Task<ImageWorkerResult>>(StringComparer.OrdinalIgnoreCase);
= new(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// The read worker used for limiting identical requests.
/// </summary>
private static readonly ConcurrentDictionary<string, Task<ImageWorkerResult>> ReadWorkers
= new ConcurrentDictionary<string, Task<ImageWorkerResult>>(StringComparer.OrdinalIgnoreCase);
= new(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// Used to temporarily store source metadata reads to reduce the overhead of cache lookups.
/// </summary>
private static readonly ConcurrentTLruCache<string, ImageMetadata> SourceMetadataLru
= new ConcurrentTLruCache<string, ImageMetadata>(1024, TimeSpan.FromMinutes(5));
= new(1024, TimeSpan.FromMinutes(5));

/// <summary>
/// Used to temporarily store cache resolver reads to reduce the overhead of cache lookups.
/// </summary>
private static readonly ConcurrentTLruCache<string, ValueTuple<IImageCacheResolver, ImageCacheMetadata>> CacheResolverLru
= new ConcurrentTLruCache<string, ValueTuple<IImageCacheResolver, ImageCacheMetadata>>(1024, TimeSpan.FromMinutes(5));
private static readonly ConcurrentTLruCache<string, (IImageCacheResolver, ImageCacheMetadata)> CacheResolverLru
= new(1024, TimeSpan.FromMinutes(5));

/// <summary>
/// The function processing the Http request.
Expand Down Expand Up @@ -274,7 +274,7 @@ private async Task ProcessRequestAsync(
ImageWorkerResult readResult = default;
try
{
readResult = await this.IsNewOrUpdatedAsync(sourceImageResolver, imageContext, key);
readResult = await this.IsNewOrUpdatedAsync(sourceImageResolver, key);
}
finally
{
Expand All @@ -288,7 +288,7 @@ private async Task ProcessRequestAsync(
}

// Not cached, or is updated? Let's get it from the image resolver.
var sourceImageMetadata = readResult.SourceImageMetadata;
ImageMetadata sourceImageMetadata = readResult.SourceImageMetadata;

// Enter an asynchronous write worker which prevents multiple writes and delays any reads for the same request.
// This reduces the overheads of unnecessary processing.
Expand Down Expand Up @@ -433,7 +433,6 @@ private ValueTask StreamDisposeAsync(Stream stream)

private async Task<ImageWorkerResult> IsNewOrUpdatedAsync(
IImageResolver sourceImageResolver,
ImageContext imageContext,
string key)
{
// Pause until the write has been completed.
Expand Down Expand Up @@ -514,7 +513,7 @@ private async Task SendResponseAsync(
this.logger.LogImageServed(imageContext.GetDisplayUrl(), key);

// When stream is null we're sending from the cache.
using (var stream = await cacheResolver.OpenReadAsync())
using (Stream stream = await cacheResolver.OpenReadAsync())
{
await imageContext.SendAsync(stream, metadata);
}
Expand Down
5 changes: 2 additions & 3 deletions src/ImageSharp.Web/Providers/IImageProvider.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright (c) Six Labors.
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using SixLabors.ImageSharp.Web.Resolvers;
Expand Down Expand Up @@ -40,4 +39,4 @@ public interface IImageProvider
/// <returns>The <see cref="IImageResolver"/>.</returns>
Task<IImageResolver> GetAsync(HttpContext context);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Security.Cryptography;
using System.Text;
using SixLabors.ImageSharp.Web.Caching;
Expand Down
7 changes: 3 additions & 4 deletions tests/ImageSharp.Web.Tests/Caching/CacheHashTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using SixLabors.ImageSharp.Web.Caching;
using SixLabors.ImageSharp.Web.Middleware;
using Xunit;

using MSOptions = Microsoft.Extensions.Options.Options;

namespace SixLabors.ImageSharp.Web.Tests.Caching
Expand All @@ -17,7 +16,7 @@ public class CacheHashTests
private static readonly ICacheHash CacheHash = new CacheHash(Options);

[Fact]
public void CachHashProducesIdenticalResults()
public void CacheHashProducesIdenticalResults()
{
const string Input = "http://testwebsite.com/image-12345.jpeg?width=400";
string expected = CacheHash.Create(Input, 8);
Expand All @@ -27,7 +26,7 @@ public void CachHashProducesIdenticalResults()
}

[Fact]
public void CachHashProducesDifferentResults()
public void CacheHashProducesDifferentResults()
{
const string Input = "http://testwebsite.com/image-12345.jpeg?width=400";
const string Input2 = "http://testwebsite.com/image-23456.jpeg?width=400";
Expand All @@ -38,7 +37,7 @@ public void CachHashProducesDifferentResults()
}

[Fact]
public void CachHashLengthIsIdentical()
public void CacheHashLengthIsIdentical()
{
const int Length = 12;
const string Input = "http://testwebsite.com/image-12345.jpeg?width=400";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System.IO;
using SixLabors.ImageSharp.Web.Caching;
using Xunit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void WebProcessingExtensions_GetBySupportedCommands()
ResizeWebProcessor.Height
};

var supportedProcessors = WebProcessingExtensions.GetBySupportedCommands(processors, commands).ToArray();
IImageWebProcessor[] supportedProcessors = WebProcessingExtensions.GetBySupportedCommands(processors, commands).ToArray();

Assert.Equal(2, supportedProcessors.Length);
Assert.IsType<ResizeWebProcessor>(supportedProcessors[0]);
Expand All @@ -46,7 +46,7 @@ public void WebProcessingExtensions_GetBySupportedCommands_Empty()

var commands = new List<string>();

var supportedProcessors = WebProcessingExtensions.GetBySupportedCommands(processors, commands).ToArray();
IImageWebProcessor[] supportedProcessors = WebProcessingExtensions.GetBySupportedCommands(processors, commands).ToArray();

Assert.Empty(supportedProcessors);
}
Expand Down

0 comments on commit f73155d

Please sign in to comment.