aio-libs/aiomcache

Store zlib compressed value

kikdevops opened this issue · 4 comments

I'm trying to set key with zlib compressed value with python3

resp = 'some message'
res = zlib.compress(resp.encode())

mc = aiomcache.Client("slivei_memcached", 11211)
await mc.set(key, res.decode(), 60)
await mc.close()`

and got an error:
'utf-8' codec can't decode byte 0x9c in position 1: invalid start byte

Does anyone know how to solve this problem?

The library uses text memcache protocol, that's why keys and values are URF-8 encoded.
You can apply base64 encoding as a workaround.

I changed code to

resp = 'some message'
res = zlib.compress(resp.encode())
z = base64.b64encode(res)

mc = aiomcache.Client("slivei_memcached", 11211)
await mc.set(key, z, 60)
await mc.close()

but get an error:

'bytes' object has no attribute 'encode'

Indeed it hasn't. Convert z into a str.

It works, thanks a lot.

    res = zlib.compress(an.encode())
    z = base64.b64encode(z).decode()