# DB Cache 活用法

## 混用 ProudNet DB cache系統和 ADO

DB cache系統和ADO Wrapper API可以混合使用。 但是，當用戶直接處理[**服務器和客戶端**](/proudnet.cn/proudnet/db_system/db_cache_ver2/server_client.md)獨家訪問的表時，應注意以下幾點。

> **讀表**
>
> 在 <mark style="color:orange;">Proud.CDbCacheServer2</mark> 中緩存的數據可能尚未在 DBMS 中 flush，因此獲得的數據信息可能是過去的數據，而不是實際狀態。\
> \
> **寫入表格**\
> 在 <mark style="color:orange;">Proud.CDbCacheServer2</mark> 中斷斷續續地寫入這些表 。 因此，如果在這些表中記錄某些東西， 可能會與 <mark style="color:orange;">Proud.CDbCacheServer2</mark> 中記錄的數據重合。 因此，我們不建議記錄在這些桌子上。

{% hint style="success" %}
**參考**\
[**DB直接存取DB cache所處理的數據**](/proudnet.cn/proudnet/db_system/db_cache_ver2/theory.md#db-cache-db)
{% endhint %}

## 調整DB cache定位

如果將緩存DB信息寫入DBMS的週期縮短,以降低DBMS的性能爲代價,DB緩存服務器異常終止,減少未來得及保存的數據丟失的危險。

相關方法爲<mark style="color:orange;">Proud.CDbCacheServer2.SetDbmsWriteIntervalMs</mark>。

在DB cache client中使用數據並進行unload時,該玩家信息會留在DB cache server存儲器中,過一段時間後,DB cache server的存儲器中也會消失。

如果縮短這一段時間，DB cache server的使用內存就會增加，但DBMS的負載會減少，因爲DBMS的數據加載次數會減少。

相關方法爲<mark style="color:orange;">Proud.CDbCacheServer2.SetUnloadedDataHibernateDurationMs</mark>。

## 載入 DB cache部分

加載的玩家角色中可能只有關於一個角色的信息需要加載。 利用ProudNet DB 緩存系統的功能可實現部分加載。<br>

> **例子**\
> \* RootTable 是 Gamer 和 CharacterInfo 表。 \
> \* ChildTable是Hero下的一個Item，是Gamer下CharacterInfo下的一個Item。 \
> \* Hero 中至少有一個使用者定義欄位指向CharacterInfo 中的一個欄位。

您可以使用[**裝入獨家數據並添加新數據方法**](/proudnet.cn/proudnet/db_system/db_cache_ver2/using.md#undefined)來載入玩家並根據使用者選擇的角色載入<mark style="color:orange;">CharacterInfo</mark>。<br>

下面是大致的代碼。

```cpp
Proud::Guid outSessionGuid;
Proud::CDbCacheClient2* c = ...;
c->RequestExclusiveLoadData(L"Gamer",L"GamerID",gamername,outSessionGuid);
//通過這個請求接收loadedGamerData。
 
...//用戶選擇角色，在loadedGamerData中找到用戶選擇的角色。
 
c->RequestExclusiveLoadData(L"CharacterInfo",L"HeroIndex",findCharacter->heroIndex,outSessionGuid); 
//加載用戶挑選的角色信息樹。
```

## 從 DB cache ver.1 遷移

<table><thead><tr><th width="300">項目</th><th width="66" align="center">ver 1</th><th width="69" align="center">ver 2</th><th>應付方法</th></tr></thead><tbody><tr><td>Credential 發放</td><td align="center">有</td><td align="center">無</td><td>必須直接實現Credential簽發</td></tr><tr><td>由DB cache認證玩家回撥</td><td align="center">有</td><td align="center">無</td><td>玩家需要直接呼叫而不是認證的回撥</td></tr></tbody></table>

> 在**DB cache ver 1**中
>
> * 它涵蓋了 Gamer、 Hero 和 WorldObject 表，您需要指定 <mark style="color:orange;">OwnerUUID</mark>, <mark style="color:orange;">RootUUID</mark>, <mark style="color:orange;">ParentUUID</mark> 來移動它們。
> * 它涉及CLoadedHeroData、CLoadedItemData和CLoadedGamerData，它們必須涉及<mark style="color:orange;">CLoadedData</mark>。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.proudnet.com/proudnet.cn/proudnet/db_system/db_cache_ver2/using/usage.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
