e-Book - Redis in Action

This book covers the use of Redis, an in-memory database/data structure server.
  • Foreword
  • Preface
  • Acknowledgments
  • About this Book
  • About the Cover Illustration
  • Part 1: Getting Started
  • Part 2: Core concepts
  • Part 3: Next steps
  • Appendix A
  • Appendix B
  • Buy the paperback

    A.4 Hello Redis

    After Redis itself is installed, you need to ensure that Python has the proper libraries
    for accessing Redis. If you followed the earlier instructions, you may still have a command
    prompt open. In this command prompt (you can open a new one if you closed
    the old one), you’ll run Python. (Windows users can refer to how you ran Python during
    the setup procedure.) Inside of the Python console, you’ll try connecting to Redis
    and issue a couple commands, as shown in the next listing.

    Listing A.7Testing Redis from Python
    ~:$ python
    

    Start Python so that you can verify everything is up and running correctly.

    Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
    [GCC 4.4.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    

    *insert a tooltip/decription here*

    >>> import redis
    

    Import the redis library; it will automatically use the hiredis C accelerator library if it’s available.

    >>> conn = redis.Redis()
    

    Create a connection to Redis.

    >>> conn.set('hello', 'world')
    True
    

    Set a value and see that it was set.

    >>> conn.get('hello')
    'world'
    

    Get the value you just set.

    RUNNING PYTHON IN OTHER WAYSThough you can run Python in a standard
    terminal, there are a variety of other more “fully featured” ways to have a
    Python console available. A basic editor and console called Idle ships with
    Python on Windows and OS X; you can also install it on Linux (install the
    idle-python2.6 or idle-python2.7 package, as relevant). In a console, you
    can run python -m idlelib.idle from the command line, and Idle should
    load. Idle is a fairly basic editor and Python console itself, so if you’re new to
    programming, it should be a gentle introduction. Many other people have
    found IPython to be the Python console of choice, offering a list of amazing features too long to mention here. Whether you go basic or fully featured,
    you can’t go wrong.

    REDIS ON OS X AND WINDOWSRight now, precompiled versions of Redis for
    Windows and OS X are from the 2.4 series. In some chapters, we use features
    that are only available in the Redis 2.6 and later series. If you find that something
    we do doesn’t work, and you’re using Redis 2.4, it’s probably because
    the feature or usage was added in Redis 2.6. See the notes in chapter 3 for
    specific examples.

    CONFIGURING REDISBy default, Redis should be configured to keep your data
    using either snapshots or append-only files, and as long as you execute shutdown
    on a client, Redis should keep your data around. Depending on how you
    started it, Redis may be keeping the on-disk version of data in the same path
    as the path you’re running it from. To update that, you’ll want to edit
    redis.conf and use system startup scripts appropriate for your platform
    (remember to move your data to the newly configured path). More information
    about configuring Redis is available in chapter 4.

    IS HIREDIS AVAILABLE ON NON-LINUX PLATFORMS?For those who are using Windows
    or OS X and peeked at the Debian/Ubuntu install instructions, you’ll
    have noticed that we installed a library called hiredis to be used with Python.
    This library is an accelerator that passes protocol processing to a C library.
    Though this library can be compiled for OS X and Windows, binaries for
    them aren’t readily downloadable on the internet. Also, because I didn’t have
    you install a compiler, if you’re interested in compiling and using hiredis on
    your platform, you’re on your own.

    Periodically in other chapters, we’ll use the Python console to show interactions with
    Redis. In other cases, we’ll show function definitions and executable statements outside
    of a console. In those cases where we’re not using the console, it’s assumed that
    those functions are defined in a Python module. If you haven’t used Python before,
    you should read Python’s tutorial on modules and running modules as programs in
    the first part of http://docs.python.org/tutorial/modules.html, up to and including
    the section “Executing modules as scripts.”

    If you’re the kind of person who can read through language documentation and
    tutorials and just “get it,” and you haven’t spent a lot of time with Python in the past, you
    may want to consider going through the Python language tutorial at http://docs.python.org/tutorial/. If you’re only interested in the important stuff where you’ll
    learn the most about Python’s syntax and semantics, read sections 3-7, and then 9.10
    and 9.11 (which are about generators, which we use a couple of times).

    By now you’ll have Redis and a Python interpreter running. If you got here via a
    reference from chapter 1, go back to really start using Redis from Python.

    If you’re having difficulty installing Redis or Python, please post your questions or
    read others’ answers on the Redis in Action Manning forum: http://mng.bz/vB6c.