Hash 测试

master
ruyu 7 years ago
parent a5a582ce29
commit 3fc5019c0f

@ -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<Student> students;
private Student student = null;
private string preHashKey = "RedisStudy:Student:";
/// <summary>
/// 构造
/// </summary>
public RedisHashStudyTest()
{
redisDb = RedisHelper.GetRedisDatabase();
redisDatabase = RedisHelper.GetRedisDatabase();
hashStudy = new RedisHashStudy();
student = new Student()
{
Id = 1,
Name = "王高峰",
Age = 2 * 9
};
students = new List<Student>()
{
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<ArgumentNullException>(() => hashStudy.HashSet(string.Empty, null));
Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet("", null));
Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet(preHashKey + "-1", null));
Assert.Throws<ArgumentNullException>(() => 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
/// </summary>
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<HashEntry>()
{
new HashEntry("Id",student.Id),
new HashEntry("Name",student.Name),
new HashEntry("Age",student.Age),
};
return entrys.ToArray();
}
#endregion
}
}

@ -13,6 +13,9 @@ namespace RedisStuy
/// <summary>
/// Redis Hash 学习
/// </summary>
/// <remarks>
/// http://www.redis.net.cn
/// </remarks>
public class RedisHashStudy
{
#region 初始化
@ -146,9 +149,40 @@ namespace RedisStuy
/// 此命令会覆盖哈希表中已存在的字段。
/// 如果哈希表不存在,会创建一个空哈希表,并执行 HMSET 操作。
/// </summary>
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;
}
/// <summary>

Loading…
Cancel
Save