diff --git a/RedisStudyTest/RedisHashStudyTest.cs b/RedisStudyTest/RedisHashStudyTest.cs index 744f02b..c9791be 100644 --- a/RedisStudyTest/RedisHashStudyTest.cs +++ b/RedisStudyTest/RedisHashStudyTest.cs @@ -16,26 +16,19 @@ namespace RedisStudyTest { public class RedisHashStudyTest : IDisposable { - private IDatabase redisDb = null; + private IDatabase redisDatabase = null; private RedisHashStudy hashStudy = null; - private Student student; private List students; + private Student student = null; + private string preHashKey = "RedisStudy:Student:"; /// /// 构造 /// public RedisHashStudyTest() { - redisDb = RedisHelper.GetRedisDatabase(); + redisDatabase = RedisHelper.GetRedisDatabase(); hashStudy = new RedisHashStudy(); - - student = new Student() - { - Id = 1, - Name = "王高峰", - Age = 2 * 9 - }; - students = new List() { new Student() @@ -76,13 +69,44 @@ namespace RedisStudyTest [Fact] public void AddStudentTest() { - //hashStudy.AddStudent(student); + student = new Student() + { + Id = 1, + Name = "王高峰", + Age = 2 * 9 + }; + string redisKey = preHashKey + student.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[] { })); - //var newStudent = hashStudy.QueryOneStudent(student.Id); - //Assert.NotNull(newStudent); - //Assert.Equal(1, newStudent.Id); - //Assert.Equal("王高峰", newStudent.Name); - //Assert.Equal(18, actual: newStudent.Age); + + var studentEntries = new HashEntry[] + { + new HashEntry("Id",1), + new HashEntry("Name",student.Name), + new HashEntry("Age",student.Age), + }; + + //插入Sudent + var addHash = hashStudy.HashSet(redisKey, studentEntries, CommandFlags.None); + //设置过期 + redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(5)); + Assert.True(addHash); + + //更新(插入)一项 + + var addOrUpdateOne = hashStudy.HashSet(redisKey, "Id",student.Id+1); + Assert.True(addOrUpdateOne); + + addOrUpdateOne = hashStudy.HashSet(redisKey, "Name", student.Name + 1); + Assert.True(addOrUpdateOne); + + addOrUpdateOne = hashStudy.HashSet(redisKey, "Age", student.Age + 1); + Assert.True(addOrUpdateOne); } [Theory] @@ -144,11 +168,33 @@ namespace RedisStudyTest /// public void Dispose() { + if (student != null) + { + redisDatabase.KeyDelete(preHashKey + student.Id); + } //hashStudy.DelStudentById(student.Id); //foreach (var temp in students) //{ // hashStudy.DelStudentById(temp.Id); //} } + + #region 辅助方法 + public HashEntry[] StudentsToHashEntrys(Student student) + { + if (student == null) + { + return null; + } + + var entrys = new List() + { + new HashEntry("Id",student.Id), + new HashEntry("Name",student.Name), + new HashEntry("Age",student.Age), + }; + return entrys.ToArray(); + } + #endregion } } diff --git a/RedisStuy/RedisHashStudy.cs b/RedisStuy/RedisHashStudy.cs index 3887f70..7533b0e 100644 --- a/RedisStuy/RedisHashStudy.cs +++ b/RedisStuy/RedisHashStudy.cs @@ -13,6 +13,9 @@ namespace RedisStuy /// /// Redis Hash 学习 /// + /// + /// http://www.redis.net.cn + /// public class RedisHashStudy { #region 初始化 @@ -146,9 +149,40 @@ namespace RedisStuy /// 此命令会覆盖哈希表中已存在的字段。 /// 如果哈希表不存在,会创建一个空哈希表,并执行 HMSET 操作。 /// - public void HashSet(RedisKey key, HashEntry[] hashFields, CommandFlags flags = CommandFlags.None) + public bool HashSet(RedisKey key, HashEntry[] hashFields, CommandFlags flags = CommandFlags.None) { + if (string.IsNullOrEmpty(key)) + { + throw new ArgumentNullException("key", "参数 key, 不能为null值或空串"); + } + + if (hashFields == null) + { + throw new ArgumentNullException("hashFields", "参数 hashFields, 不能为null值"); + } + + if (hashFields.Length <= 0) + { + throw new ArgumentNullException("hashFields", "参数hashFields.Length "); + } + redisDatabase.HashSet(key, hashFields, flags); + bool result = false; + try + { + + result = true; + } + catch (ArgumentNullException argumentNullException) + { + throw argumentNullException; + } + catch (Exception baseException) + { + throw baseException; + } + + return result; } ///