Skip to content

Commit

Permalink
Cache descriptor set file for 10 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueMatthew committed Apr 3, 2021
1 parent 1fb2db8 commit e686d3c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions ProtobufInspector.sln
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ Global
{6C695AF2-50D7-498C-A1BE-A4268C689D8F}.Release|x86.Build.0 = Release|Any CPU
{5DBDFAF4-0D15-4386-8E42-CB647D143CF2}.Debug|Any CPU.ActiveCfg = Debug|Win32
{5DBDFAF4-0D15-4386-8E42-CB647D143CF2}.Debug|x64.ActiveCfg = Debug|x64
{5DBDFAF4-0D15-4386-8E42-CB647D143CF2}.Debug|x64.Build.0 = Debug|x64
{5DBDFAF4-0D15-4386-8E42-CB647D143CF2}.Debug|x86.ActiveCfg = Debug|Win32
{5DBDFAF4-0D15-4386-8E42-CB647D143CF2}.Debug|x86.Build.0 = Debug|Win32
{5DBDFAF4-0D15-4386-8E42-CB647D143CF2}.Release|Any CPU.ActiveCfg = Release|Win32
{5DBDFAF4-0D15-4386-8E42-CB647D143CF2}.Release|x64.ActiveCfg = Release|x64
{5DBDFAF4-0D15-4386-8E42-CB647D143CF2}.Release|x64.Build.0 = Release|x64
{5DBDFAF4-0D15-4386-8E42-CB647D143CF2}.Release|x86.ActiveCfg = Release|Win32
{5DBDFAF4-0D15-4386-8E42-CB647D143CF2}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
Expand Down
23 changes: 22 additions & 1 deletion ProtobufInspector/Protobuf2Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Runtime.Serialization;
using System.Xml;
using System.Net;
using System.Net.Cache;
using Fiddler;

namespace Google.Protobuf.FiddlerInspector
Expand Down Expand Up @@ -56,6 +57,13 @@ private static extern int FreeOutputString(
[MarshalAs(UnmanagedType.U4)] UInt32 lengthOfOutputString
);

[DllImport("Protobuf2Json.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern int CacheDescriptorSet(
[MarshalAs(UnmanagedType.LPStr)] String udescriptorSetUrlrl,
[MarshalAs(UnmanagedType.LPStr)] String descriptorSetFilePath,
[MarshalAs(UnmanagedType.LPStr)] UInt32 expireSeconds
);

public static string convertAndFreeStrPtr(IntPtr outputStringPtr, UInt32 outputStringLength)
{
string outputString = "";
Expand Down Expand Up @@ -205,8 +213,11 @@ protected static bool DownloadDescriptorSetFile(string descriptorSetUrl, out str
FileStream fs = null;
try
{
HttpRequestCachePolicy requestCachePolicy = new HttpRequestCachePolicy(HttpCacheAgeControl.MaxAge, TimeSpan.FromSeconds(600));

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(descriptorSetUrl);
request.Headers.Add("Accept-Encoding", "gzip,deflate");
request.CachePolicy = requestCachePolicy;
request.AllowAutoRedirect = true;
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.Credentials = CredentialCache.DefaultCredentials;
Expand All @@ -221,7 +232,15 @@ protected static bool DownloadDescriptorSetFile(string descriptorSetUrl, out str

fs = File.OpenWrite(descriptorSetFileName);
stm.CopyTo(fs);


bool isFromCache = response.IsFromCache;

if (!response.IsFromCache)
{
// Save to cache
CacheDescriptorSet(descriptorSetUrl, descriptorSetFileName, 600);

}
ret = true;
}
}
Expand Down Expand Up @@ -250,6 +269,8 @@ protected static bool DownloadDescriptorSetFile(string descriptorSetUrl, out str

return ret;
}



}
}

0 comments on commit e686d3c

Please sign in to comment.