Redis basic usage

To interact with Redis, you first need to construct a Redis client. The Redis library primarily supports TCP sockets.

This requires a hostname, port and worker. The eventloop 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.

  1. let client = try RedisClient.connect(on: worker) // Future<RedisClient>

The connect method will return a future 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.

  1. let null = RedisData.null
  2. let helloWorld = RedisData.bulkString("Hello World")
  3. let three = RedisData.integer(3)
  4. let oneThroughTen = RedisData.array([
  5. .integer(1),
  6. .integer(2),
  7. .integer(3),
  8. .integer(4),
  9. .integer(5),
  10. .integer(6),
  11. .integer(7),
  12. .integer(8),
  13. .integer(9),
  14. .integer(10)
  15. ])

The above is the explicit way of defining Redis Types. You can also use literals in most scenarios:

  1. let array = RedisData.array([
  2. [
  3. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  4. ],
  5. "Hello World",
  6. "One",
  7. "Two",
  8. .null,
  9. .null,
  10. "test"
  11. ])

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.

Creating a record

Creating a record is done using a RedisData for a value and a key.

  1. client.set("world", forKey: "hello")

This returns a future that’ll indicate successful or unsuccessful insertion.

Reading a record

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.

  1. let futureRecord = client.getData(forKey: "hello") // Future<RedisData>

Deleting a record

Deleting a record is similar but allows querying the keys, too.

  1. client.delete(keys: ["hello"])

Where the above command will remove the key “hello”, the next command will delete all keys from the Redis database.

  1. client.delete(keys: ["*"])