Vapor's CacheProtocol allows you to store and fetch items from a cache using optional expiration dates.

By default, the Droplet's cache is set to MemoryCache. See the various providers below.


Storing data into the cache is straightforward.

try drop.cache.set("hello", "world")


When storing data, you can also supply an expiration date.

try drop.cache.set("ephemeral", 42, expiration: Date(timeIntervalSinceNow: 30))

In the above example, the supplied key value pair will expire after 30 seconds.


You can retreive data from the cache using the .get() method.

try drop.cache.get("hello") // "world"


Keys can be deleted from the cache using the .delete() method.

try drop.cache.delete("hello")


Here is a list of official cache providers. You can search GitHub for additional packages.

Type Key Description Package Class
Memory memory In-memory cache. Not persisted. Vapor MemoryCache
Fluent fluent Uses Fluent database. Fluent Provider FluentCache
Redis redis Uses Redis database. RedisProvider RedisCache

How to Use

To use a different cache provider besides the default MemoryCache, make sure you have added the provider to your Package.

import Vapor
import <package>Provider

let config = try Config()
try config.addProvider(<package>Provider.Provider.self)

let drop = try Droplet(config)


Then change the Droplet's configuration file.


    "cache": "<key>"