Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 5d77873

Browse files
committed
optimize compression factory
1 parent f9c7ba4 commit 5d77873

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

Titanium.Web.Proxy/Compression/CompressionFactory.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
namespace Titanium.Web.Proxy.Compression
1+
using System;
2+
3+
namespace Titanium.Web.Proxy.Compression
24
{
35
/// <summary>
46
/// A factory to generate the compression methods based on the type of compression
57
/// </summary>
68
internal class CompressionFactory
79
{
8-
public ICompression Create(string type)
10+
//cache
11+
private static Lazy<ICompression> gzip = new Lazy<ICompression>(() => new GZipCompression());
12+
private static Lazy<ICompression> deflate = new Lazy<ICompression>(() => new DeflateCompression());
13+
14+
public static ICompression GetCompression(string type)
915
{
1016
switch (type)
1117
{
1218
case "gzip":
13-
return new GZipCompression();
19+
return gzip.Value;
1420
case "deflate":
15-
return new DeflateCompression();
21+
return deflate.Value;
1622
default:
1723
return null;
1824
}

Titanium.Web.Proxy/ResponseHandler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ private async Task HandleHttpSessionResponse(SessionEventArgs args)
122122
/// <returns></returns>
123123
private async Task<byte[]> GetCompressedResponseBody(string encodingType, byte[] responseBodyStream)
124124
{
125-
var compressionFactory = new CompressionFactory();
126-
var compressor = compressionFactory.Create(encodingType);
125+
var compressor = CompressionFactory.GetCompression(encodingType);
127126
return await compressor.Compress(responseBodyStream);
128127
}
129128
}

0 commit comments

Comments
 (0)