:orphan:

===================
Connecting to Redis
===================

Data Science & AI Workbench enables you to easily connect to a Redis in-memory database.

Before you can do so, however, you'll need to pip install the Python package required to connect to Redis servers::

   pip install redis

.. note:: 

    Any packages you install from the command line are available *during the current session only*. If you want them to persist, add them to the project's ``anaconda-project.yml`` file. For more information, see :doc:`../projects/project-config`.

You can then import the library and use code such as this to connect to Redis from within a notebook session:

.. code-block:: python

   # Import the library
   import redis


   queried_value = None
   try:
       # Generate the connection
       r = redis.Redis(host='support-redis.dev.anaconda.com', port=6379)
    
       # Set and retrieve the same key
       r.set('test_key', 'This is a test value for showing redis connectivity')
       queried_value = r.get('test_key')
   except Exception as e:
       print(f'Unable to connect or execute commands on Redis server: {e}')

   # Print out queried value
   print(queried_value.decode('utf-8'))
  
