From 8ae8e924add6741eb9b8e668aed6b1cf732d9f6d Mon Sep 17 00:00:00 2001 From: ruyu Date: Sun, 24 Jun 2018 13:05:19 +0800 Subject: [PATCH] =?UTF-8?q?HashStudy=E6=B5=8B=E8=AF=95=E7=8E=87100%?= =?UTF-8?q?=EF=BC=8C=E6=95=B4=E7=90=86=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RedisStudyTest/RedisHashStudyTest.cs | 321 ++++++++++++++---------- RedisStudyTest/RedisLockStudyTest.cs | 1 + RedisStudyTest/RedisServerStudyTest.cs | 1 + RedisStudyTest/RedisSortSetStudyTest.cs | 1 + RedisStuy/RedisHashStudy.cs | 19 +- RedisStuy/RedisScanStudy.cs | 17 ++ RedisStuy/RedisStuy.csproj | 1 + 7 files changed, 210 insertions(+), 151 deletions(-) create mode 100644 RedisStuy/RedisScanStudy.cs diff --git a/RedisStudyTest/RedisHashStudyTest.cs b/RedisStudyTest/RedisHashStudyTest.cs index dd5b1a8..08fb3d5 100644 --- a/RedisStudyTest/RedisHashStudyTest.cs +++ b/RedisStudyTest/RedisHashStudyTest.cs @@ -18,18 +18,19 @@ namespace RedisStudyTest /// /// Redis Hash 类型测试 /// + [Trait("RedisHash", "All")] public class RedisHashStudyTest : IDisposable { #region 初始化 private readonly ITestOutputHelper testOutput; private IDatabase redisDatabase = null; - private RedisHashStudy hashStudy = null; - private List students; + private RedisHashStudy redisHashStudy = null; + private List defaultStudentList; private Student defaultStudent = null; - private string preHashKey = "RedisStudy:Student:"; - private string defaultHashKey = ""; - private int keyExpireSeconds = 20; + private string preStudentHashKey = "RedisStudy:Student:"; + private string defaultStudentHashKey = ""; + private int keyDefaultExpireSeconds = 20; /// /// 构造 @@ -39,7 +40,7 @@ namespace RedisStudyTest this.testOutput = output; redisDatabase = RedisHelper.GetRedisDatabase(); - hashStudy = new RedisHashStudy(); + redisHashStudy = new RedisHashStudy(); defaultStudent = new Student() { Id = 1, @@ -47,9 +48,9 @@ namespace RedisStudyTest Age = 18 }; - defaultHashKey = preHashKey + defaultStudent.Id; + defaultStudentHashKey = preStudentHashKey + defaultStudent.Id; - students = new List() + defaultStudentList = new List() { new Student() { @@ -82,6 +83,8 @@ namespace RedisStudyTest Age = 55 }, }; + + //删理默认学生 DeleteExitsStudents(); } #endregion @@ -90,6 +93,7 @@ namespace RedisStudyTest /// /// xUnit输出信息 测试 /// + [Trait("RedisHash", "HashSet")] [Fact(DisplayName = "HashSet 输出信息测试")] public void HashSetOutputTest() { @@ -100,72 +104,74 @@ namespace RedisStudyTest /// /// 参数异常 测试 /// + [Trait("RedisHash", "HashSet")] [Fact(DisplayName = "HashSet 异常测试")] public void HashSetExceptionTest() { - string redisKey = preHashKey + defaultStudent.Id; + string redisKey = preStudentHashKey + defaultStudent.Id; //参数异常测试 - Assert.Throws(() => hashStudy.HashSet(string.Empty, null)); - Assert.Throws(() => hashStudy.HashSet("", null)); - Assert.Throws(() => hashStudy.HashSet(preHashKey + "-1", null)); - Assert.Throws(() => hashStudy.HashSet(preHashKey + "-1", new HashEntry[] { })); + Assert.Throws(() => redisHashStudy.HashSet(string.Empty, null)); + Assert.Throws(() => redisHashStudy.HashSet("", null)); + Assert.Throws(() => redisHashStudy.HashSet(preStudentHashKey + "-1", null)); + Assert.Throws(() => redisHashStudy.HashSet(preStudentHashKey + "-1", new HashEntry[] { })); } /// /// 参数 When 测试 /// - [Trait("HashSet", "When")] + [Trait("RedisHash", "HashSet")] [Fact(DisplayName = "HashSet When参数测试")] public void HashSetWhenTest() { - string redisKey = preHashKey + defaultStudent.Id; + string redisKey = preStudentHashKey + defaultStudent.Id; //当前上下文不能使用: When.Exists - var id_When_NotExists_No = hashStudy.HashSet(redisKey, "Id", defaultStudent.Id + 1, When.NotExists); + var id_When_NotExists_No = redisHashStudy.HashSet(redisKey, "Id", defaultStudent.Id + 1, When.NotExists); Assert.True(id_When_NotExists_No); - var id_When_NotExists_Yes = hashStudy.HashSet(redisKey, "Id", defaultStudent.Id + 1, When.NotExists); + var id_When_NotExists_Yes = redisHashStudy.HashSet(redisKey, "Id", defaultStudent.Id + 1, When.NotExists); Assert.False(id_When_NotExists_Yes); - var id_When_Always_Exists = hashStudy.HashSet(redisKey, "Id", defaultStudent.Id + 1, When.Always); + var id_When_Always_Exists = redisHashStudy.HashSet(redisKey, "Id", defaultStudent.Id + 1, When.Always); Assert.False(id_When_Always_Exists); - var id_When_Always_NotExists = hashStudy.HashSet(redisKey, "Id4", defaultStudent.Id + 1, When.Always); + var id_When_Always_NotExists = redisHashStudy.HashSet(redisKey, "Id4", defaultStudent.Id + 1, When.Always); Assert.True(id_When_Always_NotExists); } /// /// 添加一个默认学生 测试 /// + [Trait("RedisHash", "HashSet")] [Fact] public void HashSetAddStudentTest() { - string redisKey = preHashKey + defaultStudent.Id; var studentEntries = new HashEntry[] { - new HashEntry("Id",1), + new HashEntry("Id",defaultStudent.Id), new HashEntry("Name",defaultStudent.Name), new HashEntry("Age",defaultStudent.Age), }; //插入Sudent - var addHash = hashStudy.HashSet(redisKey, studentEntries, CommandFlags.None); + var addHash = redisHashStudy.HashSet(defaultStudentHashKey, studentEntries, CommandFlags.None); Assert.True(addHash); //设置过期 - redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(keyExpireSeconds)); + redisDatabase.KeyExpire(defaultStudentHashKey, TimeSpan.FromSeconds(keyDefaultExpireSeconds)); } /// /// 添加一组初始化设置的学生 测试 /// + [Trait("RedisHash", "HashSet")] [Fact] public void HashSetAddGroupStudentTest() { - foreach (var temp in students) + foreach (var temp in defaultStudentList) { - string redisKey = preHashKey + temp.Id; + string redisKey = preStudentHashKey + temp.Id; var studentEntries = new HashEntry[] { new HashEntry("Id", temp.Id), @@ -174,34 +180,35 @@ namespace RedisStudyTest }; //插入Sudent - var addStudent = hashStudy.HashSet(redisKey, studentEntries); + var addStudent = redisHashStudy.HashSet(redisKey, studentEntries); Assert.True(addStudent); //设置过期 - redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(keyExpireSeconds)); + redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(keyDefaultExpireSeconds)); } //清理删除 - foreach (var temp in students) + foreach (var temp in defaultStudentList) { - redisDatabase.KeyDelete(preHashKey + temp.Id); + redisDatabase.KeyDelete(preStudentHashKey + temp.Id); } } /// /// 设置一个哈希字段 测试 /// + [Trait("RedisHash", "HashSet")] [Fact] public void HashSetHashfieldTest() { - Assert.True(hashStudy.HashSet(preHashKey + defaultStudent.Id, "Id", defaultStudent.Id)); - Assert.False(hashStudy.HashSet(preHashKey + defaultStudent.Id, "Id", defaultStudent.Id)); + Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, "Id", defaultStudent.Id)); + Assert.False(redisHashStudy.HashSet(defaultStudentHashKey, "Id", defaultStudent.Id)); - Assert.True(hashStudy.HashSet(preHashKey + defaultStudent.Id, "Name", defaultStudent.Name)); - Assert.False(hashStudy.HashSet(preHashKey + defaultStudent.Id, "Name", defaultStudent.Name)); + Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, "Name", defaultStudent.Name)); + Assert.False(redisHashStudy.HashSet(defaultStudentHashKey, "Name", defaultStudent.Name)); - Assert.True(hashStudy.HashSet(preHashKey + defaultStudent.Id, "Age", defaultStudent.Age)); - Assert.False(hashStudy.HashSet(preHashKey + defaultStudent.Id, "Age", defaultStudent.Age)); + Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, "Age", defaultStudent.Age)); + Assert.False(redisHashStudy.HashSet(defaultStudentHashKey, "Age", defaultStudent.Age)); } /// @@ -210,8 +217,8 @@ namespace RedisStudyTest [Fact] public void HashSetGroupHashfieldTest() { - Assert.True(hashStudy.HashSet(preHashKey + defaultStudent.Id, "Id", defaultStudent.Id)); - Assert.False(hashStudy.HashSet(preHashKey + defaultStudent.Id, "Id", defaultStudent.Id + 1)); + Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, "Id", defaultStudent.Id)); + Assert.False(redisHashStudy.HashSet(defaultStudentHashKey, "Id", defaultStudent.Id + 1)); var entrys = new HashEntry[] { @@ -225,18 +232,19 @@ namespace RedisStudyTest new HashEntry("Age", defaultStudent.Age+1), }; - Assert.True(hashStudy.HashSet(preHashKey + defaultStudent.Id, entrys)); - Assert.True(hashStudy.HashSet(preHashKey + defaultStudent.Id, entrys2)); + Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, entrys)); + Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, entrys2)); } /// /// 特例测试:给key为空的哈希 设置字段 /// 结 果:添加或更新操作能成功,但是字段值插入不进去。 /// + [Trait("RedisHash", "HashSet")] [Fact] public void HashSetForEmptyKeyTest() { - Assert.True(hashStudy.HashSet(string.Empty, "Name", "wanggaofeng")); + Assert.True(redisHashStudy.HashSet(string.Empty, "Name", "wanggaofeng")); redisDatabase.KeyDelete(string.Empty); } @@ -244,13 +252,14 @@ namespace RedisStudyTest /// 特例测试:给字段名为空串的哈希 设置字段 /// 结 果:添加或更新操作正常,只是字段键名为"" /// + [Trait("RedisHash", "HashSet")] [Fact] public void HashSetForEmptyFieldTest() { - Assert.True(hashStudy.HashSet(preHashKey + defaultStudent.Id, "", defaultStudent.Id)); - Assert.False(hashStudy.HashSet(preHashKey + defaultStudent.Id, "", defaultStudent.Id + 1)); + Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, "", defaultStudent.Id)); + Assert.False(redisHashStudy.HashSet(defaultStudentHashKey, "", defaultStudent.Id + 1)); - redisDatabase.KeyDelete(preHashKey + defaultStudent.Id); + redisDatabase.KeyDelete(defaultStudentHashKey); } #endregion @@ -264,10 +273,8 @@ namespace RedisStudyTest [Fact] public void HashDecrementExceptionTest() { - string redisKey = preHashKey + defaultStudent.Id; - - Assert.True(hashStudy.HashSet(redisKey, "Name", "wanggaofeng")); - Assert.Throws(() => hashStudy.HashDecrement(redisKey, "Name", 1)); + Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, "Name", "wanggaofeng")); + Assert.Throws(() => redisHashStudy.HashDecrement(defaultStudentHashKey, "Name", 1)); } /// @@ -277,13 +284,11 @@ namespace RedisStudyTest [Fact] public void HashDecrementHashKeyTest() { - string redisKey = preHashKey + defaultStudent.Id; - //Key不存在,先创建哈希表,再添加值为0的字段,然后执行自减操作 - Assert.Equal(-1, hashStudy.HashDecrement(redisKey, "Id", 1)); + Assert.Equal(-1, redisHashStudy.HashDecrement(defaultStudentHashKey, "Id", 1)); //存在,直接自减 - Assert.Equal(-1, hashStudy.HashGet(redisKey, "Id")); + Assert.Equal(-1, redisHashStudy.HashGet(defaultStudentHashKey, "Id")); } /// @@ -293,11 +298,9 @@ namespace RedisStudyTest [Fact] public void HashDecrementHashFieldTest() { - string redisKey = preHashKey + defaultStudent.Id; - //字段不存在,则创建之 - Assert.Equal(-1, hashStudy.HashDecrement(redisKey, "Age", 1)); - Assert.Equal(-1, hashStudy.HashGet(redisKey, "Age")); + Assert.Equal(-1, redisHashStudy.HashDecrement(defaultStudentHashKey, "Age", 1)); + Assert.Equal(-1, redisHashStudy.HashGet(defaultStudentHashKey, "Age")); } /// @@ -306,10 +309,8 @@ namespace RedisStudyTest [Fact] public void HashDecrementMinusTest() { - string redisKey = preHashKey + defaultStudent.Id; - //自减负数时,则自增 - Assert.Equal(2, hashStudy.HashDecrement(redisKey, "Age", -2)); + Assert.Equal(2, redisHashStudy.HashDecrement(defaultStudentHashKey, "Age", -2)); } /// @@ -318,12 +319,10 @@ namespace RedisStudyTest [Fact] public void HashDecrementByIntTest() { - string redisKey = preHashKey + defaultStudent.Id; - //字段减少1 - Assert.Equal(-1, hashStudy.HashDecrement(redisKey, "Age", 1)); + Assert.Equal(-1, redisHashStudy.HashDecrement(defaultStudentHashKey, "Age", 1)); //字段减少2 - Assert.Equal(-3, hashStudy.HashDecrement(redisKey, "Age", 2)); + Assert.Equal(-3, redisHashStudy.HashDecrement(defaultStudentHashKey, "Age", 2)); } /// @@ -333,15 +332,13 @@ namespace RedisStudyTest [Fact] public void HashDecrementByDoubleTest() { - string redisKey = preHashKey + defaultStudent.Id; - //新字段时,可以用相等比较 - var dec = hashStudy.HashDecrement(redisKey, "Age", 2.1); + var dec = redisHashStudy.HashDecrement(defaultStudentHashKey, "Age", 2.1); var absDec = Math.Abs(-2.1 - dec); Assert.Equal(0, absDec); //已经存在的,浮点数,不使用相等比较,而改用两值相差一定的小范围 - dec = hashStudy.HashDecrement(redisKey, "Age", 2.5); + dec = redisHashStudy.HashDecrement(defaultStudentHashKey, "Age", 2.5); absDec = Math.Abs(-4.6 - dec); Assert.True(absDec < 0.01); } @@ -357,10 +354,8 @@ namespace RedisStudyTest [Fact] public void HashIncrementExceptionTest() { - string redisKey = preHashKey + defaultStudent.Id; - - Assert.True(hashStudy.HashSet(redisKey, "Name", "wanggaofeng")); - Assert.Throws(() => hashStudy.HashIncrement(redisKey, "Name", 1)); + Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, "Name", "wanggaofeng")); + Assert.Throws(() => redisHashStudy.HashIncrement(defaultStudentHashKey, "Name", 1)); } /// @@ -370,11 +365,9 @@ namespace RedisStudyTest [Fact] public void HashIncrementHashKeyTest() { - string redisKey = preHashKey + defaultStudent.Id; - //Key不存在,先创建哈希表,再添加值为0的字段,然后执行自减操作 - Assert.Equal(1, hashStudy.HashIncrement(redisKey, "Id", 1)); - Assert.Equal(1, hashStudy.HashGet(redisKey, "Id")); + Assert.Equal(1, redisHashStudy.HashIncrement(defaultStudentHashKey, "Id", 1)); + Assert.Equal(1, redisHashStudy.HashGet(defaultStudentHashKey, "Id")); } /// @@ -384,11 +377,9 @@ namespace RedisStudyTest [Fact] public void HashIncrementHashFieldTest() { - string redisKey = preHashKey + defaultStudent.Id; - //字段不存在,则创建之 - Assert.Equal(1, hashStudy.HashIncrement(redisKey, "Age", 1)); - Assert.Equal(1, hashStudy.HashGet(redisKey, "Age")); + Assert.Equal(1, redisHashStudy.HashIncrement(defaultStudentHashKey, "Age", 1)); + Assert.Equal(1, redisHashStudy.HashGet(defaultStudentHashKey, "Age")); } /// @@ -397,10 +388,8 @@ namespace RedisStudyTest [Fact] public void HashIncrementMinusTest() { - string redisKey = preHashKey + defaultStudent.Id; - //自增负数时,则自减 - Assert.Equal(-2, hashStudy.HashIncrement(redisKey, "Age", -2)); + Assert.Equal(-2, redisHashStudy.HashIncrement(defaultStudentHashKey, "Age", -2)); } /// @@ -409,12 +398,10 @@ namespace RedisStudyTest [Fact] public void HashIncrementByIntTest() { - string redisKey = preHashKey + defaultStudent.Id; - //字段减少1 - Assert.Equal(1, hashStudy.HashIncrement(redisKey, "Age", 1)); + Assert.Equal(1, redisHashStudy.HashIncrement(defaultStudentHashKey, "Age", 1)); //字段减少2 - Assert.Equal(3, hashStudy.HashIncrement(redisKey, "Age", 2)); + Assert.Equal(3, redisHashStudy.HashIncrement(defaultStudentHashKey, "Age", 2)); } /// @@ -424,15 +411,13 @@ namespace RedisStudyTest [Fact] public void HashIncrementByDoubleTest() { - string redisKey = preHashKey + defaultStudent.Id; - //新字段时,可以用相等比较 - var dec = hashStudy.HashIncrement(redisKey, "Age", 2.1); + var dec = redisHashStudy.HashIncrement(defaultStudentHashKey, "Age", 2.1); var absDec = Math.Abs(2.1 - dec); Assert.Equal(0, absDec); //已经存在的,浮点数,不使用相等比较,而改用两值相差一定的小范围 - dec = hashStudy.HashIncrement(redisKey, "Age", 2.5); + dec = redisHashStudy.HashIncrement(defaultStudentHashKey, "Age", 2.5); absDec = Math.Abs(4.6 - dec); Assert.True(absDec < 0.01); } @@ -445,19 +430,18 @@ namespace RedisStudyTest [Fact] public void HashGetAllTest() { - string redisKey = preHashKey + defaultStudent.Id; var studentEntries = new HashEntry[] { - new HashEntry("Id",1), + new HashEntry("Id",defaultStudent.Id), new HashEntry("Name",defaultStudent.Name), new HashEntry("Age",defaultStudent.Age), }; //插入Sudent - var addHash = hashStudy.HashSet(redisKey, studentEntries); + var addHash = redisHashStudy.HashSet(defaultStudentHashKey, studentEntries); Assert.True(addHash); - var entries = hashStudy.HashGetAll(redisKey); + var entries = redisHashStudy.HashGetAll(defaultStudentHashKey); Assert.NotNull(entries); @@ -481,8 +465,7 @@ namespace RedisStudyTest [Fact] public void HashGetNotKkeyTest() { - string redisKey = preHashKey + defaultStudent.Id; - var result = hashStudy.HashGet(redisKey, "Id"); + var result = redisHashStudy.HashGet(defaultStudentHashKey, "Id"); Assert.False(result.HasValue); } @@ -493,24 +476,39 @@ namespace RedisStudyTest [Fact] public void HashGetNotHashfieldTest() { - string redisKey = preHashKey + defaultStudent.Id; - - hashStudy.HashIncrement(redisKey, "Id", 1); + redisHashStudy.HashIncrement(defaultStudentHashKey, "Id", 1); - var result = hashStudy.HashGet(redisKey, "Name"); + var result = redisHashStudy.HashGet(defaultStudentHashKey, "Name"); Assert.False(result.HasValue); } [Fact] - public void HashGetTest() + public void HashGetOneFieldTest() { - string redisKey = preHashKey + defaultStudent.Id; AddDefaultStudent(); - Assert.Equal(1, hashStudy.HashGet(redisKey, "Id")); - Assert.Equal(defaultStudent.Name, hashStudy.HashGet(redisKey, "Name")); - Assert.Equal(defaultStudent.Age, hashStudy.HashGet(redisKey, "Age")); + Assert.Equal(1, redisHashStudy.HashGet(defaultStudentHashKey, "Id")); + Assert.Equal(defaultStudent.Name, redisHashStudy.HashGet(defaultStudentHashKey, "Name", CommandFlags.None)); + Assert.Equal(defaultStudent.Age, redisHashStudy.HashGet(defaultStudentHashKey, "Age", CommandFlags.None)); + } + + [Fact] + public void HashGetGroupFieldTest() + { + AddDefaultStudent(); + + RedisValue[] hashFields = new RedisValue[] + { + "Id", + "Name", + "Age", + }; + + RedisValue[] resultValues = redisHashStudy.HashGet(defaultStudentHashKey, hashFields, CommandFlags.None); + Assert.Contains(defaultStudent.Id, resultValues); + Assert.Contains(defaultStudent.Name, resultValues); + Assert.Contains(defaultStudent.Age, resultValues); } #endregion @@ -523,7 +521,7 @@ namespace RedisStudyTest [Fact] public void HashKeysNotKeyTest() { - RedisValue[] keys = hashStudy.HashKeys(defaultHashKey); + RedisValue[] keys = redisHashStudy.HashKeys(defaultStudentHashKey); Assert.NotNull(keys); Assert.Empty(keys); @@ -538,7 +536,7 @@ namespace RedisStudyTest { AddDefaultStudent(); - RedisValue[] keys = hashStudy.HashKeys(defaultHashKey); + RedisValue[] keys = redisHashStudy.HashKeys(defaultStudentHashKey); Assert.NotEmpty(keys); Assert.Contains("Id", keys); @@ -554,7 +552,7 @@ namespace RedisStudyTest [Fact] public void HashValuesNotKeyTest() { - RedisValue[] keys = hashStudy.HashValues(defaultHashKey); + RedisValue[] keys = redisHashStudy.HashValues(defaultStudentHashKey); Assert.NotNull(keys); Assert.Empty(keys); @@ -569,7 +567,7 @@ namespace RedisStudyTest { AddDefaultStudent(); - RedisValue[] values = hashStudy.HashValues(defaultHashKey); + RedisValue[] values = redisHashStudy.HashValues(defaultStudentHashKey); Assert.NotEmpty(values); Assert.Contains(defaultStudent.Id, values); @@ -587,8 +585,7 @@ namespace RedisStudyTest [Fact] public void HashLengthNotKeyTest() { - string redisKey = preHashKey + defaultStudent.Id; - var result = hashStudy.HashLength(redisKey); + var result = redisHashStudy.HashLength(defaultStudentHashKey); Assert.Equal(0, result); } @@ -599,10 +596,9 @@ namespace RedisStudyTest [Fact] public void HashLengthTest() { - string redisKey = preHashKey + defaultStudent.Id; AddDefaultStudent(); - var result = hashStudy.HashLength(redisKey); + var result = redisHashStudy.HashLength(defaultStudentHashKey); Assert.Equal(3, result); } @@ -615,9 +611,9 @@ namespace RedisStudyTest /// 哈希不存在时,HashScan /// [Fact] - public void HashScanNotKey() + public void HashScanNotKeyTest() { - var hashEntrys= hashStudy.HashScan(defaultHashKey); + var hashEntrys = redisHashStudy.HashScan(defaultStudentHashKey, "*", 20, CommandFlags.None); var num = hashEntrys.Count(); Assert.Equal(0, num); @@ -626,6 +622,48 @@ namespace RedisStudyTest Assert.Empty(entryList); } + + /// + /// 简单测试,有单独测试类详细测试 + /// + [Fact] + public void HashScanTest() + { + //插入多列hash + List hashEntries = new List(); + for (int i=1;i<=100; i++) + { + hashEntries.Add(new HashEntry("Field"+i,i)); + } + redisHashStudy.HashSet(defaultStudentHashKey, hashEntries.ToArray()); + + var hashFieldScan = redisHashStudy.HashScan(defaultStudentHashKey); + + Assert.Equal(100, hashFieldScan.Count()); + + var scanTake = hashFieldScan.Skip(10).Take(10).ToList(); + Assert.Equal(10, scanTake.Count()); + + //删除hash + redisDatabase.KeyDelete(defaultStudentHashKey); + + } + + [Fact] + public void HashScan2Test() + { + //插入多列hash + List hashEntries = new List(); + for (int i = 1; i <= 100; i++) + { + hashEntries.Add(new HashEntry("Field" + i, i)); + } + redisHashStudy.HashSet(defaultStudentHashKey, hashEntries.ToArray()); + + var hashFieldScan = redisHashStudy.HashScan(defaultStudentHashKey, "*", 20, 0, 0, CommandFlags.None); + + Assert.Equal(100, hashFieldScan.Count()); + } #endregion #region HashExists @@ -635,20 +673,19 @@ namespace RedisStudyTest [Fact] public void HashExists() { - string redisKey = preHashKey + defaultStudent.Id; var studentEntries = new HashEntry[] { - new HashEntry("Id",1), + new HashEntry("Id",defaultStudent.Id), new HashEntry("Name",defaultStudent.Name), }; //插入Sudent - var addHash = hashStudy.HashSet(redisKey, studentEntries); + var addHash = redisHashStudy.HashSet(defaultStudentHashKey, studentEntries); Assert.True(addHash); - Assert.True(hashStudy.HashExists(redisKey, "Id")); - Assert.True(hashStudy.HashExists(redisKey, "Name")); - Assert.False(hashStudy.HashExists(redisKey, "Age")); + Assert.True(redisHashStudy.HashExists(defaultStudentHashKey, "Id")); + Assert.True(redisHashStudy.HashExists(defaultStudentHashKey, "Name")); + Assert.False(redisHashStudy.HashExists(defaultStudentHashKey, "Age")); } #endregion @@ -660,8 +697,7 @@ namespace RedisStudyTest [Fact] public void HashDeleteNotKeyTest() { - string redisKey = preHashKey + defaultStudent.Id; - var hashDeleteResult = hashStudy.HashDelete(redisKey, "FieldFirst"); + var hashDeleteResult = redisHashStudy.HashDelete(defaultStudentHashKey, "FieldFirst"); Assert.False(hashDeleteResult); } @@ -672,31 +708,47 @@ namespace RedisStudyTest [Fact] public void HashDeleteNotFieldTest() { - string redisKey = preHashKey + defaultStudent.Id; - hashStudy.HashIncrement(redisKey, "Id",1); + redisHashStudy.HashIncrement(defaultStudentHashKey, "Id",1); - var hashDeleteResult = hashStudy.HashDelete(redisKey, "Name"); + var hashDeleteResult = redisHashStudy.HashDelete(defaultStudentHashKey, "Name"); Assert.False(hashDeleteResult); } /// - /// 删除哈希字段 + /// 删除哈希一个字段 /// [Fact] public void HashDeleteTest() { - string redisKey = preHashKey + defaultStudent.Id; AddDefaultStudent(); - var hashDeleteResult = hashStudy.HashDelete(redisKey, "Id"); + var hashDeleteResult = redisHashStudy.HashDelete(defaultStudentHashKey, "Id"); Assert.True(hashDeleteResult); - hashDeleteResult = hashStudy.HashDelete(redisKey, "Name"); + hashDeleteResult = redisHashStudy.HashDelete(defaultStudentHashKey, "Name", CommandFlags.None); Assert.True(hashDeleteResult); - hashDeleteResult = hashStudy.HashDelete(redisKey, "Age"); + hashDeleteResult = redisHashStudy.HashDelete(defaultStudentHashKey, "Age", CommandFlags.None); Assert.True(hashDeleteResult); } + + /// + /// 删除一组哈希字段 + /// + [Fact] + public void HashDeleteGroupFieldTest() + { + AddDefaultStudent(); + + RedisValue[] delValues = new RedisValue[] + { + "Id", + "Name", + "Age", + }; + var hashDeleteResult = redisHashStudy.HashDelete(defaultStudentHashKey, delValues, CommandFlags.None); + Assert.Equal(3,hashDeleteResult); + } #endregion #region HashKeyDelete @@ -707,7 +759,7 @@ namespace RedisStudyTest [Fact] public void KeyDeleteTest() { - Assert.False(redisDatabase.KeyDelete(preHashKey + "-2000")); + Assert.False(redisDatabase.KeyDelete(preStudentHashKey + "-2000")); } #endregion @@ -728,22 +780,21 @@ namespace RedisStudyTest { if (defaultStudent != null) { - redisDatabase.KeyDelete(preHashKey + defaultStudent.Id); + redisDatabase.KeyDelete(preStudentHashKey + defaultStudent.Id); } } private void AddDefaultStudent() { - string redisKey = preHashKey + defaultStudent.Id; var studentEntries = new HashEntry[] { - new HashEntry("Id",1), + new HashEntry("Id",defaultStudent.Id), new HashEntry("Name",defaultStudent.Name), new HashEntry("Age",defaultStudent.Age), }; //插入Sudent - var addHash = hashStudy.HashSet(redisKey, studentEntries); + var addHash = redisHashStudy.HashSet(defaultStudentHashKey, studentEntries); } #endregion } diff --git a/RedisStudyTest/RedisLockStudyTest.cs b/RedisStudyTest/RedisLockStudyTest.cs index 5b1c87d..fb602a3 100644 --- a/RedisStudyTest/RedisLockStudyTest.cs +++ b/RedisStudyTest/RedisLockStudyTest.cs @@ -13,6 +13,7 @@ using Xunit; namespace RedisStudyTest { + [Trait("RedisLock", "All")] public class RedisLockStudyTest { private IDatabase redisDatabase; diff --git a/RedisStudyTest/RedisServerStudyTest.cs b/RedisStudyTest/RedisServerStudyTest.cs index d0dd741..02522c3 100644 --- a/RedisStudyTest/RedisServerStudyTest.cs +++ b/RedisStudyTest/RedisServerStudyTest.cs @@ -12,6 +12,7 @@ using Xunit; namespace RedisStudyTest { + [Trait("RedisServer", "All")] public class RedisServerStudyTest { private IServer redisServer; diff --git a/RedisStudyTest/RedisSortSetStudyTest.cs b/RedisStudyTest/RedisSortSetStudyTest.cs index 45453a2..ef23cbf 100644 --- a/RedisStudyTest/RedisSortSetStudyTest.cs +++ b/RedisStudyTest/RedisSortSetStudyTest.cs @@ -14,6 +14,7 @@ using RedisStuy; namespace RedisStudyTest { + [Trait("RedisSortSet", "All")] public class RedisSortSetStudyTest : IDisposable { private IDatabase redisDatabase = null; diff --git a/RedisStuy/RedisHashStudy.cs b/RedisStuy/RedisHashStudy.cs index 76b7f5d..d35b9d4 100644 --- a/RedisStuy/RedisHashStudy.cs +++ b/RedisStuy/RedisHashStudy.cs @@ -167,23 +167,10 @@ namespace RedisStuy throw new ArgumentNullException("hashFields", "参数hashFields.Length "); } - - bool result = false; - try - { - redisDatabase.HashSet(key, hashFields, flags); - result = true; - } - catch (ArgumentNullException argumentNullException) - { - throw argumentNullException; - } - catch (Exception baseException) - { - throw baseException; - } - return result; + redisDatabase.HashSet(key, hashFields, flags); + + return true; } /// diff --git a/RedisStuy/RedisScanStudy.cs b/RedisStuy/RedisScanStudy.cs new file mode 100644 index 0000000..97b46d0 --- /dev/null +++ b/RedisStuy/RedisScanStudy.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RedisStuy +{ + /// + /// Scan命令学习:包括Scan SScan HScan ZScan + /// Scan命令比较特殊,并且此组命令相似,所以放在一起单独学习 + /// + public class RedisScanStudy + { + + } +} diff --git a/RedisStuy/RedisStuy.csproj b/RedisStuy/RedisStuy.csproj index 46c8600..3ceb585 100644 --- a/RedisStuy/RedisStuy.csproj +++ b/RedisStuy/RedisStuy.csproj @@ -50,6 +50,7 @@ +