

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用结果缓存的 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)。该生存时间以秒为单位指定。例如，以下查询将生存时间指定为六十秒：

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

在 60 秒 time-to-live结束之前，您可以使用带有`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`，如果查询的结果太大而无法容纳在结果缓存中，则会引发该异常。