Connecting to ElastiCache
The following examples use the Redis OSS client to connect to ElastiCache.
Connecting to a cluster mode disabled cluster
Copy the following program and paste it into a file named ConnectClusterModeDisabled.py. Update the mechanism for supplying credentials. Credentials in this example are shown as replaceable and assigned an undeclared item. Avoid hard-coding credentials.
from redis import Redis import logging logging.basicConfig(level=logging.INFO) redis = Redis(host='primary.xxx.yyyyyy.zzz1.cache.amazonaws.com', port=6379, decode_responses=True, ssl=True, username=
example
, password=EXAMPLE
) if redis.ping(): logging.info("Connected to Redis")
To run the program, enter the following command:
python ConnectClusterModeDisabled.py
Connecting to a cluster mode enabled cluster
Copy the following program and paste it into a file named ConnectClusterModeEnabled.py.
from rediscluster import RedisCluster import logging logging.basicConfig(level=logging.INFO) redis = RedisCluster(startup_nodes=[{"host": "xxx.yyy.clustercfg.zzz1.cache.amazonaws.com","port": "6379"}], decode_responses=True,skip_full_coverage_check=True) if redis.ping(): logging.info("Connected to Redis")
To run the program, enter the following command:
python ConnectClusterModeEnabled.py