I’m doing some tests with memcached on my django project to understand if I can get benefit of it. But I stuck at unicode character caching. I read many article about it but still could not understand how can I make it work. Here is my code block:
def cache(request):
client = Client("127.0.0.1:11211")
cache_key = '123' # needs to be unique
cache_time = 86400 # time in seconds for cache to be valid
data = client.get(cache_key) # returns None if no key-value pair
if not data:
print("setting cache")
#data contains unicode characters - Throws an error
datafromdb = Subject.objects.all()
#unicode characters - Throws an error
unicodestr="abçüıö"
#encoded unicode characters - Working fine
encodedunicodestr=str.encode("abçüıö")
client.set(cache_key,unicodestr , cache_time)
else:
print("getting from cache")
return HttpResponse(data)
So If I use str.encode on text values It works fine and data can be cached.
But If I retrieve data from database(postgres) and this data contains unicode memcached throws an error.
I’m wondering if is there a way to use str.unicode on my queryset/list etc that returned from database or something else may be.