|
|
|
@ -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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|