Skip to content

Commit d1d82e7

Browse files
committed
New Methods added:
>> API.Utility.GZipCompress to compress a string with GZip >> API.Utility.GZipCompress to decompress a string with GZip API.MemCacheD enhanced to compress/decompress cached data
1 parent 437b319 commit d1d82e7

File tree

11 files changed

+76
-23
lines changed

11 files changed

+76
-23
lines changed

rls/API.Library.dll

1 KB
Binary file not shown.

rls/API.Library.pdb

2 KB
Binary file not shown.

src/API.Library/Entities/MemCacheD.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
using System;
2-
using System.Configuration;
1+
using Enyim.Caching;
2+
using Enyim.Caching.Memcached;
3+
using System;
34
using System.Collections.Generic;
5+
using System.Configuration;
46
using System.Dynamic;
5-
using Newtonsoft.Json;
6-
using Enyim.Caching;
7-
using Enyim.Caching.Memcached;
87

98
namespace API
109
{
@@ -333,13 +332,18 @@ private static bool Store(string key, MemCachedD_Value value, TimeSpan validFor,
333332
try
334333
{
335334
// The value must be serialised
336-
string serializedValue = Utility.JsonSerialize_IgnoreLoopingReference(value);
335+
string cacheSerialised = Utility.JsonSerialize_IgnoreLoopingReference(value);
336+
Log.Instance.Info("Cache Size Serialised (Byte): " + cacheSerialised.Length * sizeof(Char));
337+
338+
// The value must be compressed
339+
string cacheCompressed = Utility.GZipCompress(cacheSerialised);
340+
Log.Instance.Info("Cache Size Compressed (Byte): " + cacheCompressed.Length * sizeof(Char));
337341

338342
bool isStored = false;
339343

340344
// Fix the MemCacheD issue with bad Windows' compiled version: use validFor instead of expiresAt
341345
// validFor and expiresAt match each other
342-
isStored = MemcachedClient.Store(StoreMode.Set, key, serializedValue, validFor);
346+
isStored = MemcachedClient.Store(StoreMode.Set, key, cacheCompressed, validFor);
343347

344348
// Store Value by Key
345349
if (isStored)
@@ -550,12 +554,18 @@ private static MemCachedD_Value Get(string key)
550554

551555
try
552556
{
553-
// Get serialised cache by Key
554-
string cacheSerialised = MemcachedClient.Get<string>(key);
557+
// Get the compressed cache by Key
558+
string cacheCompressed = MemcachedClient.Get<string>(key);
555559

556-
if (!String.IsNullOrEmpty(cacheSerialised))
560+
if (!String.IsNullOrEmpty(cacheCompressed))
557561
{
558562
Log.Instance.Info("Cache found: " + key);
563+
Log.Instance.Info("Cache Size Compressed (Byte): " + cacheCompressed.Length * sizeof(Char));
564+
565+
// Decompress the cache
566+
string cacheSerialised = Utility.GZipDecompress(cacheCompressed);
567+
Log.Instance.Info("Cache Size Serialised (Byte): " + cacheSerialised.Length * sizeof(Char));
568+
559569
// The value must be deserialised
560570
dynamic cache = Utility.JsonDeserialize_IgnoreLoopingReference(cacheSerialised);
561571

@@ -829,4 +839,4 @@ public MemCachedD_Value()
829839
data = null;
830840
}
831841
}
832-
}
842+
}

src/API.Library/Entities/Utility.cs

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
using System.Reflection;
2-
using log4net;
3-
using System.Text;
4-
using System.Security.Cryptography;
1+
using log4net;
2+
using Newtonsoft.Json;
53
using System;
4+
using System.Collections.Specialized;
65
using System.Configuration;
7-
using System.Net;
6+
using System.IO;
7+
using System.IO.Compression;
88
using System.Linq;
9+
using System.Net;
910
using System.Net.Sockets;
10-
using Newtonsoft.Json;
11-
using System.Collections.Specialized;
11+
using System.Reflection;
12+
using System.Security.Cryptography;
13+
using System.Text;
1214
using System.Web;
1315

1416
namespace API
@@ -230,6 +232,47 @@ public static string DecodeBase64ToUTF8(string data)
230232
}
231233
}
232234

235+
/// <summary>
236+
/// Compress a UTF8 input string into Base64
237+
/// </summary>
238+
/// <param name="inputUTF8"></param>
239+
/// <returns></returns>
240+
public static string GZipCompress(string inputUTF8)
241+
{
242+
var byteInput = Encoding.UTF8.GetBytes(inputUTF8);
243+
244+
using (var msInput = new MemoryStream(byteInput))
245+
using (var msOutput = new MemoryStream())
246+
{
247+
using (var stream = new GZipStream(msOutput, CompressionMode.Compress))
248+
{
249+
msInput.CopyTo(stream);
250+
}
251+
252+
return Convert.ToBase64String(msOutput.ToArray());
253+
}
254+
}
255+
256+
/// <summary>
257+
/// Decompress a Base64 input string into UTF8
258+
/// </summary>
259+
/// <param name="inputBase64"></param>
260+
/// <returns></returns>
261+
public static string GZipDecompress(string inputBase64)
262+
{
263+
byte[] byteInput = Convert.FromBase64String(inputBase64);
264+
265+
using (var msInput = new MemoryStream(byteInput))
266+
using (var msOutput = new MemoryStream())
267+
{
268+
using (var stream = new GZipStream(msInput, CompressionMode.Decompress))
269+
{
270+
stream.CopyTo(msOutput);
271+
}
272+
273+
return Encoding.UTF8.GetString(msOutput.ToArray());
274+
}
275+
}
233276
#endregion
234277
}
235-
}
278+
}

src/API.Library/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("3.0.4.0")]
36-
[assembly: AssemblyFileVersion("3.0.4.0")]
35+
[assembly: AssemblyVersion("3.0.5.0")]
36+
[assembly: AssemblyFileVersion("3.0.5.0")]
3737

3838
// Configure log4net using the Web.config file by default
3939
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

test/API.Test/API.Test.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@
7171
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
7272
</PropertyGroup>
7373
<ItemGroup>
74-
<Reference Include="API.Library, Version=3.0.4.0, Culture=neutral, processorArchitecture=MSIL">
74+
<Reference Include="API.Library, Version=3.0.5.0, Culture=neutral, processorArchitecture=MSIL">
7575
<SpecificVersion>False</SpecificVersion>
76-
<HintPath>..\packages\API.Library.3.0.4\API.Library.dll</HintPath>
76+
<HintPath>..\packages\API.Library.3.0.5\API.Library.dll</HintPath>
7777
</Reference>
7878
<Reference Include="Enyim.Caching, Version=2.16.0.0, Culture=neutral, PublicKeyToken=cec98615db04012e, processorArchitecture=MSIL">
7979
<HintPath>..\packages\EnyimMemcached.2.16.0\lib\net35\Enyim.Caching.dll</HintPath>
-60.5 KB
Binary file not shown.
-85.5 KB
Binary file not shown.
61.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)