我没有使用Redis的经验,也不知道如何开始使用它。我试图解释。例如,我有数据库表:
employees
id primary key
name
department_id
我对此表有REST API:
所以我决定将我的员工按ID缓存在Redis中:
REST API | REDIS commands
---------------------------------------------------------
GetById(id) | trying to GET("empl_by_id:" + id); if not exists get employee from database and SET("empl_by_id:" + id, employee)
Add(employee) | SET("empl_by_id:" + employee.id, employee)
Update(employee) | SET("empl_by_id:" + employee.id, employee)
Delete(employee) | DELETE("empl_by_id:" + employee.id)
现在一切都很好,但是现在我需要通过department_id聘用员工。我可以尝试使用相同的策略:
REST API | REDIS commands
---------------------------------------------------------
GetByDepratmentId(dep_id) | trying to GET("empl_by_dep:" + dep_id); if not exists get employees from database and SET("empl_by_dep:" + dep_id, employees)
但是现在我意识到,如果我要添加新的Employee,那么执行SET(“ empl_by_id:” + employee.id,employee)是不够的。我需要通过“ empl_by_dep:” + employee.department_id键更改值。
删除员工时,仅执行DELETE(“ empl_by_id:” + employee.id)是不够的。我需要从数据库或缓存中读取该员工,检查department_id并通过“ empl_by_dep:” + employee.department_id键更新值。
依此类推....
有没有解决方案,模式,好的文章可供阅读,或者有任何好的关键字可以谷歌解决方案。