chore: add diagram

main
sudo pacman -Syu 2023-07-30 12:16:18 +07:00
parent 286d21a723
commit 3ad375308d
2 changed files with 57 additions and 0 deletions

View File

@ -46,6 +46,34 @@
re-write to cache re-write to cache
</li> </li>
</ul> </ul>
<div class="highlight highlight-source-mermaid">
<pre><span class="pl-k">sequenceDiagram</span>
<span class="pl-k">participant</span> <span class="pl-ent">other</span>
<span class="pl-k">participant</span> <span class="pl-ent">service</span>
<span class="pl-k">participant</span> <span class="pl-ent">cache</span>
<span class="pl-k">participant</span> <span class="pl-ent">db</span>
<span class="pl-ent">other </span><span class="pl-k">-&gt;&gt;</span> <span class="pl-ent">service</span><span class="pl-k">:</span> <span class="pl-s">get data</span>
<span class="pl-k">activate</span> <span class="pl-ent">service</span>
<span class="pl-ent">service </span><span class="pl-k">-&gt;&gt;</span> <span class="pl-ent">cache</span><span class="pl-k">:</span> <span class="pl-s">get data</span>
<span class="pl-k">alt</span> <span class="pl-s">exist in cache</span>
<span class="pl-ent">service </span><span class="pl-k">--&gt;&gt;</span> <span class="pl-ent">other</span><span class="pl-k">:</span> <span class="pl-s">return data</span>
<span class="pl-k">else</span> <span class="pl-s">not exist in cache</span>
<span class="pl-ent">service </span><span class="pl-k">-&gt;&gt;</span> <span class="pl-ent">db</span><span class="pl-k">:</span> <span class="pl-s">get data</span>
<span class="pl-k">alt</span> <span class="pl-s">exist data in db</span>
<span class="pl-ent">service </span><span class="pl-k">--&gt;&gt;</span> <span class="pl-ent">other</span><span class="pl-k">:</span> <span class="pl-s">return data</span>
<span class="pl-k">else</span> <span class="pl-s">not exist data in db</span>
<span class="pl-ent">service </span><span class="pl-k">--&gt;&gt;</span> <span class="pl-ent">other</span><span class="pl-k">:</span> <span class="pl-s">return error not found</span>
<span class="pl-k">end</span>
<span class="pl-k">end</span>
<span class="pl-k">deactivate</span> <span class="pl-ent">service</span>
<span class="pl-ent">other </span><span class="pl-k">-&gt;&gt;</span> <span class="pl-ent">service</span><span class="pl-k">:</span> <span class="pl-s">set data</span>
<span class="pl-k">activate</span> <span class="pl-ent">service</span>
<span class="pl-ent">service </span><span class="pl-k">-&gt;&gt;</span> <span class="pl-ent">db</span><span class="pl-k">:</span> <span class="pl-s">set data</span>
<span class="pl-ent">service </span><span class="pl-k">-&gt;&gt;</span> <span class="pl-ent">cache</span><span class="pl-k">:</span> <span class="pl-s">set data</span>
<span class="pl-k">deactivate</span> <span class="pl-ent">service</span></pre>
</div>
<p>It's good for general cases, for example with CRUD action.</p> <p>It's good for general cases, for example with CRUD action.</p>
<p> <p>
The bad things happen when cache and database are not consistent. For The bad things happen when cache and database are not consistent. For

View File

@ -7,6 +7,35 @@ My default strategy is:
- Write to database first then to cache second - Write to database first then to cache second
- Read from cache first, if not found then read from database second, then re-write to cache - Read from cache first, if not found then read from database second, then re-write to cache
```mermaid
sequenceDiagram
participant other
participant service
participant cache
participant db
other ->> service: get data
activate service
service ->> cache: get data
alt exist in cache
service -->> other: return data
else not exist in cache
service ->> db: get data
alt exist data in db
service -->> other: return data
else not exist data in db
service -->> other: return error not found
end
end
deactivate service
other ->> service: set data
activate service
service ->> db: set data
service ->> cache: set data
deactivate service
```
It's good for general cases, for example with CRUD action. It's good for general cases, for example with CRUD action.
The bad things happen when cache and database are not consistent. The bad things happen when cache and database are not consistent.