Databases Reference
In-Depth Information
If you haven't already, install and set up Redis. Refer to Appendix A for help setting up Redis.
To explain Redis, I rely on the Redis command-line client (
redis-cli
) — and a simple use case that
involves a categorized list of books.
To b e g i n , it's a r t
redis-cli
and make sure it's working. First, go to the Redis distribution folder.
Redis is distributed as source. You can extract it anywhere in the fi le system and compile the source.
Once compiled executables are available in the distribution folder. On some operating systems,
symbolic links to executables are created at locations, where executables are commonly found. On
my system Redis is available within a folder named Redis-2.2.2, which corresponds to the latest
release of Redis. Start the Redis server by using the
redis-server
command. To use the default
confi guration, run
./redis-server
from within the Redis distribution folder. Now run
redis-cli
to connect to this server. By default, the Redis server listens for connections on port 6379.
To save a key/value pair —
{ akey: “avalue” }
— simply type the following, from within the
Redis distribution folder:
./redis-cli set akey “avalue”
If you see OK on the console in response to the command you just typed, then things look good. If not,
go through the installation instructions and verify that the setup is correct. To confi rm that
avalue
is
stored for
akey
, simply get the value for
akey
like so:
./redis-cli get akey
You should see
a value
in response to this request.
UNDERSTANDING THE REDIS EXAMPLE
In this Redis example, a database stores a list of book titles. Each book is tagged
using an arbitrary set of tags. For example, I add “The Omnivore's Dilemma”
by “Michael Pollan” to the list and tag it with the following: “organic,”
“industrialization,” “local,” “written by a journalist,” “best seller,” and “insight”
or add “Outliers” by “Malcolm Gladwell” and tag it with the following: “insight,”
“best seller,” and “written by a journalist.” Now I can get all the topics on my list or
all the topics “written by a journalist” or all those that relate to “organic.” I could
also get a list of the topic by a given author. Querying will come in the next section,
but for now storing the data appropriately is the focus.
If you install Redis using
“make install”
after compiling from source, the
redis-server
and
redis-cli
will be added to
/usr/local/bin
by default. If
/usr/local/bin
is added to the PATH environment variable you will be able to
run
redis-server
and
redis-cli
from any directory.