Redis and Vapor With Server-Side Swift: Getting Started

Learn how to use the in-memory data store, Redis, and Vapor to cache objects by saving them in JSON, then configuring them to expire after a set time. By Walter Tyree.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Where to Go From Here?

In this tutorial, you learned how to run Redis in Docker with your Vapor app. You also saw how to cache and retrieve an encodable object and how to expire a key.

This was only a small part of what Redis can do. Here are some suggestions for other ways to use Redis:

You’ll notice that the value returned is greater. In a Vapor app, the command looks a little different: req.redis.increment(RedisKey("pageCount")).

  • Track a user’s session or page visits by opening the Redis CLI and entering a new key with a value of 0:
    SET pageCount 0
    
  • To track how many people have visited a page, increase pageCount using the Redis INCR command, like so:
    INCR pageCount
    
  • To retrieve the current value type:
    GET pageCount
    
SET pageCount 0
INCR pageCount
GET pageCount

Using these commands lets you easily add buttons for upvotes and downvotes to the dog pages, for example.

You can find the complete documentation for INCR and its counterpart, DECR, at the Redis documentation site.

You can download the completed project files by clicking the Download Materials button at the top or bottom of this tutorial.

The Vapor Redis library is built on RediStack. A full list of the commands available to your Vapor app can be found at the RediStack documentation site.

It’s also helpful to look at the redis and RediStack header files. Sometimes there’s extra documentation or a slight wording change between the actual Redis command and how it’s implemented for Vapor. INCR is one example of that.

If you have any questions or comments, please join the forum discussion below.