

# Cache

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.

## Store

Storing data into the cache is straightforward.

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

### Expiration

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.

## Fetch

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

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

## Delete

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

    try drop.cache.delete("hello")

## Providers

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.

`Config/droplet.json`

    {
        "cache": "<key>"
    }