The reading-cloud-book service provides book details, chapters, and reading interfaces.
Chapter Reading Logic
To optimize performance, chapter navigation (previous/next chapter) is implemented using a doubly linked list structure stored in Redis (Hash type). This avoids expensive database calculations for every page turn.
Data Structure Model:
Each entry in the Redis Hash uses the chapter ID as the key. The value contains the chapter details and pointers to the previous (pre) and next (next) chapters.
[
{
"key":"519",
"value":{
"id":529,
"name":"第一章 装B的乞丐",
"pre":null,
"next":[
{
"id":530,
"name":"第二章 资格"
}
]
}
},
{
"key":"530",
"value":{
"id":530,
"name":"第二章 资格",
"pre":[
{
"id":529,
"name":"第一章 装B的乞丐"
}
],
"next":[
{
"id":531,
"name":"第三章 开始修炼清心诀"
}
]
}
}
]
Workflow:
- Request chapter via
book/chapter/readChapter. - Check Redis cache for the chapter node data.
- If cache miss: Query the database, calculate the full linked list for the book, and store the result in the Redis Hash.
- If cache hit: Return the data directly from Redis (O(1) complexity).
[
{
"key":"519",
"value":{
"id":529,
"name":"第一章 装B的乞丐",
"pre":null,
"next":[
{
"id":530,
"name":"第二章 资格"
}
]
}
}
]