You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
175 lines
5.0 KiB
C#
175 lines
5.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
using RedisStudyModel;
|
|
|
|
using StackExchange.Redis;
|
|
|
|
namespace RedisStuy
|
|
{
|
|
/// <summary>
|
|
/// Redis Hash 学习
|
|
/// </summary>
|
|
public class RedisHashStudy
|
|
{
|
|
private string studentHashKey = "student:hask:";
|
|
private IDatabase redisDatabase = RedisHelper.GetRedisDatabase();
|
|
|
|
/// <summary>
|
|
/// 添加一个学生
|
|
/// </summary>
|
|
public bool AddStudent(Student student)
|
|
{
|
|
try
|
|
{
|
|
HashEntry[] hashEntries = new HashEntry[]
|
|
{
|
|
new HashEntry("Id",student.Id),
|
|
new HashEntry("Name",student.Name),
|
|
new HashEntry("Age",student.Age),
|
|
};
|
|
|
|
redisDatabase.HashSet(studentHashKey + student.Id, hashEntries, CommandFlags.None);
|
|
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加多个学生
|
|
/// </summary>
|
|
public bool AddStudents(List<Student> students)
|
|
{
|
|
try
|
|
{
|
|
foreach (var student in students)
|
|
{
|
|
AddStudent(student);
|
|
}
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除一个学生
|
|
/// </summary>
|
|
public bool DelStudentById(int studentId)
|
|
{
|
|
return redisDatabase.KeyDelete(studentHashKey + studentId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询一个学生
|
|
/// </summary>
|
|
public Student QueryOneStudent(int studentId)
|
|
{
|
|
Student student = null;
|
|
|
|
var studentHashEntryList = redisDatabase.HashGetAll(studentHashKey + studentId);
|
|
if (studentHashEntryList.Length > 0)
|
|
{
|
|
student = new Student();
|
|
foreach (var temp in studentHashEntryList)
|
|
{
|
|
switch (temp.Name)
|
|
{
|
|
case "Id":
|
|
student.Id = (int)temp.Value;
|
|
break;
|
|
case "Name":
|
|
student.Name = temp.Value.HasValue ? temp.Value.ToString() : string.Empty;
|
|
break;
|
|
case "Age":
|
|
student.Age = (int)temp.Value;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return student;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询一个学生
|
|
/// </summary>
|
|
public Student QueryOneStudent(string redisKey)
|
|
{
|
|
Student student = null;
|
|
|
|
var studentHashEntryList = redisDatabase.HashGetAll(redisKey);
|
|
if (studentHashEntryList.Length > 0)
|
|
{
|
|
student = new Student();
|
|
foreach (var temp in studentHashEntryList)
|
|
{
|
|
switch (temp.Name)
|
|
{
|
|
case "Id":
|
|
student.Id = (int)temp.Value;
|
|
break;
|
|
case "Name":
|
|
student.Name = temp.Value.HasValue ? temp.Value.ToString() : string.Empty;
|
|
break;
|
|
case "Age":
|
|
student.Age = (int)temp.Value;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return student;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询 所有学生
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<Student> QueryAllStudents()
|
|
{
|
|
List<Student> students = new List<Student>();
|
|
|
|
var studdentKeys = RedisHelper.GetDefaultRedisServer().Keys(1, studentHashKey + "*", 200);
|
|
foreach (var key in studdentKeys)
|
|
{
|
|
students.Add(QueryOneStudent(key));
|
|
}
|
|
|
|
return students;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询某项的值
|
|
/// </summary>
|
|
public void GetValue(int studentId, string haskFiledName)
|
|
{
|
|
var redisValue = redisDatabase.HashGet(studentHashKey + studentId, haskFiledName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新一列的值
|
|
/// </summary>
|
|
public void UpdateHashFile<T>(int studentId, string haskFiledName, RedisValue redisValue)
|
|
{
|
|
redisDatabase.HashSet(studentHashKey + studentId, haskFiledName, redisValue);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 学生是否存在
|
|
/// </summary>
|
|
public bool ExistStudent(int studentId)
|
|
{
|
|
bool exists = redisDatabase.KeyExists(studentHashKey + studentId);
|
|
return exists;
|
|
}
|
|
}
|
|
}
|