redis + cache docs

This commit is contained in:
tanner0101 2017-04-06 13:03:26 +02:00
parent 5babc16e52
commit d0b59e971b
5 changed files with 213 additions and 0 deletions

76
2.0/docs/cache/index.md vendored Normal file
View File

@ -0,0 +1,76 @@
# 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](#providers) below.
## Store
Storing data into the cache is straightforward.
```swift
try drop.cache.set("hello", "world")
```
### Expiration
When storing data, you can also supply an expiration date.
```swift
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.
```swift
try drop.cache.get("hello") // "world"
```
## Delete
Keys can be deleted from the cache using the `.delete()` method.
```swift
try drop.cache.delete("hello")
```
## Providers
Here is a list of official cache providers. You can [search GitHub](https://github.com/search?utf8=✓&q=topic%3Avapor-provider+topic%3Acache&type=Repositories) for additional packages.
| Type | Key | Description | Package | Class |
|--------|--------|---------------------------------|-----------------------------------------|-------------|
| Memory | memory | In-memory cache. Not persisted. | Vapor | MemoryCache |
| Fluent | fluent | Uses Fluent database. | [Fluent Provider](../fluent/package.md) | FluentCache |
| Redis | redis | Uses Redis database. | [RedisProvider](../redis/package.md) | RedisCache |
### How to Use
To use a different cache provider besides the default `MemoryCache`, make sure you have added the provider to your Package.
```swift
import Vapor
import <package>Provider
let drop = try Droplet()
try drop.addProvider(<package>Provider.Provider.self)
...
```
Then change the Droplet's configuration file.
`Config/droplet.json`
```json
{
"cache": "<key>"
}
```

View File

@ -93,6 +93,20 @@ import MySQLProvider
let mysqlDriver = try drop.mysql()
```
## Configure Cache
You can also choose to use your Fluent database (now set to MySQL) for caching.
`Config/droplet.json`
```json
{
"driver": "fluent"
}
```
Learn more about [caching here](../cache/index.md).
## Done
Next time you boot your Droplet, you should see:

43
2.0/docs/redis/package.md Normal file
View File

@ -0,0 +1,43 @@
# Using Redis
This section outlines how to import the Redis package both with or without a Vapor project.
## With Vapor
The easiest way to use Redis with Vapor is to include the Redis provider.
```swift
import PackageDescription
let package = Package(
name: "Project",
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 2),
.Package(url: "https://github.com/vapor/redis-provider.git", majorVersion: 1)
],
exclude: [ ... ]
)
```
The Redis provider package adds Redis to your project and conforms it to Vapor's `CacheProtocol`.
Use `import RedisProvider`.
## Just Redis
At the core of the Redis provider is a pure Swift Redis client. This package can be used by itself to send raw cache queries to your Redis database.
```swift
import PackageDescription
let package = Package(
name: "Project",
dependencies: [
...
.Package(url: "https://github.com/vapor/redis.git", majorVersion: 2)
],
exclude: [ ... ]
)
```
Use `import Redis`.

View File

@ -0,0 +1,75 @@
# Redis Provider
After you've [added the Redis Provider package](package.md) to your project, setting the provider up in code is easy.
## Add to Droplet
First, register the `RedisProvider.Provider` with your Droplet.
```swift
import Vapor
import RedisProvider
let drop = try Droplet()
try drop.addProvider(RedisProvider.Provider.self)
...
```
## Configure Vapor
Once the provider is added to your Droplet, you can configure Vapor to use Redis for caching.
`Config/droplet.json`
```json
{
"cache": "redis"
}
```
!!! seealso
Learn more about configuration files in the [Settings guide](../settings/config.md).
## Configure Redis
If you run your application now, you will likely see an error that the Redis configuration file is missing. Let's add that now.
### Basic
Here is an example of a simple Redis configuration file.
`Config/redis.json`
```json
{
"hostname": "127.0.0.1",
"port": 6379,
"password": "secret",
"database": 2
}
```
Both password and database are optional.
!!! note
It's a good idea to store the Redis configuration file in the `Config/secrets` folder since it may contain sensitive information.
### URL
You can also pass the Redis credentials as a URL.
`Config/Redis.json`
```json
{
"url": "redis://:secret@127.0.0.1:6379/2"
}
```
Both password and database are optional.
## Done
You are now ready to [start using Cache](../cache/index.md) with your Redis database.

View File

@ -38,10 +38,15 @@ pages:
- 'Model': 'fluent/model.md'
- 'Query': 'fluent/query.md'
- 'Relation': 'fluent/relation.md'
- Cache: 'cache/index.md'
- MySQL:
- 'Package': 'mysql/package.md'
- 'Provider': 'mysql/provider.md'
- 'Driver': 'mysql/driver.md'
- Redis:
- 'Package': 'redis/package.md'
- 'Provider': 'redis/provider.md'
- Auth:
- 'Getting Started': 'auth/getting-started.md'
- 'Helper': 'auth/helper.md'