AppSync supports server-side data caching. You can find the caching configuration under the appSync.caching attribute.
appSync:
name: my-api
caching:
behavior: 'PER_RESOLVER_CACHING'
type: 'SMALL'
ttl: 3600
atRestEncryption: false
transitEncryption: falsebehavior:FULL_REQUEST_CACHINGorPER_RESOLVER_CACHINGtype: The type of the Redis instance.SMALL,MEDIUM,LARGE,XLARGE,LARGE_2X,LARGE_4X,LARGE_8X,LARGE_12X. Defaults toSMALLttl: The default TTL of the cache in seconds. Defaults to3600. Maximum is3600enabled: Boolean. Whether caching is enabled. Defaults totruewhen thecachingdefinition is present.atRestEncryption: Boolean. Whether to encrypt the data at rest. Defaults tofalsetransitEncryption: Boolean. Whether to encrypt the data in transit. Defaults tofalse
See Resolver caching
To evict a single entry from the cache (rather than flushing everything), use AppSync's evictFromApiCache extension from within a resolver. This is a runtime feature of AppSync, so there is nothing special to configure in this plugin: just call it from the mapping template or JavaScript code of the resolver, and it is passed through to AppSync as-is.
A few things to keep in mind (see the AppSync docs for the details):
- It only works in mutation resolvers, not queries.
- The target entry is identified by the type name, field name, and a map of caching keys. The keys must match — in the same order — the
keysof the cached resolver you want to evict.
For example, given a cached query resolver:
appSync:
resolvers:
Query.getNote:
dataSource: notes
caching:
ttl: 60
keys:
- '$context.arguments.id'You can evict its cached entry from a mutation resolver. With a VTL response template:
#set($keys = {})
$util.qr($keys.put("context.arguments.id", $ctx.args.id))
$extensions.evictFromApiCache("Query", "getNote", $keys)
$util.toJson($ctx.result)Or with a JavaScript resolver:
import { extensions } from '@aws-appsync/utils';
export function response(ctx) {
extensions.evictFromApiCache('Query', 'getNote', {
'context.arguments.id': ctx.args.id,
});
return ctx.result;
}You can use the flush-cache command to easily flush the cache.