本文整理汇总了C#中RedisClient.Sort方法的典型用法代码示例。如果您正苦于以下问题:C# RedisClient.Sort方法的具体用法?C# RedisClient.Sort怎么用?C# RedisClient.Sort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RedisClient
的用法示例。
在下文中一共展示了RedisClient.Sort方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestSort
public void TestSort()
{
using(var mock = new MockConnector("localhost", 9999, "*2\r\n$2\r\nab\r\n$2\r\ncd\r\n", "*0\r\n", "*0\r\n", "*0\r\n", "*0\r\n", "*0\r\n", "*0\r\n", "*0\r\n"))
using(var redis = new RedisClient(mock))
{
var resp1 = redis.Sort("test1");
Assert.Equal(2, resp1.Length);
Assert.Equal("ab", (string)resp1[0]);
Assert.Equal("cd", (string)resp1[1]);
Assert.Equal("*2\r\n$4\r\nSORT\r\n$5\r\ntest1\r\n", mock.GetMessage());
var resp2 = redis.Sort("test2", offset: 0, count: 2);
Assert.Equal("*5\r\n$4\r\nSORT\r\n$5\r\ntest2\r\n$5\r\nLIMIT\r\n$1\r\n0\r\n$1\r\n2\r\n", mock.GetMessage());
var resp3 = redis.Sort("test3", by: "xyz");
Assert.Equal("*4\r\n$4\r\nSORT\r\n$5\r\ntest3\r\n$2\r\nBY\r\n$3\r\nxyz\r\n", mock.GetMessage());
var resp4 = redis.Sort("test4", sort: RedisSort.Asc);
Assert.Equal("*3\r\n$4\r\nSORT\r\n$5\r\ntest4\r\n$3\r\nASC\r\n", mock.GetMessage());
var resp5 = redis.Sort("test5", sort: RedisSort.Desc);
Assert.Equal("*3\r\n$4\r\nSORT\r\n$5\r\ntest5\r\n$4\r\nDESC\r\n", mock.GetMessage());
var resp6 = redis.Sort("test6", alpha: true);
Assert.Equal("*3\r\n$4\r\nSORT\r\n$5\r\ntest6\r\n$5\r\nALPHA\r\n", mock.GetMessage());
var resp7 = redis.Sort("test7", get: new[] { "get1", "get2" });
Assert.Equal("*6\r\n$4\r\nSORT\r\n$5\r\ntest7\r\n$3\r\nGET\r\n$4\r\nget1\r\n$3\r\nGET\r\n$4\r\nget2\r\n", mock.GetMessage());
var resp8 = redis.Sort("test8", offset: 0, count: 2, by: "xyz", sort: RedisSort.Asc, alpha: true, get: new[] { "a", "b" });
Assert.Equal("*13\r\n$4\r\nSORT\r\n$5\r\ntest8\r\n$2\r\nBY\r\n$3\r\nxyz\r\n$5\r\nLIMIT\r\n$1\r\n0\r\n$1\r\n2\r\n$3\r\nGET\r\n$1\r\na\r\n$3\r\nGET\r\n$1\r\nb\r\n$3\r\nASC\r\n$5\r\nALPHA\r\n", mock.GetMessage());
}
}
示例2: TestSort
public void TestSort()
{
using (var mock = new FakeRedisSocket("*2\r\n$2\r\nab\r\n$2\r\ncd\r\n", "*0\r\n", "*0\r\n", "*0\r\n", "*0\r\n", "*0\r\n", "*0\r\n", "*0\r\n"))
using (var redis = new RedisClient(mock, new DnsEndPoint("fakehost", 9999)))
{
var resp1 = redis.Sort("test1");
Assert.AreEqual(2, resp1.Length);
Assert.AreEqual("ab", resp1[0]);
Assert.AreEqual("cd", resp1[1]);
Assert.AreEqual("*2\r\n$4\r\nSORT\r\n$5\r\ntest1\r\n", mock.GetMessage());
var resp2 = redis.Sort("test2", offset: 0, count: 2);
Assert.AreEqual("*5\r\n$4\r\nSORT\r\n$5\r\ntest2\r\n$5\r\nLIMIT\r\n$1\r\n0\r\n$1\r\n2\r\n", mock.GetMessage());
var resp3 = redis.Sort("test3", by: "xyz");
Assert.AreEqual("*4\r\n$4\r\nSORT\r\n$5\r\ntest3\r\n$2\r\nBY\r\n$3\r\nxyz\r\n", mock.GetMessage());
var resp4 = redis.Sort("test4", dir: RedisSortDir.Asc);
Assert.AreEqual("*3\r\n$4\r\nSORT\r\n$5\r\ntest4\r\n$3\r\nASC\r\n", mock.GetMessage());
var resp5 = redis.Sort("test5", dir: RedisSortDir.Desc);
Assert.AreEqual("*3\r\n$4\r\nSORT\r\n$5\r\ntest5\r\n$4\r\nDESC\r\n", mock.GetMessage());
var resp6 = redis.Sort("test6", isAlpha: true);
Assert.AreEqual("*3\r\n$4\r\nSORT\r\n$5\r\ntest6\r\n$5\r\nALPHA\r\n", mock.GetMessage());
var resp7 = redis.Sort("test7", get: new[] { "get1", "get2" });
Assert.AreEqual("*6\r\n$4\r\nSORT\r\n$5\r\ntest7\r\n$3\r\nGET\r\n$4\r\nget1\r\n$3\r\nGET\r\n$4\r\nget2\r\n", mock.GetMessage());
var resp8 = redis.Sort("test8", offset: 0, count: 2, by: "xyz", dir: RedisSortDir.Asc, isAlpha: true, get: new[] { "a", "b" });
Assert.AreEqual("*13\r\n$4\r\nSORT\r\n$5\r\ntest8\r\n$2\r\nBY\r\n$3\r\nxyz\r\n$5\r\nLIMIT\r\n$1\r\n0\r\n$1\r\n2\r\n$3\r\nGET\r\n$1\r\na\r\n$3\r\nGET\r\n$1\r\nb\r\n$3\r\nASC\r\n$5\r\nALPHA\r\n", mock.GetMessage());
}
}
示例3: Run
public override void Run()
{
var personnels = Personnel.GetSamplePersonnels();
var client = new RedisClient();
var listKey = "personnels";
// setup sample data
client.Pipeline(c =>
{
client.Del(listKey);
foreach (var person in personnels) {
var idStr = person.Id.ToString();
c.LPush(listKey, idStr);
c.HSet(idStr, "name", person.Name);
c.HSet(idStr, "email", person.Email);
c.HIncrBy(idStr, "age", person.Age);
c.HIncrBy(idStr, "salary", person.Salary);
c.HSet(idStr, "json", person.ToJson());
}
});
while (true) {
WriteLine("0. List personnels sorted by yearly income.");
WriteLine("1. List personnels sorted by age.");
WriteLine("2. List personnels sorted by id descending.");
WriteLine("3. List personnels sorted by name.");
WriteLine("4. List all personnel emails.");
string[] result;
var parseResult = true;
switch (ReadLine()) {
case "0": {
result = client.Sort(listKey,
byPattern: "*->salary",
getPattern: new[] { "*->json" });
} break;
case "1": {
parseResult = true;
result = client.Sort(listKey,
byPattern: "*->age",
getPattern: new[] { "*->json" });
} break;
case "2": {
parseResult = true;
result = client.Sort(listKey,
descending: true,
getPattern: new[] { "*->json" });
} break;
case "3": {
result = client.Sort(listKey,
byPattern: "*->name",
alpha: true,
getPattern: new[] { "*->json" });
} break;
case "4": {
result = client.Sort(listKey,
byPattern: "nosort", // non-existent key
getPattern: new[] { "*->email" });
parseResult = false;
} break;
default: continue;
}
if (parseResult)
result = result
.Select(json => Personnel.FromJson(json).ToString())
.ToArray();
WriteLine("\r\nResults:");
Array.ForEach(result, WriteLine);
WriteLine("\r\n");
}
}