Skip to content
This repository has been archived by the owner on Dec 24, 2022. It is now read-only.

Commit

Permalink
Redis INFO parser was added to RedisDataInfoExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
IvAlex1986 committed May 18, 2016
1 parent 3310e30 commit ba36e0e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/ServiceStack.Redis/RedisDataInfoExtensions.cs
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Script.Serialization;

namespace ServiceStack.Redis
{
public static class RedisDataInfoExtensions
{
public static String ToJsonInfo(this RedisText redisText)
{
var source = redisText.GetResult();
return Parse(source);
}

#region Private

private static String Parse(String source)
{
var result = new Dictionary<String, Dictionary<String, String>>();
var section = new Dictionary<String, String>();

var rows = SplitRows(source);

foreach (var row in rows)
{
if (row.IndexOf("#", StringComparison.Ordinal) == 0)
{
var name = ParseSection(row);
section = new Dictionary<String, String>();
result.Add(name, section);
}
else
{
var pair = ParseKeyValue(row);
if (pair.HasValue)
{
section.Add(pair.Value.Key, pair.Value.Value);
}
}
}

return new JavaScriptSerializer().Serialize(result);
}

private static IEnumerable<String> SplitRows(String source)
{
return source.Split(new[] { "\r\n" }, StringSplitOptions.None).Where(n => !String.IsNullOrWhiteSpace(n));
}

private static String ParseSection(String source)
{
return (source.IndexOf("#", StringComparison.Ordinal) == 0)
? source.Trim('#').Trim()
: String.Empty;
}

private static KeyValuePair<String, String>? ParseKeyValue(String source)
{
KeyValuePair<String, String>? result = null;

var devider = source.IndexOf(":", StringComparison.Ordinal);
if (devider > 0)
{
var name = source.Substring(0, devider);
var value = source.Substring(devider + 1);
result = new KeyValuePair<String, String>(name.Trim(), value.Trim());
}

return result;
}

#endregion Private
}
}
2 changes: 2 additions & 0 deletions src/ServiceStack.Redis/ServiceStack.Redis.csproj
Expand Up @@ -135,6 +135,7 @@
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Net" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
Expand All @@ -148,6 +149,7 @@
<Compile Include="BasicRedisClientManager.cs" />
<Compile Include="BasicRedisClientManager.ICacheClient.cs" />
<Compile Include="BasicRedisResolver.cs" />
<Compile Include="RedisDataInfoExtensions.cs" />
<Compile Include="RedisResolver.cs" />
<Compile Include="BufferPool.cs" />
<Compile Include="Commands.cs" />
Expand Down

0 comments on commit ba36e0e

Please sign in to comment.