diff --git a/RedisStudyTest/RedisListStudyTest.cs b/RedisStudyTest/RedisListStudyTest.cs index 75fc936..1d9c18f 100644 --- a/RedisStudyTest/RedisListStudyTest.cs +++ b/RedisStudyTest/RedisListStudyTest.cs @@ -58,6 +58,18 @@ namespace RedisStudyTest Assert.Equal("first", first); } + [Fact] + public void ListLeftPushsTest() + { + RedisValue[] listValues = new RedisValue[] + { + "first", + "secnd", + "third" + }; + var listLenth = redisListStudy.ListLeftPush(defaultRedisKey, listValues); + Assert.Equal(3, listLenth); + } #endregion #region ListRightPush @@ -76,6 +88,20 @@ namespace RedisStudyTest var first = redisListStudy.ListRightPop(defaultRedisKey); Assert.Equal("first", first); } + + [Fact] + public void ListRightPushsTest() + { + RedisValue[] listValues = new RedisValue[] + { + "first", + "secnd", + "third" + }; + var listLenth = redisListStudy.ListRightPush(defaultRedisKey, listValues); + Assert.Equal(3, listLenth); + } + #endregion #region ListInsertBefore @@ -162,7 +188,7 @@ namespace RedisStudyTest { redisListStudy.ListLeftPush(defaultRedisKey, "first"); - redisListStudy.ListSetByIndex(defaultRedisKey,0,"firstSetByIndex"); + redisListStudy.ListSetByIndex(defaultRedisKey, 0, "firstSetByIndex"); var first = redisListStudy.ListLeftPop(defaultRedisKey); @@ -170,6 +196,114 @@ namespace RedisStudyTest } #endregion + #region ListLength + [Fact] + public void ListLengthNotKeyTest() + { + var listLenth = redisListStudy.ListLength(defaultRedisKey); + Assert.Equal(0, listLenth); + } + + [Fact] + public void ListLengthTest() + { + redisListStudy.ListLeftPush(defaultRedisKey, "first"); + var listLenth = redisListStudy.ListLength(defaultRedisKey); + Assert.Equal(1, listLenth); + + redisListStudy.ListLeftPush(defaultRedisKey, "second"); + listLenth = redisListStudy.ListLength(defaultRedisKey); + Assert.Equal(2, listLenth); + + redisListStudy.ListLeftPush(defaultRedisKey, "third"); + listLenth = redisListStudy.ListLength(defaultRedisKey); + Assert.Equal(3, listLenth); + } + #endregion + + #region ListRange + + [Fact] + public void ListRangeNotKeyTest() + { + RedisValue[] values = redisListStudy.ListRange(defaultRedisKey, 0, -1); + Assert.Empty(values); + } + + [Fact] + public void ListRangeTest() + { + redisListStudy.ListLeftPush(defaultRedisKey, "first"); + var values = redisListStudy.ListRange(defaultRedisKey, 0, -1); + Assert.NotEmpty(values); + Assert.Contains("first", values); + + redisListStudy.ListLeftPush(defaultRedisKey, "second"); + values = redisListStudy.ListRange(defaultRedisKey, 0, -1); + Assert.NotEmpty(values); + Assert.Contains("first", values); + Assert.Contains("second", values); + } + + #endregion + + #region ListRemove + [Fact] + public void ListRemoveNotKeyTest() + { + var removeNum = redisListStudy.ListRemove(defaultRedisKey, "first"); + Assert.Equal(0, removeNum); + } + + [Fact] + public void ListRemoveTest() + { + var removeNum = 0L; + + redisListStudy.ListLeftPush(defaultRedisKey, new RedisValue[] { "first", "second", "third", "four" }); + removeNum = redisListStudy.ListRemove(defaultRedisKey, "first", 0); + Assert.Equal(1, removeNum); + + redisListStudy.ListLeftPush(defaultRedisKey, new RedisValue[] { "third", "four" }); + removeNum = redisListStudy.ListRemove(defaultRedisKey, "third", 0); + Assert.Equal(2, removeNum); + + redisListStudy.ListLeftPush(defaultRedisKey, new RedisValue[] { "1", "2", "1", "2", "1", "2" }); + removeNum = redisListStudy.ListRemove(defaultRedisKey, "2", 0); + Assert.Equal(3, removeNum); + } + #endregion + + #region ListTrim + + [Fact] + public void ListTrimNotKeyTest() + { + redisListStudy.ListTrim(defaultRedisKey, 2, 5); + var listCount = redisListStudy.ListLength(defaultRedisKey); + Assert.Equal(0, listCount); + } + + [Fact] + public void ListTrimTest() + { + RedisValue[] values = new RedisValue[] + { + "first", + "second", + "third", + "four" + }; + redisListStudy.ListLeftPush(defaultRedisKey, values); + + redisListStudy.ListTrim(defaultRedisKey,1,-1); + Assert.Equal(3, redisListStudy.ListLength(defaultRedisKey)); + + redisListStudy.ListTrim(defaultRedisKey, 2, -1); + Assert.Equal(1, redisListStudy.ListLength(defaultRedisKey)); + } + #endregion + #region 清理 public void Dispose() { diff --git a/RedisStuy/RedisListStudy.cs b/RedisStuy/RedisListStudy.cs index 1002d14..1afe6e8 100644 --- a/RedisStuy/RedisListStudy.cs +++ b/RedisStuy/RedisListStudy.cs @@ -45,7 +45,7 @@ namespace RedisStuy /// 没有找到指定元素 ,返回 -1 /// key 不存在或为空列表,返回 0 /// - public long ListLeftPushs(RedisKey key, RedisValue[] values, CommandFlags flags = CommandFlags.None) + public long ListLeftPush(RedisKey key, RedisValue[] values, CommandFlags flags = CommandFlags.None) { return redisDatabase.ListLeftPush(key, values, flags); } @@ -155,13 +155,11 @@ namespace RedisStuy } /// - /// 移除列表中与值相等的元素 - /// count > 0:从 头到尾 的移除值相等的元素 - /// count < 0:从 尾到头 的移除值相等的元素 - /// count =0:移除所有与值相等的元素 + /// 根据参数 COUNT 的值,移除列表中与参数 VALUE 相等的元素 + /// count 大于 0 从 头到尾 的移除值相等的元素 + /// count 小于 0 从 尾到头 的移除值相等的元素 + /// count 等于 0 移除所有与值相等的元素 /// - /// - /// public long ListRemove(RedisKey key, RedisValue value, long count = 0, CommandFlags flags = CommandFlags.None) { return redisDatabase.ListRemove(key, value, count, flags); @@ -195,6 +193,8 @@ namespace RedisStuy /// /// 修剪(trim)列表 /// 让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。 + /// 索引从0开始,例如:LTrim(FooBar 0 2)将修改在FooBar列表,保留列表的前三个元素。 + /// 索引为负数,表示从列表末尾的偏移,-1表示列表的最后一个元素,类推。 /// public void ListTrim(RedisKey key, long start, long stop, CommandFlags flags = CommandFlags.None) {