Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store and get django model object in redis

I setup Django & Redis project and try to store django model object in Redis. If I use django.core.cache as below, card2 is properly set to django object. However, if I use raw redis conenction provided by django_redis, it gets string representation of django model object.

I set both keys as below. How can I use raw redis connection to get card object itself instead of its string repr? I need it because I'd like to use mget, zrange like methods of redis.py.

from django.core.cache import cache
from django_redis import get_redis_connection

con = get_redis_connection("default")
card = Card.objects.all()[0]

key = "card_" + str(card.id)

con.delete(key)
cache.delete(key)

con.set(key, card)
cache.set(key, card)

card1 = con.get(key)
card2 = cache.get(key)
like image 540
brsbilgic Avatar asked Oct 20 '22 21:10

brsbilgic


1 Answers

You have to convert your response from redis to your python object manually.

Hint: use pickle, here are examples: 1, 2

like image 113
danielgpm Avatar answered Oct 29 '22 14:10

danielgpm