Using Cache Service
DreamFactory allows you to expose various Cache backends as API Endpoints. The following Cache services are currently supported:
- Local Cache (configurable to use local file based cache, Redis, Memcached, APC, or Database)
- Redis
- Memcached
Contents
- 1 Provisioning a Cache Service
- 2 API Endpoints
- 3 Example - Creating cache keys in bulk
- 4 Example - Creating a cache key with custom TTL
- 5 Example - Creating a forever cache key
- 6 Example - Replacing cache keys in bulk
- 7 Example - Replacing a cache key with custom TTL
- 8 Example - Replacing a cache key and store it forever
- 9 Example - Retrieving a cache key
- 10 Example - Retrieving a cache key with default value
- 11 Example - Retrieving and clearing a cache key
- 12 Example - Deleting a cache key
Provisioning a Cache Service
You can provision a cache service from the admin console.
Log in to the admin console and select the 'Services' tab. Click on the 'Create' button from the left menu. From the 'Service Type' drop down select Cache menu then select your desired Cache type (Local, Redis, Memcached). After that complete the new service form and save it to create your cache service. For more details on cache service provisioning please see this.
For this tutorial, we will use a Cache service named my-cache.
API Endpoints
Creating cache keys
Creating cache keys in bulk:
POST http://{url}/api/v2/{cache_service_name}
Request body:
{ "{key_name}": "string" }
Creating a single cache key:
POST http://{url}/api/v2/{cache_service_name}/{key_name}
Request body:
string|json
Note: Creating a cache key that already exists will return a Bad Request (400) response. Use PUT to replace existing key(s)
Replacing cache keys
Replacing cache keys in bulk:
PUT http://{url}/api/v2/{cache_service_name}
Request body:
{ "{key_name}": "string" }
Replacing a single cache key:
PUT http://{url}/api/v2/{cache_service_name}/{key_name}
Request body:
string|json
Updating a cache key
PATCH http://{url}/api/v2/{cache_service_name}/{key_name}
Request body:
string|json
Retrieving a cache key
GET http://{url}/api/v2/{cache_service_name}/{key_name}
Deleting a cache key
DELETE http://{url}/api/v2/{cache_service_name}/{key_name}
Example - Creating cache keys in bulk
- Service name: my-cache
- Request body:
{ "key1":"value1", "key2":"value2, "key3":"value3" }
- Request URL:
POST https://foo.com/api/v2/my-cache
Example - Creating a cache key with custom TTL
- Service name: my-cache
- Cache key name: key1
- TTL: 10 (minutes)
- Request Content-Type: text/plain
- Request body:
A plain string value
- Request URL:
POST https://foo.com/api/v2/my-cache/key1?ttl=10
Example - Creating a forever cache key
- Service name: my-cache
- Cache key name: customer
- Request Content-Type: application/json
- Request body:
{ "name":"John Doe", "age":"30", "address":"123 st, Atlanta, GA" }
- Request URL:
POST https://foo.com/api/v2/my-cache/key1?forever=true
Example - Replacing cache keys in bulk
- Service name: my-cache
- Request body:
{ "key1":"value1", "key2":"value2, "key3":"value3" }
- Request URL:
PUT https://foo.com/api/v2/my-cache
Example - Replacing a cache key with custom TTL
- Service name: my-cache
- Cache key name: key1
- TTL: 10 (minutes)
- Request Content-Type: text/plain
- Request body:
A plain string value
- Request URL:
PUT https://foo.com/api/v2/my-cache/key1?ttl=10
Example - Replacing a cache key and store it forever
- Service name: my-cache
- Cache key name: customer
- Request Content-Type: application/json
- Request body:
{ "name":"John Doe", "age":"30", "address":"123 st, Atlanta, GA" }
- Request URL:
PUT https://foo.com/api/v2/my-cache/key1?forever=true
Example - Retrieving a cache key
- Service name: my-cache
- Cache key name: key1
- Request URL:
GET https://foo.com/api/v2/my-cache/key1
Example - Retrieving a cache key with default value
- Service name: my-cache
- Cache key name: key1
- Default value: value1
- Request URL:
GET https://foo.com/api/v2/my-cache/key1?default=value1
Example - Retrieving and clearing a cache key
- Service name: my-cache
- Cache key name: key1
- Request URL:
GET https://foo.com/api/v2/my-cache/key1?clear=true
Example - Deleting a cache key
- Service name: my-cache
- Cache key name: key1
- Request URL:
DELETE https://foo.com/api/v2/my-cache/key1