mirror of https://github.com/vapor/docs.git
Redis documentation
This commit is contained in:
parent
4d037f8486
commit
0f056ee7fd
|
|
@ -0,0 +1,101 @@
|
|||
# Redis basic usage
|
||||
|
||||
To interact with Redis, you first need to construct a Redis client.
|
||||
|
||||
To connect to Redis you can use a variety of methods. The Redis library primarily supports [TCP sockets](../sockets/tcp-client.md).
|
||||
|
||||
This requires a hostname, port and [worker](../async/worker.md). The worker's DispatchQueue will be used for Redis' Socket. The hostname and port have a default. The hostname is defaulted to `localhost`, and the port to Redis' default port `6379`.
|
||||
|
||||
```swift
|
||||
let client = try RedisClient<TCPClient>.connect(worker: worker) // Future<RedisClient<TCPClient>>
|
||||
```
|
||||
|
||||
The `connect` method will return a [Future](../async/promise-future.md) containing the TCP based Redis Client.
|
||||
|
||||
## Redis Data Types
|
||||
|
||||
Redis has 6 data types:
|
||||
|
||||
- null
|
||||
- Int
|
||||
- Error
|
||||
- Array
|
||||
- Basic String (used for command names and basic replies only)
|
||||
- Bulk String (used for Strings and binary data blobs)
|
||||
|
||||
You can instantiate one from the static functions and variables on `RedisData`.
|
||||
|
||||
```swift
|
||||
let null = RedisData.null
|
||||
|
||||
let helloWorld = RedisData.bulkString("Hello World")
|
||||
|
||||
let three = RedisData.integer(3)
|
||||
|
||||
let oneThroughTen = RedisData.array([
|
||||
.integer(1),
|
||||
.integer(2),
|
||||
.integer(3),
|
||||
.integer(4),
|
||||
.integer(5),
|
||||
.integer(6),
|
||||
.integer(7),
|
||||
.integer(8),
|
||||
.integer(9),
|
||||
.integer(10)
|
||||
])
|
||||
```
|
||||
|
||||
The above is the explicit way of defining Redis Types. You can also use literals in most scenarios:
|
||||
|
||||
```swift
|
||||
let array = RedisData.array([
|
||||
[
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
|
||||
],
|
||||
"Hello World",
|
||||
"One",
|
||||
"Two",
|
||||
.null,
|
||||
.null,
|
||||
"test"
|
||||
])
|
||||
```
|
||||
|
||||
## CRUD using Redis
|
||||
|
||||
From here on it is assumed that your client has been successfully created and is available in the variable `client` as a `RedisClient<TCPClient>`.
|
||||
|
||||
### Creating a record
|
||||
|
||||
Creating a record is done using a `RedisData` for a value and a key.
|
||||
|
||||
```swift
|
||||
client.set("world", forKey: "hello")
|
||||
```
|
||||
|
||||
This returns a future that'll indicate successful or unsuccessful insertion.
|
||||
|
||||
### Reading a recod
|
||||
|
||||
Reading a record is similar, only you'll get a warning if you don't use the returned future.
|
||||
|
||||
The `Future<RedisData>` for the key "hello" will be "world" if you created the record as shown above.
|
||||
|
||||
```swift
|
||||
let futureRecord = client.getData(forKey: "hello") // Future<RedisData>
|
||||
```
|
||||
|
||||
### Deleting a record
|
||||
|
||||
Deleting a record is similar but allows querying the keys, too.
|
||||
|
||||
```swift
|
||||
client.delete(keys: ["hello"])
|
||||
```
|
||||
|
||||
Where the above command will remove the key "hello", the next command will delete **all** keys from the Redis database.
|
||||
|
||||
```swift
|
||||
client.delete(keys: ["*"])
|
||||
```
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
# Custom commands
|
||||
|
||||
Many commands are not (yet) implemented by the driver using a convenience function. This does not mean the feature/command is not usable.
|
||||
|
||||
[(Almost) all functions listed here](https://redis.io/commands) work out of the box using custom commands.
|
||||
|
||||
## Usage
|
||||
|
||||
The Redis client has a `run` function that allows you to run these commands.
|
||||
|
||||
The following code demonstrates a "custom" implementation for [GET](https://redis.io/commands/get).
|
||||
|
||||
```swift
|
||||
let future = client.run(command: "GET", arguments: ["my-key"]) // Future<RedisData>
|
||||
```
|
||||
|
||||
This future will contain the result as specified in the article on the redis command page or an error.
|
||||
|
||||
The future can be used as described in the [Async API](../async/index.md).
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
# Redis
|
||||
|
||||
Redis is a Redis client library that can communicate with a Redis database.
|
||||
|
||||
### What is Redis?
|
||||
|
||||
Redis is an in-memory data store used as a database, cache and message broker. It supports most common data structures. Redis is most commonly used for caching data such as sessions and notifications (between multiple servers).
|
||||
|
||||
Redis works as a key-value store, but allows querying the keys, unlike most databases.
|
||||
|
||||
### Index
|
||||
|
||||
- [Basics](basics.md)
|
||||
- [Custom commands](custom-commands.md)
|
||||
|
||||
## With and without Vapor
|
||||
|
||||
To include it in your package, add the following to your `Package.swift` file.
|
||||
|
||||
```swift
|
||||
// swift-tools-version:4.0
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "Project",
|
||||
dependencies: [
|
||||
...
|
||||
.package(url: "https://github.com/vapor/redis.git", .upToNextMajor(from: "3.0.0")),
|
||||
],
|
||||
targets: [
|
||||
.target(name: "Project", dependencies: ["Redis", ... ])
|
||||
]
|
||||
)
|
||||
```
|
||||
|
||||
Use `import Redis` to access Redis' APIs.
|
||||
|
|
@ -25,6 +25,16 @@ pages:
|
|||
- 'Request': 'http/request.md'
|
||||
- 'Status codes': 'http/status.md'
|
||||
- 'URI': 'http/uri.md'
|
||||
- Redis:
|
||||
- 'Index': 'redis/index.md'
|
||||
- 'Basics': 'redis/basics.md'
|
||||
- 'Custom commands': 'redis/custom-commands.md'
|
||||
- Routing:
|
||||
- 'Async': 'routing/async.md'
|
||||
- 'Parameters': 'routing/parameters.md'
|
||||
- 'Route': 'routing/route.md'
|
||||
- 'Router': 'routing/router.md'
|
||||
- 'Sync': 'routing/sync.md'
|
||||
- WebSocket:
|
||||
- 'WebSocket': 'websocket/websocket.md'
|
||||
- 'Client': 'websocket/client.md'
|
||||
|
|
@ -32,13 +42,6 @@ pages:
|
|||
- 'Text': 'websocket/text-stream.md'
|
||||
- JWT:
|
||||
- 'Signed Tokens': 'jwt/jws.md'
|
||||
- Routing:
|
||||
- 'Index': 'routing/index.md'
|
||||
- 'Async': 'routing/async.md'
|
||||
- 'Sync': 'routing/sync.md'
|
||||
- 'Parameters': 'routing/parameters.md'
|
||||
- 'Router': 'routing/router.md'
|
||||
- 'Route': 'routing/route.md'
|
||||
- Crypto:
|
||||
- 'Index': 'crypto/index.md'
|
||||
- 'Base64': 'crypto/base64.md'
|
||||
|
|
|
|||
Loading…
Reference in New Issue