serverless-api-gateway-caching
Advanced tools
Comparing version 1.2.0 to 1.2.1
{ | ||
"name": "serverless-api-gateway-caching", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "A plugin for the serverless framework which helps with configuring caching for API Gateway endpoints.", | ||
@@ -5,0 +5,0 @@ "main": "src/apiGatewayCachingPlugin.js", |
@@ -21,6 +21,5 @@ # serverless-api-gateway-caching | ||
## Currently not supported: | ||
* lambda functions with many HTTP events. | ||
## Examples | ||
## Example | ||
### Minimal setup | ||
@@ -35,13 +34,7 @@ ```yml | ||
enabled: true | ||
clusterSize: '0.5' # defaults to '0.5' | ||
ttlInSeconds: 300 # defaults to the maximum allowed: 3600 | ||
perKeyInvalidation: | ||
requireAuthorization: true # default is true | ||
handleUnauthorizedRequests: IgnoreWithWarning # default is "IgnoreWithWarning" | ||
functions: | ||
# Responses are not cached | ||
# Responses are cached | ||
list-all-cats: | ||
handler: rest_api/cats/get/handler.handle | ||
role: listCatsRole | ||
events: | ||
@@ -52,4 +45,12 @@ - http: | ||
caching: | ||
enabled: false # default is false | ||
enabled: true | ||
# Responses are *not* cached | ||
update-cat: | ||
handler: rest_api/cat/post/handler.handle | ||
events: | ||
- http: | ||
path: /cat | ||
method: post | ||
# Responses are cached based on the 'pawId' path parameter and the 'Accept-Language' header | ||
@@ -64,6 +65,51 @@ get-cat-by-paw-id: | ||
enabled: true | ||
cacheKeyParameters: | ||
- name: request.path.pawId | ||
- name: request.header.Accept-Language | ||
``` | ||
### Configuring the cache cluster size and cache time to live | ||
Cache time to live and invalidation settings are applied to all functions, unless specifically overridden. | ||
```yml | ||
plugins: | ||
- serverless-api-gateway-caching | ||
custom: | ||
# Enable or disable caching globally | ||
apiGatewayCaching: | ||
enabled: true | ||
clusterSize: '0.5' # defaults to '0.5' | ||
ttlInSeconds: 300 # defaults to the maximum allowed: 3600 | ||
perKeyInvalidation: | ||
requireAuthorization: true # default is true | ||
handleUnauthorizedRequests: Ignore # default is "IgnoreWithWarning" | ||
``` | ||
### Configuring per-function cache time to live, cache invalidation strategy and cache key parameters | ||
```yml | ||
plugins: | ||
- serverless-api-gateway-caching | ||
custom: | ||
# Enable or disable caching globally | ||
apiGatewayCaching: | ||
enabled: true | ||
functions: | ||
# Responses are cached based on the 'pawId' path parameter and the 'Accept-Language' header | ||
get-cat-by-paw-id: | ||
handler: rest_api/cat/get/handler.handle | ||
events: | ||
- http: | ||
path: /cats/{pawId} | ||
method: get | ||
caching: | ||
enabled: true | ||
ttlInSeconds: 3600 | ||
perKeyInvalidation: | ||
requireAuthorization: true | ||
handleUnauthorizedRequests: Ignore | ||
requireAuthorization: true # default is true | ||
handleUnauthorizedRequests: Fail # default is "IgnoreWithWarning" | ||
cacheKeyParameters: | ||
@@ -70,0 +116,0 @@ - name: request.path.pawId |
@@ -13,16 +13,17 @@ const split = require('lodash.split'); | ||
const getApiGatewayMethodNameFor = (path, httpMethod) => { | ||
const pathElements = split(path,'/'); | ||
const pathElements = split(path, '/'); | ||
pathElements.push(httpMethod.toLowerCase()); | ||
let gatewayResourceName = pathElements | ||
.map (element => { | ||
element = element.toLowerCase(); | ||
if(element.startsWith('{')) { | ||
element = element.substring(element.indexOf('{') + 1,element.indexOf('}')) + "Var"; | ||
} | ||
//capitalize first letter | ||
return element.charAt(0).toUpperCase() + element.slice(1); | ||
}).reduce((a, b) => a + b); | ||
.map(element => { | ||
element = element.toLowerCase(); | ||
element = element.replace('+', ''); | ||
if (element.startsWith('{')) { | ||
element = element.substring(element.indexOf('{') + 1, element.indexOf('}')) + "Var"; | ||
} | ||
//capitalize first letter | ||
return element.charAt(0).toUpperCase() + element.slice(1); | ||
}).reduce((a, b) => a + b); | ||
gatewayResourceName = "ApiGatewayMethod" + gatewayResourceName; | ||
return gatewayResourceName; | ||
gatewayResourceName = "ApiGatewayMethod" + gatewayResourceName; | ||
return gatewayResourceName; | ||
} | ||
@@ -34,11 +35,11 @@ | ||
continue; | ||
} | ||
} | ||
const resourceName = getApiGatewayMethodNameFor(endpointSettings.path, endpointSettings.method); | ||
const method = getResourcesByName(resourceName,serverless); | ||
if (!method) { | ||
const method = getResourcesByName(resourceName, serverless); | ||
if (!method) { | ||
serverless.cli.log(`[serverless-api-gateway-caching] The method ${resourceName} couldn't be found in the | ||
compiled CloudFormation template. Caching settings will not be updated for this endpoint.`); | ||
const index = settings.endpointSettings.indexOf(endpointSettings); | ||
if(index != -1) { | ||
settings.endpointSettings.splice(index,1); | ||
if (index != -1) { | ||
settings.endpointSettings.splice(index, 1); | ||
} | ||
@@ -64,5 +65,5 @@ return; | ||
module.exports = { | ||
module.exports = { | ||
addPathParametersCacheConfig: addPathParametersCacheConfig, | ||
getApiGatewayMethodNameFor: getApiGatewayMethodNameFor | ||
} |
21027
374
115