public async Task Can_Store_UnionBetweenSets() { string set1Name = PrefixedKey("testunionset1"); string set2Name = PrefixedKey("testunionset2"); string storeSetName = PrefixedKey("testunionsetstore"); var set1Members = new List <string> { "one", "two", "three", "four", "five" }; var set2Members = new List <string> { "four", "five", "six", "seven" }; await set1Members.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync(set1Name, x)); await set2Members.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync(set2Name, x)); await RedisAsync.StoreUnionFromSortedSetsAsync(storeSetName, new[] { set1Name, set2Name }); var unionMembers = await RedisAsync.GetAllItemsFromSortedSetAsync(storeSetName); Assert.That(unionMembers, Is.EquivalentTo( new List <string> { "one", "two", "three", "four", "five", "six", "seven" })); }
public async Task Can_enumerate_large_ICollection_Set() { if (TestConfig.IgnoreLongTests) { return; } const int setSize = 2500; storeMembers = new List <string>(); await setSize.TimesAsync(async x => { await RedisAsync.AddItemToSortedSetAsync(SetId, x.ToString()); storeMembers.Add(x.ToString()); }); var members = new List <string>(); await foreach (var item in RedisAsync.SortedSets[SetId]) { members.Add(item); } members.Sort((x, y) => int.Parse(x).CompareTo(int.Parse(y))); Assert.That(members.Count, Is.EqualTo(storeMembers.Count)); Assert.That(members, Is.EquivalentTo(storeMembers)); }
public async Task Can_call_Type_in_transaction() { await RedisAsync.SetValueAsync("string", "STRING"); await RedisAsync.AddItemToListAsync("list", "LIST"); await RedisAsync.AddItemToSetAsync("set", "SET"); await RedisAsync.AddItemToSortedSetAsync("zset", "ZSET", 1); var keys = new[] { "string", "list", "set", "zset" }; var results = new Dictionary <string, string>(); await using (var trans = await RedisAsync.CreateTransactionAsync()) { foreach (var key in keys) { trans.QueueCommand(r => r.TypeAsync(key), x => results[key] = x); } await trans.CommitAsync(); } results.PrintDump(); Assert.That(results, Is.EquivalentTo(new Dictionary <string, string> { { "string", "string" }, { "list", "list" }, { "set", "set" }, { "zset", "zset" }, })); }
public async Task Can_use_SortedIndex_to_store_articles_by_Date() { var redisArticles = RedisAsync.As <Article>(); var articles = new[] { new Article { Id = 1, Title = "Article 1", ModifiedDate = new DateTime(2015, 01, 02) }, new Article { Id = 2, Title = "Article 2", ModifiedDate = new DateTime(2015, 01, 01) }, new Article { Id = 3, Title = "Article 3", ModifiedDate = new DateTime(2015, 01, 03) }, }; await redisArticles.StoreAllAsync(articles); const string LatestArticlesSet = "urn:Article:modified"; foreach (var article in articles) { await RedisAsync.AddItemToSortedSetAsync(LatestArticlesSet, article.Id.ToString(), article.ModifiedDate.Ticks); } var articleIds = await RedisAsync.GetAllItemsFromSortedSetDescAsync(LatestArticlesSet); articleIds.PrintDump(); var latestArticles = await redisArticles.GetByIdsAsync(articleIds); latestArticles.PrintDump(); }
public async Task Can_Pop_priority_message_from_SortedSet_and_Add_to_workq_in_atomic_transaction() { var messages = new List <string> { "message4", "message3", "message2" }; await RedisAsync.AddItemToListAsync(Prefix + "workq", "message1"); var priority = 1; await messages.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync(Prefix + "prioritymsgs", x, priority++)); var highestPriorityMessage = await RedisAsync.PopItemWithHighestScoreFromSortedSetAsync(Prefix + "prioritymsgs"); await using (var trans = await RedisAsync.CreateTransactionAsync()) { trans.QueueCommand(r => r.RemoveItemFromSortedSetAsync(Prefix + "prioritymsgs", highestPriorityMessage)); trans.QueueCommand(r => r.AddItemToListAsync(Prefix + "workq", highestPriorityMessage)); await trans.CommitAsync(); } Assert.That(await RedisAsync.GetAllItemsFromListAsync(Prefix + "workq"), Is.EquivalentTo(new List <string> { "message1", "message2" })); Assert.That(await RedisAsync.GetAllItemsFromSortedSetAsync(Prefix + "prioritymsgs"), Is.EquivalentTo(new List <string> { "message3", "message4" })); }
public async Task Can_add_large_score_to_sortedset() { await RedisAsync.AddItemToSortedSetAsync(SetId, "value", 12345678901234567890d); var score = await RedisAsync.GetItemScoreInSortedSetAsync(SetId, "value"); Assert.That(score, Is.EqualTo(12345678901234567890d)); }
public async Task Can_GetSetCount() { await storeMembers.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync(SetId, x)); var setCount = await RedisAsync.GetSortedSetCountAsync(SetId); Assert.That(setCount, Is.EqualTo(storeMembers.Count)); }
public async Task Can_AddItemToSortedSet_in_different_Culture() { await RedisAsync.AddItemToSortedSetAsync("somekey1", "somevalue", 66121.202); var score = await RedisAsync.GetItemScoreInSortedSetAsync("somekey1", "somevalue"); Assert.That(score, Is.EqualTo(66121.202)); }
public async Task Can_AddItemToSortedSet_and_GetAllFromSet() { var i = 0; await storeMembers.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync(SetId, x, i++)); var members = await RedisAsync.GetAllItemsFromSortedSetAsync(SetId); Assert.That(members.EquivalentTo(storeMembers), Is.True); }
public async Task AddToSet_without_score_adds_an_implicit_lexical_order_score() { await storeMembers.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync(SetId, x)); var members = await RedisAsync.GetAllItemsFromSortedSetAsync(SetId); storeMembers.Sort((x, y) => x.CompareTo(y)); Assert.That(members.EquivalentTo(storeMembers), Is.True); }
public async Task Can_PopFromSet() { var i = 0; await storeMembers.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync(SetId, x, i++)); var member = await RedisAsync.PopItemWithHighestScoreFromSortedSetAsync(SetId); Assert.That(member, Is.EqualTo("four")); }
public async Task AddToSet_with_same_score_is_still_returned_in_lexical_order_score() { await storeMembers.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync(SetId, x, 1)); var members = await RedisAsync.GetAllItemsFromSortedSetAsync(SetId); storeMembers.Sort((x, y) => x.CompareTo(y)); Assert.That(members.EquivalentTo(storeMembers)); }
public async Task Does_SortedSetContainsValue() { const string existingMember = "two"; const string nonExistingMember = "five"; await storeMembers.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync(SetId, x)); Assert.That(await RedisAsync.SortedSetContainsItemAsync(SetId, existingMember), Is.True); Assert.That(await RedisAsync.SortedSetContainsItemAsync(SetId, nonExistingMember), Is.False); }
public async Task Can_GetRangeFromSortedSetByHighestScore_from_sorted_set() { await storeMembers.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync(SetId, x)); storeMembers.Sort((x, y) => y.CompareTo(x)); var memberRage = storeMembers.Where(x => x.CompareTo("four") >= 0 && x.CompareTo("three") <= 0).ToList(); var range = await RedisAsync.GetRangeFromSortedSetByHighestScoreAsync(SetId, "four", "three"); Assert.That(range.EquivalentTo(memberRage)); }
public async Task Can_GetSetCountByScores() { var scores = new List <double>(); await storeMembers.ForEachAsync(async x => { await RedisAsync.AddItemToSortedSetAsync(SetId, x); scores.Add(RedisClient.GetLexicalScore(x)); }); Assert.That(await RedisAsync.GetSortedSetCountAsync(SetId, scores.Min(), scores.Max()), Is.EqualTo(storeMembers.Count())); Assert.That(await RedisAsync.GetSortedSetCountAsync(SetId, scores.Min(), scores.Min()), Is.EqualTo(1)); }
public async Task Can_RemoveFromSet() { const string removeMember = "two"; await storeMembers.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync(SetId, x)); await RedisAsync.RemoveItemFromSortedSetAsync(SetId, removeMember); storeMembers.Remove(removeMember); var members = await RedisAsync.GetAllItemsFromSortedSetAsync(SetId); Assert.That(members, Is.EquivalentTo(storeMembers)); }
public async Task Can_pop_items_with_lowest_and_highest_scores_from_sorted_set() { await storeMembers.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync(SetId, x)); storeMembers.Sort((x, y) => x.CompareTo(y)); var lowestScore = await RedisAsync.PopItemWithLowestScoreFromSortedSetAsync(SetId); Assert.That(lowestScore, Is.EqualTo(storeMembers.First())); var highestScore = await RedisAsync.PopItemWithHighestScoreFromSortedSetAsync(SetId); Assert.That(highestScore, Is.EqualTo(storeMembers[storeMembers.Count - 1])); }
public async Task Can_enumerate_small_ICollection_Set() { await storeMembers.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync(SetId, x)); var members = new List <string>(); await foreach (var item in RedisAsync.SortedSets[SetId]) { members.Add(item); } members.Sort(); Assert.That(members.Count, Is.EqualTo(storeMembers.Count)); Assert.That(members, Is.EquivalentTo(storeMembers)); }
public async Task Can_ZScan_10_sortedsets() { await RedisAsync.FlushAllAsync(); var items = 10.Times(x => "item" + x); var i = 0; await items.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync("scanzset", x, i++)); var ret = await NativeAsync.ZScanAsync("scanzset", 0); var itemsWithScore = ret.AsItemsWithScores(); Assert.That(itemsWithScore.Keys, Is.EqualTo(items)); Assert.That(itemsWithScore.Values, Is.EqualTo(10.Times(x => (double)x))); }
public async Task Can_GetItemIndexInSortedSet_in_Asc_and_Desc() { var i = 10; await storeMembers.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync(SetId, x, i++)); Assert.That(await RedisAsync.GetItemIndexInSortedSetAsync(SetId, "one"), Is.EqualTo(0)); Assert.That(await RedisAsync.GetItemIndexInSortedSetAsync(SetId, "two"), Is.EqualTo(1)); Assert.That(await RedisAsync.GetItemIndexInSortedSetAsync(SetId, "three"), Is.EqualTo(2)); Assert.That(await RedisAsync.GetItemIndexInSortedSetAsync(SetId, "four"), Is.EqualTo(3)); Assert.That(await RedisAsync.GetItemIndexInSortedSetDescAsync(SetId, "one"), Is.EqualTo(3)); Assert.That(await RedisAsync.GetItemIndexInSortedSetDescAsync(SetId, "two"), Is.EqualTo(2)); Assert.That(await RedisAsync.GetItemIndexInSortedSetDescAsync(SetId, "three"), Is.EqualTo(1)); Assert.That(await RedisAsync.GetItemIndexInSortedSetDescAsync(SetId, "four"), Is.EqualTo(0)); }
public async Task Can_get_index_and_score_from_SortedSet() { storeMembers = new List <string> { "a", "b", "c", "d" }; const double initialScore = 10d; var i = initialScore; await storeMembers.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync(SetId, x, i++)); Assert.That(await RedisAsync.GetItemIndexInSortedSetAsync(SetId, "a"), Is.EqualTo(0)); Assert.That(await RedisAsync.GetItemIndexInSortedSetDescAsync(SetId, "a"), Is.EqualTo(storeMembers.Count - 1)); Assert.That(await RedisAsync.GetItemScoreInSortedSetAsync(SetId, "a"), Is.EqualTo(initialScore)); Assert.That(await RedisAsync.GetItemScoreInSortedSetAsync(SetId, "d"), Is.EqualTo(initialScore + storeMembers.Count - 1)); }
public async Task Can_RemoveItemsFromSortedSet() { var removeMembers = new[] { "two", "four", "six" }; await storeMembers.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync(SetId, x)); var removeCount = await RedisAsync.RemoveItemsFromSortedSetAsync(SetId, removeMembers.ToList()); Assert.That(removeCount, Is.EqualTo(2)); removeMembers.Each(x => storeMembers.Remove(x)); var members = await RedisAsync.GetAllItemsFromSortedSetAsync(SetId); Assert.That(members, Is.EquivalentTo(storeMembers)); }
public async Task Can_IncrementItemInSortedSet() { await stringDoubleMap.ForEachAsync(async (k, v) => await RedisAsync.AddItemToSortedSetAsync(SetId, k, v)); var currentScore = await RedisAsync.IncrementItemInSortedSetAsync(SetId, "one", 3); stringDoubleMap["one"] = stringDoubleMap["one"] + 3; Assert.That(currentScore, Is.EqualTo(stringDoubleMap["one"])); currentScore = await RedisAsync.IncrementItemInSortedSetAsync(SetId, "four", -3); stringDoubleMap["four"] = stringDoubleMap["four"] - 3; Assert.That(currentScore, Is.EqualTo(stringDoubleMap["four"])); var map = await RedisAsync.GetAllWithScoresFromSortedSetAsync(SetId); Assert.That(stringDoubleMap.UnorderedEquivalentTo(map)); }
public async Task Can_get_Types() { await RedisAsync.SetValueAsync("string", "string"); await RedisAsync.AddItemToListAsync("list", "list"); await RedisAsync.AddItemToSetAsync("set", "set"); await RedisAsync.AddItemToSortedSetAsync("sortedset", "sortedset"); await RedisAsync.SetEntryInHashAsync("hash", "key", "val"); Assert.That(await RedisAsync.GetEntryTypeAsync("nokey"), Is.EqualTo(RedisKeyType.None)); Assert.That(await RedisAsync.GetEntryTypeAsync("string"), Is.EqualTo(RedisKeyType.String)); Assert.That(await RedisAsync.GetEntryTypeAsync("list"), Is.EqualTo(RedisKeyType.List)); Assert.That(await RedisAsync.GetEntryTypeAsync("set"), Is.EqualTo(RedisKeyType.Set)); Assert.That(await RedisAsync.GetEntryTypeAsync("sortedset"), Is.EqualTo(RedisKeyType.SortedSet)); Assert.That(await RedisAsync.GetEntryTypeAsync("hash"), Is.EqualTo(RedisKeyType.Hash)); }
public async Task Does_lazy_scan_all_sortedset_items() { await RedisAsync.FlushAllAsync(); var items = 100.Times(x => "item" + x); var i = 0; await items.ForEachAsync(async x => await RedisAsync.AddItemToSortedSetAsync("scanzset", x, i++)); var scanAllItems = RedisAsync.ScanAllSortedSetItemsAsync("scanzset", pageSize: 10); var tenKeys = await scanAllItems.TakeAsync(10).ToListAsync(); Assert.That(tenKeys.Count, Is.EqualTo(10)); Assert.That(await scanAllItems.CountAsync(), Is.EqualTo(100)); var map = await scanAllItems.ToDictionaryAsync(x => x.Key, x => x.Value); Assert.That(map.Keys, Is.EquivalentTo(items)); }
public async Task Can_create_ZRevPop_with_lua() { var luaBody = @" local val = redis.call('zrange', KEYS[1], -ARGV[1], -1) if val then redis.call('zremrangebyrank', KEYS[1], -ARGV[1], -1) end return val"; var i = 0; var alphabet = 26.Times(c => ((char)('A' + c)).ToString()); foreach (var x in alphabet) { await RedisAsync.AddItemToSortedSetAsync("zalphabet", x, i ++); } var letters = await RedisAsync.ExecLuaAsListAsync(luaBody, keys : new[] { "zalphabet" }, args : new[] { "3" }); letters.PrintDump(); Assert.That(letters, Is.EquivalentTo(new[] { "X", "Y", "Z" })); }
public async Task Can_WorkInSortedSetUnderDifferentCulture() { #if NETCORE var prevCulture = CultureInfo.CurrentCulture; CultureInfo.CurrentCulture = new CultureInfo("ru-RU"); #else var prevCulture = Thread.CurrentThread.CurrentCulture; Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("ru-RU"); #endif await RedisAsync.AddItemToSortedSetAsync(SetId, "key", 123.22); var map = await RedisAsync.GetAllWithScoresFromSortedSetAsync(SetId); Assert.AreEqual(123.22, map["key"]); #if NETCORE CultureInfo.CurrentCulture = prevCulture; #else Thread.CurrentThread.CurrentCulture = prevCulture; #endif }