Read and Write In Redis
2017-04-03

Databases can be slow and cumbersome. Redis offers database, cache, and message broker all rolled into one at high speed. With a variety of storage types and the ability to persist to disk, it's what all the cool kids use to make their lives easier. While DragToCode doesn't exercise every option available in Redis, you can command your instance with our blocks.

Here's how we did it.

Redis Background

Redis is an open source, advanced key-value store. It is often referred to as a data structure server, since the keys can contain strings, hashes, lists, sets and sorted sets.

Redis Setup

We created a free account on RedisLabs, RedisToGo, and Redis4You during our testing, and all three were nearly instant setup after confirming your email address. You are free to have your own instance of Redis too!

In order to take advantage of all current blocks, you'll need to have Redis version 2.6.0 or higher. If your version is lower, your mileage may vary. We found Redis4You started a 2.4.0 instance, breaking our increment/decrement blocks. Caveat Developer.

In each one, there was a connection string, like:
redis:// [ USERNAME ] : [ PASSWORD ] @ [ HOST ] : [ PORT ]
You'll need the host, port, and password in just a minute.

Connector Setup

Now that we picked up a host name (or IP address), port, and password, we can enter it in a DragToCode connector.
A direct route is https://www.dragtocode.com/connectors_view?action=Redis

Script Setup

Let's cover the individual blocks, then go to a composed example. I like a theme, and hopefully this one isn't too annoying-- Mission: Impossible. Spies depend on information, so we can set something to be read using the set block:

Notice that it's a fairly straightforward key-value set. So far, so good. Let's read it back:

Time to get fancy. When Mr. Phelps had a mission, the tape would self-destruct in 5 seconds. We can do that too:
We may not be a terribly efficient set of spies, so we may want to let it go for a specific date or time:

Need to append to an existing string? We can do that in one block:

If something is there and we want it gone, we can delete it:

We can check whether it's really gone:

Lists and arrays are basically the same thing, with some more specialized methods of setting the items in the list. You can get the list with:

It (of course) has a special way to set the entire list:

We can insert an item into the list (and set an item in the same block:

And now you can retrieve a single item in the list:

At any point, you can get the number of items in the list:

Let's put this all together in an example using SMS:

If the key (the phone number) isn't set, then we text "what is your name?" and set the key to 1.
If the key is set to 1, we see if the entire message body is "arthur." If it is, send "what is your quest?" and set the key to 2. If the response is not "arthur," then text "who?"
If the key is set to 2, we check anywhere in the response text for "grail." If it's there, send "what is the flight speed of a fully laden swallow?" and delete the Redis key. If it's not, we send "who?" and keep the key intact.
If nothing at all matches, we send back "no idea what's going on."

Conclusion

Redis offers some easy and rich features at high speed. DragToCode can command your instance with powerful blocks.
Give it a try and tell us about the great things you build!