Django view with redis cluster not working
I'm using AWS elasticache redis cluster. However, I'm unable to implement my current view to utilize the cluster. Here's my view:
redis_conn = redis.Redis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=0, decode_responses=True)
def orders(request):
logger.info(f"Accessing the orders view for user: {request.user.username}")
# Fetch user-specific cached shipment data
processed_shipments_display_str = redis_conn.get(f"processed_shipments_display_{request.user.id}")
all_shipments_display = []
last_updated_at = "Not Available"
if processed_shipments_display_str:
cache_data = json.loads(processed_shipments_display_str)
all_shipments_display = cache_data.get('shipments', [])
last_updated_at = cache_data.get('timestamp', 'Not available')
else:
all_shipments_display = []
last_updated_at = 'Not available'
return render(request, 'app/orders.html', {
'all_shipments': all_shipments_display,
'last_updated_at': last_updated_at
})
Everytime I try to access the page it just loads and nothing ever happens. I'm unable to use redis-py-cluster (which worked initially) since my redis version is incompatible with channels-redis and this.
I also tried using
startup_nodes = [{"host": settings.REDIS_HOST, "port": settings.REDIS_PORT}]
redis_conn = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)
but then I keep getting the error
AttributeError at /app/orders/
'dict' object has no attribute 'name'
Here's my settings.py:
REDIS_HOST = os.getenv('REDIS_HOST', 'clustercfg.rediscluster.m0rn9y.use1.cache.amazonaws.com')
REDIS_PORT = os.getenv('REDIS_PORT', 6379)
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.pubsub.RedisPubSubChannelLayer",
"CONFIG": {
"hosts": [{
"address": "rediss://clustercfg.rediscluster.m0rn9y.use1.cache.amazonaws.com:6379",
"ssl_cert_reqs": None,
}]
},
},
}
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "rediss://clustercfg.rediscluster.m0rn9y.use1.cache.amazonaws.com:6379",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"SSL_CERT_REQS": None,
}
}
}
Any help is appreciated. Thanks.