Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Removing control / space characters from the cache key in python

$
0
0

I have to remove control/space characters from my cache key. It is giving following error:

MemcachedKeyCharacterError: Control/space characters not allowed (key=' :1 :ZjE3MDJiNDY4NjI3OGJlY2ZmYzg4YmFhYmU2ZjRiN2E4MzU1NTMyYw==\n')

The code which I am using is:

class CachedAPIView(APIView): def get_object(self,request): s = hashlib.sha1() s.update('Messages') returnData=cache.get(base64.encodestring(s.hexdigest()),None) if not returnData: obj=function(self,request.data) s = hashlib.sha1() s.update('Messages') cache.set(base64.encodestring(s.hexdigest()),returnData) base64.encodestring(s.hexdigest()).strip()

PS. You dont need to calculate s.update('Messages') twice. You can even cash s.hexdigest() .

cache.set(…,returnData)

is wrong because you don't have returnData ― you checked its absense with if not returnData . Do you mean

cache.set(…,obj)

?


Viewing all articles
Browse latest Browse all 9596

Trending Articles