

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用結果快取的 Gremlin 查詢提示
<a name="gremlin-query-hints-results-cache"></a>

啟用[查詢結果快取](gremlin-results-cache.md)時，可以使用下列查詢提示。

## Gremlin `enableResultCache` 查詢提示
<a name="gremlin-query-hints-results-cache-enableResultCache"></a>

值為 `true` 的 `enableResultCache` 查詢提示會導致從快取傳回這些結果 (如果已快取它們的話)。如果沒有，它會傳回新的結果並快取它們，直到它們從快取中清除。例如：

```
g.with('Neptune#enableResultCache', true)
 .V().has('genre','drama').in('likes')
```

稍後，您可以再次發出完全相同的查詢來存取快取的結果。

如果此查詢提示的值為 `false`，或者如果它不存在，則不會快取查詢結果。不過，將其設定為 `false` 不會清除現有的快取結果。若要清除快取的結果，請使用 `invalidateResultCache` 或 `invalidateResultCachekey` 提示。

## Gremlin `enableResultCacheWithTTL` 查詢提示
<a name="gremlin-query-hints-results-cache-enableResultCacheWithTTL"></a>

`enableResultCacheWithTTL` 查詢提示也會傳回快取的結果 (如果有的話)，而不會影響已在快取中的結果的 TTL。如果目前沒有快取的結果，查詢會傳回新的結果，並快取這些結果，存留時間為 `enableResultCacheWithTTL` 查詢提示所指定的存留時間 (TTL)。該存留時間是以秒為單位指定的。例如，下列查詢會指定 60 秒的存留時間：

```
g.with('Neptune#enableResultCacheWithTTL', 60)
 .V().has('genre','drama').in('likes')
```

在 60 秒的存留時間結束之前，您可以搭配 `enableResultCache` 或 `enableResultCacheWithTTL` 查詢提示使用相同的查詢 (此處的 `g.V().has('genre','drama').in('likes')`) 來存取快取的結果。

**注意**  
使用 `enableResultCacheWithTTL` 指定的存留時間不會影響已快取的結果。  
如果之前已使用 `enableResultCache` 快取結果，則必須先明確地清除此快取，然後 `enableResultCacheWithTTL` 才能產生新結果，並快取它們，存留時間為其指定的 TTL。
如果之前已使用 `enableResultCachewithTTL` 快取結果，則先前的 TTL 必須先過期，然後 `enableResultCacheWithTTL` 才能產生新結果，並快取它們，存留時間為其指定的 TTL。

在存留時間過後，會清除查詢的快取結果，然後相同查詢的後續執行個體會傳回新的結果。如果 `enableResultCacheWithTTL` 附加至該後續查詢，則會快取新結果，存留時間為其指定的 TTL 。

## Gremlin `invalidateResultCacheKey` 查詢提示
<a name="gremlin-query-hints-results-cache-invalidateResultCacheKey"></a>

`invalidateResultCacheKey` 查詢提示可以採取 `true` 或 `false` 值。`true` 值會導致清除附加 `invalidateResultCacheKey` 的查詢的快取結果。例如，下列範例會導致清除針對查詢金鑰 `g.V().has('genre','drama').in('likes')` 快取的結果：

```
g.with('Neptune#invalidateResultCacheKey', true)
 .V().has('genre','drama').in('likes')
```

上述範例查詢不會導致其新的結果進行快取。如果您想要在清除現有的快取結果之後快取新結果，則可以在同一查詢中包含 `enableResultCache` (或 `enableResultCacheWithTTL`)：

```
g.with('Neptune#enableResultCache', true)
 .with('Neptune#invalidateResultCacheKey', true)
 .V().has('genre','drama').in('likes')
```

## Gremlin `invalidateResultCache` 查詢提示
<a name="gremlin-query-hints-results-cache-invalidateResultCache"></a>

`invalidateResultCache` 查詢提示可以採取 `true` 或 `false` 值。`true` 值會導致清除結果快取中的所有結果。例如：

```
g.with('Neptune#invalidateResultCache', true)
 .V().has('genre','drama').in('likes')
```

上述範例查詢不會導致其結果進行快取。如果您想要在完全清除現有的快取之後快取新結果，則可以在同一查詢中包含 `enableResultCache` (或 `enableResultCacheWithTTL`)：

```
g.with('Neptune#enableResultCache', true)
 .with('Neptune#invalidateResultCache', true)
 .V().has('genre','drama').in('likes')
```

## Gremlin `numResultsCached` 查詢提示
<a name="gremlin-query-hints-results-cache-numResultsCached"></a>

`numResultsCached` 查詢提示只能與包含 `iterate()` 的查詢搭配使用，而且它會針對其所附加的查詢指定要快取的結果數目上限。請注意，`numResultsCached` 存在時快取的結果不會傳回，只會快取。

例如，下列查詢會指定應快取其最多 100 個結果，但不會傳回任何快取的結果：

```
g.with('Neptune#enableResultCache', true)
 .with('Neptune#numResultsCached', 100)
 .V().has('genre','drama').in('likes').iterate()
```

然後，您可以使用如下的查詢來擷取一系列快取的結果 (此處為前十個)：

```
g.with('Neptune#enableResultCache', true)
 .with('Neptune#numResultsCached', 100)
 .V().has('genre','drama').in('likes').range(0, 10)
```

## Gremlin `noCacheExceptions` 查詢提示
<a name="gremlin-query-hints-results-cache-noCacheExceptions"></a>

`noCacheExceptions` 查詢提示可以採取 `true` 或 `false` 值。`true` 值會導致隱藏與結果快取相關的任何例外狀況。例如：

```
g.with('Neptune#enableResultCache', true)
 .with('Neptune#noCacheExceptions', true)
 .V().has('genre','drama').in('likes')
```

尤其，這會隱藏 `QueryLimitExceededException`，如果查詢的結果太大而無法容納在結果快取中，就會引發此例外狀況。