# DB Cache usage and application

## How to load proprietary data and add new data

The [**proprietary load**](https://docs.proudnet.com/proudnet.eng/proudnet/db_system/theory#exclusive-load) method is <mark style="color:orange;">Proud.CDbCacheClient2.RequestExclusiveLoadData</mark>. To search for data to load, you must enter search conditions in the arguments <mark style="color:orange;">fieldName</mark> and <mark style="color:orange;">cmpValue</mark>.<br>

This is a usage example.

```cpp
Proud::Guid outSessetionGuid;
Proud::CDbCacheClient2* c = ...;
c->RequestExclusiveLoadData(L"Gamer",L"GamerID",gamername,outSessetionGuid);
```

The <mark style="color:orange;">Proud.CDbCacheClient2.RequestExclusiveLoadNewData</mark> method is similarly a proprietary load function, but it is used when you add data to RootTable and write it right away.<br>

This is a usage example.

```cpp
CPropNodePtr newNode = CPropNodePtr(new CPropNode(L"Gamer"));
 
newNode->Fields[L"GamerID"] = "Ulelio";
 
Proud::Guid outSessetionGuid;
Proud::CDbCacheClient2* c = ...;
c->RequestExclusiveLoadNewData(L"Gamer", newNode, outSessetionGuid);
```

## Making unilateral data changes

> * Proud.CDbCacheClient2.UnilateralAddData
> * Proud.CDbCacheClient2.UnilateralUpdateData
> * Proud.CDbCacheClient2.UnilateralRemoveData

These methods return immediately upon call because they reflect the change in DB cache memory and later do the actual writing to the DBMS asynchronously. They also have no response method callbacks.

## Use of request-responsive and blocked methods that are reflected in DBMS first

Using request-responsive methods and blocked methods, which are reflected in the DBMS first, are methods for [**request-responsive data changes**](https://docs.proudnet.com/proudnet.eng/proudnet/db_system/theory#id-2-changing-request-responsive-data).\
Returns immediately upon call, and changes are only reflected in the DB cache's memory after they have been written to the actual DBMS.

<table data-full-width="false"><thead><tr><th width="294">Calling methods</th><th>Result callback</th></tr></thead><tbody><tr><td>Proud.CDbCacheClient2.RequestAddData</td><td>Proud.IDbCacheClientDelegate2.OnAddDataFailed</td></tr><tr><td></td><td>Proud.IDbCacheClientDelegate2.OnAddDataSuccess</td></tr><tr><td>Proud.CDbCacheClient2.RequestUpdateData</td><td>Proud.IDbCacheClientDelegate2.OnUpdateDataFailed</td></tr><tr><td></td><td>Proud.IDbCacheClientDelegate2.OnUpdateDataSuccess</td></tr><tr><td>Proud.CDbCacheClient2.RequestRemoveData</td><td>Proud.IDbCacheClientDelegate2.OnRemoveDataFailed</td></tr><tr><td></td><td>Proud.IDbCacheClientDelegate2.OnRemoveDataSuccess</td></tr></tbody></table>

If you want a blocked call rather than a request-responsive one, use these methods.

> * Proud.CDbCacheClient2.BlockedAddData
> * Proud.CDbCacheClient2.BlockedUpdateData
> * Proud.CDbCacheClient2.BlockedRemoveData

## Request-responsive SessionGuid

The proprietary clients will have one session each, and the DB Cache client will use a Guid value to distinguish between them.\
When requesting, the SessionGuid value is issued as an Output parameter, and the value issued as <mark style="color:orange;">CCallbackArgs::m\_sessionGuid</mark> of the response <mark style="color:orange;">Event</mark> is received. You can check the requested session by saving and comparing <mark style="color:orange;">outSessionGuid</mark>.

**Request**

```
CDbCacheClient2::RequestExclusiveLoadNewData(String rootTableName, CPropNodePtr addData, Guid &outSessionGuid);
```

**Response**

```
IDbCacheClientDelegate2::OnExclusiveLoadDataFailed(CCallbackArgs args)
```

{% hint style="info" %}
The <mark style="color:orange;">outSessionGuid</mark> of the **request** is the same as the <mark style="color:orange;">args.m\_sessionGuid</mark> of the **response**.
{% endhint %}

## Non-proprietary data access methods

You must set <mark style="color:orange;">Proud.CDbCacheServer2StartParameter.m\_allowNonExclusiveAccess</mark> to allow non-proprietary access.

<table data-full-width="false"><thead><tr><th width="397">Calling methods</th><th>Result callback</th></tr></thead><tbody><tr><td>Proud.CDbCacheClient2.RequestNonExclusiveSnapshotData</td><td>Proud.IDbCacheClientDelegate2.OnNonExclusiveSnapshotDataFailed</td></tr><tr><td></td><td>Proud.IDbCacheClientDelegate2.OnNonExclusiveSnapshotDataSuccess</td></tr><tr><td>Proud.CDbCacheClient2.RequestNonExclusiveAddData</td><td>Proud.IDbCacheClientDelegate2.OnNonExclusiveAddDataAck</td></tr><tr><td>Proud.CDbCacheClient2.RequestNonExclusiveRemoveData</td><td>Proud.IDbCacheClientDelegate2.OnNonExclusiveRemoveDataAck</td></tr><tr><td>Proud.CDbCacheClient2.RequestNonExclusiveSetValueIf</td><td>Proud.IDbCacheClientDelegate2.OnNonExclusiveSetValueIfFailed</td></tr><tr><td></td><td>Proud.IDbCacheClientDelegate2.OnNonExclusiveSetValueIfSuccess</td></tr><tr><td>Proud.CDbCacheClient2.RequestNonExclusiveModifyValue</td><td>Proud.IDbCacheClientDelegate2.OnNonExclusiveModifyValueFailed</td></tr><tr><td></td><td>Proud.IDbCacheClientDelegate2.OnNonExclusiveModifyValueSuccess</td></tr></tbody></table>

When a non-proprietary data change is successful, the change is reported to the DB cache client that was loading the data exclusively. The following methods are called for reporting.

> * Proud.IDbCacheClientDelegate2.OnSomeoneAddData
> * Proud.IDbCacheClientDelegate2.OnSomeoneRemoveData
> * Proud.IDbCacheClientDelegate2.OnSomeoneModifyValue
> * Proud.IDbCacheClientDelegate2.OnSomeoneSetValue

To make it easier to view, each request and response relationship is grouped together as follows.

<mark style="color:orange;">Proud.CDbCacheClient2.RequestNonExclusiveSnapshotData</mark> : Get a snapshot

```cpp
// Response:
IDbCacheClientDelegate2::OnNonExclusiveSnapshotDataSuccess (CCallbackArgs args)
IDbCacheClientDelegate2::OnNonExclusiveSnapshotDataFailed (CCallbackArgs args)
```

<mark style="color:orange;">Proud.CDbCacheClient2.RequestNonExclusiveAddData</mark> : Add data

```cpp
// Response:
IDbCacheClientDelegate2::OnNonExclusiveAddDataAck (CCallbackArgs args)
// Callback Event of proprietary DB Cache Client:
IDbCacheClientDelegate2::OnSomeoneAddData(CCallbackArgs& args)
```

<mark style="color:orange;">Proud.CDbCacheClient2.RequestNonExclusiveRemoveData</mark> : Remove data

```cpp
// Response:
IDbCacheClientDelegate2::OnNonExclusiveRemoveDataAck (CCallbackArgs args)
// Callback Event of proprietary DB Cache Client:
IDbCacheClientDelegate2::OnSomeoneRemoveData (CCallbackArgs& args)
```

<mark style="color:orange;">Proud.CDbCacheClient2.RequestNonExclusiveModifyValue</mark> : Perform specific operations on data.(+, -, \*, /)

```cpp
// Response:
IDbCacheClientDelegate2::OnNonExclusiveModifyValueSuccess (CCallbackArgs args)
IDbCacheClientDelegate2::OnNonExclusiveModifyValueFailed (CCallbackArgs args)
// Callback Event of proprietary DB Cache Client:
IDbCacheClientDelegate2::OnSomeoneModifyValue (CCallbackArgs& args)
```

<mark style="color:orange;">Proud.CDbCacheClient2.RequestNonExclusiveSetValueIf</mark> : Request to compare and store two values.

```cpp
// Response:
IDbCacheClientDelegate2::OnNonExclusiveSetValueIfSuccess (CCallbackArgs args)
IDbCacheClientDelegate2::OnNonExclusiveSetValueIfFailed (CCallbackArgs args)
 
// Callback Event of proprietary DB Cache Client:
IDbCacheClientDelegate2::OnSomeoneSetValue (CCallbackArgs& args)
```

## Precautions for using DB cache

This is a precaution when mixing unilateral type and request-response type (proprietary data request-response type & Block type & non-proprietary data access).

{% hint style="danger" %}

* Using a unilateral method while a request-response method is in use can cause <mark style="color:orange;">Proud.IDbCacheClientDelegate2.OnAccessError</mark> to be called.
* If you use a unilateral method without getting a response for the data you changed, you may end up with the wrong data.
  {% endhint %}

***

## Usage

{% content-ref url="using/usage" %}
[usage](https://docs.proudnet.com/proudnet.eng/proudnet/db_system/db_cache_ver2/using/usage)
{% endcontent-ref %}
