-
Notifications
You must be signed in to change notification settings - Fork 24
Quick Start: Add Caching
This topic explains how to add caching to the quick start API using Volos.js Swagger extensions.
- Before you begin
- Configuring the cache in the Swagger editor
- Testing the cache configuration
- Using a helper function to customize caching
If you're using the default skeleton project (the one that's installed when you do a127 project create
), then you can skip this step.
But just keep in mind that you must include one of the volos-cache-*
modules in your project's package.json
file. In this example, we're going to use volos-cache-memory
. If you change package.json
, remember to run npm install
in the project root folder to install the dependencies.
For information on the volos-cache-*
modules, see the Volos.js page on GitHub.
Adding a cache using Volos.js is as simple as adding a few lines of YAML in your API's Swagger specification file.
- Execute
$ a127 project edit
to open up the Swagger editor. - In the Swagger configuration file, under
x-a127-services
, add the following to enable an in-memory cache provider namedcache
. In this case, the cache is set with a TTL (time to live) of 60000 milliseconds.
x-a127-services:
cache:
provider: volos-cache-memory
options:
name: hello-cache
ttl: 60000
- Finally, use the
x-a127-apply
extension to apply this policy to the paths you want to enforce caching on:
paths:
/hello:
x-swagger-router-controller: hello_world
x-a127-authorizations: {}
x-a127-apply:
cache: {}
quota: {}
You set up your cache to hold values in memory for 60000 milliseconds. Let's test it out.
- Start the app with
a127 project start
. - Call the API in succession:
$ curl http://localhost:10010/hello\?name\=Me
$ curl http://localhost:10010/hello\?name\=Me
$ curl http://localhost:10010/hello\?name\=Me
The caching policy caches responses for the specified TTL duration.
If you do not specify a helper function to evaluate the Cache Key the entire URI will be used. This is less effective if you have API keys or other query parameters that result in many distinct URIs that could be cached using the same key.
The example above shows how you can use a single query parameter, city
in this case, as the Cache Key
paths:
/hello_cached:
x-swagger-router-controller: hello_world
x-a127-apply:
cache:
key:
helper: volos
function: cacheKey
get:
...
Using this annotation instructs Apigee-127 to call the function cacheKey
in the helper module volos
which it will look for in api/helpers
by default. Here is an example implementation that uses the name
query parameter to evaluate the Cache key:
module.exports = {
cacheKey: cacheKey,
};
function cacheKey(req) {
// This can check for a specific query parameter
var key = req.swagger.params.name.value;
console.log('Cache Key: '+key)
return key;
}
Tip: You can add whatever logic you wish to use to evaluate the Cache key, but don't add too much complexity or your latency may go way up!
Having Trouble? Try posting your question to the Apigee Community. Or, for more links and resources, check out our Help Page
Need help? Visit the Apigee Community ! |
---|
-
Getting started
-
Add policies to your API
-
Add security policies
-
Deploy your projects
-
Programmatic hooks
-
Good to know about
-
Deep dives
-
Reference topics
-
Troubleshooting and getting help
-
Related resources