forked from ServiceStack/ServiceStack.Redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisExtensions.cs
More file actions
205 lines (181 loc) · 6.75 KB
/
RedisExtensions.cs
File metadata and controls
205 lines (181 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// https://github.com/ServiceStack/ServiceStack.Redis
// ServiceStack.Redis: ECMA CLI Binding to the Redis key-value storage system
//
// Authors:
// Demis Bellot (demis.bellot@gmail.com)
//
// Copyright 2013 Service Stack LLC. All Rights Reserved.
//
// Licensed under the same terms of ServiceStack.
//
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Sockets;
using ServiceStack.Model;
namespace ServiceStack.Redis
{
public static class RedisExtensions
{
public static List<RedisEndpoint> ToRedisEndPoints(this IEnumerable<string> hosts)
{
return hosts == null
? new List<RedisEndpoint>()
: hosts.Select(x => ToRedisEndpoint(x)).ToList();
}
public static RedisEndpoint ToRedisEndpoint(this string connectionString, int? defaultPort = null)
{
if (connectionString == null)
throw new ArgumentNullException("connectionString");
if (connectionString.StartsWith("redis://"))
connectionString = connectionString.Substring("redis://".Length);
var domainParts = connectionString.SplitOnLast('@');
var qsParts = domainParts.Last().SplitOnFirst('?');
var hostParts = qsParts[0].SplitOnLast(':');
var useDefaultPort = true;
var port = defaultPort.GetValueOrDefault(RedisConfig.DefaultPort);
if (hostParts.Length > 1)
{
port = int.Parse(hostParts[1]);
useDefaultPort = false;
}
var endpoint = new RedisEndpoint(hostParts[0], port);
if (domainParts.Length > 1)
{
var authParts = domainParts[0].SplitOnFirst(':');
if (authParts.Length > 1)
endpoint.Client = authParts[0];
endpoint.Password = authParts.Last();
}
if (qsParts.Length > 1)
{
var qsParams = qsParts[1].Split('&');
foreach (var param in qsParams)
{
var entry = param.Split('=');
var value = entry.Length > 1 ? entry[1].UrlDecode() : null;
if (value == null) continue;
var name = entry[0].ToLower();
switch (name)
{
case "db":
endpoint.Db = int.Parse(value);
break;
case "ssl":
endpoint.Ssl = bool.Parse(value);
if (useDefaultPort)
endpoint.Port = RedisConfig.DefaultPortSsl;
break;
case "client":
endpoint.Client = value;
break;
case "password":
endpoint.Password = value;
break;
case "namespaceprefix":
endpoint.NamespacePrefix = value;
break;
case "connecttimeout":
endpoint.ConnectTimeout = int.Parse(value);
break;
case "sendtimeout":
endpoint.SendTimeout = int.Parse(value);
break;
case "receivetimeout":
endpoint.ReceiveTimeout = int.Parse(value);
break;
case "idletimeout":
case "idletimeoutsecs":
endpoint.IdleTimeOutSecs = int.Parse(value);
break;
}
}
}
return endpoint;
}
}
internal static class RedisExtensionsInternal
{
public static bool IsConnected(this Socket socket)
{
try
{
return !(socket.Poll(1, SelectMode.SelectRead) && socket.Available == 0);
}
catch (SocketException)
{
return false;
}
}
public static string[] GetIds(this IHasStringId[] itemsWithId)
{
var ids = new string[itemsWithId.Length];
for (var i = 0; i < itemsWithId.Length; i++)
{
ids[i] = itemsWithId[i].Id;
}
return ids;
}
public static List<string> ToStringList(this byte[][] multiDataList)
{
if (multiDataList == null)
return new List<string>();
var results = new List<string>();
foreach (var multiData in multiDataList)
{
results.Add(multiData.FromUtf8Bytes());
}
return results;
}
public static Dictionary<string, string> ToStringDictionary(this byte[][] multiDataList)
{
if (multiDataList == null)
return new Dictionary<string, string>();
var map = new Dictionary<string, string>();
for (var i = 0; i < multiDataList.Length; i += 2)
{
var key = multiDataList[i].FromUtf8Bytes();
map[key] = multiDataList[i + 1].FromUtf8Bytes();
}
return map;
}
private static readonly NumberFormatInfo DoubleFormatProvider = new NumberFormatInfo
{
PositiveInfinitySymbol = "+inf",
NegativeInfinitySymbol = "-inf"
};
public static byte[] ToFastUtf8Bytes(this double value)
{
return FastToUtf8Bytes(value.ToString("R", DoubleFormatProvider));
}
private static byte[] FastToUtf8Bytes(string strVal)
{
var bytes = new byte[strVal.Length];
for (var i = 0; i < strVal.Length; i++)
bytes[i] = (byte) strVal[i];
return bytes;
}
public static byte[][] ToMultiByteArray(this string[] args)
{
var byteArgs = new byte[args.Length][];
for (var i = 0; i < args.Length; ++i)
byteArgs[i] = args[i].ToUtf8Bytes();
return byteArgs;
}
public static byte[][] PrependByteArray(this byte[][] args, byte[] valueToPrepend)
{
var newArgs = new byte[args.Length + 1][];
newArgs[0] = valueToPrepend;
var i = 1;
foreach (var arg in args)
newArgs[i++] = arg;
return newArgs;
}
public static byte[][] PrependInt(this byte[][] args, int valueToPrepend)
{
return args.PrependByteArray(valueToPrepend.ToUtf8Bytes());
}
}
}