Bootstrap FreeKB - Redis - Create key value pairs using Python
Redis - Create key value pairs using Python

Updated:   |  Redis articles

This assumes you are already able to connect to Redis using Python. If not, check out my article Connect to Redis using Python. Here is an example of how to create a key value pair using set.

#!/usr/bin/python3
import redis

try:
  client = redis.Redis.from_url("redis://my-redis-cluster.abc123.clustercfg.use1.cache.amazonaws.com:6379")
except Exception as exception:
  print(f"got the following exception when trying redis.Redis.from_url - {exception}")
else:
  print(f"It appears the connection to Redis was successful")

try:
  client.set('foo', 'bar')
except Exception as exception:
  print(f"got the following exception when trying to set key 'foo' to have a value of 'bar' - {exception}")
else:
  print(f"successfully set the 'foo' key to have a value of 'bar'")

try:
  response = client.get('foo')
except Exception as exception:
  print(f"got the following exception when trying to get key 'foo' - {exception}")
else:
  print(f"foo = {response.decode('utf-8')}")

 

Something like this should be returned.

It appears the connection to Redis was successful
successfully set the 'foo' key to have a value of 'bar'
foo = bar

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 288316 in the box below so that we can be sure you are a human.