相关文章推荐
八块腹肌的酱牛肉  ·  Search Page 1/15: ...·  3 月前    · 
聪明的鞭炮  ·  实验室发布智能化工大模型-辽宁滨海实验室·  1 年前    · 
威武的菠菜  ·  metc治疗效果_39健康网·  1 年前    · 
文质彬彬的红金鱼  ·  广场舞网盘资源 - 百度·  1 年前    · 
逼格高的橙子  ·  SPSS工具:时间序列分析---商业销量预测 ...·  2 年前    · 
Code  ›  将json压缩为存储在基于内存的存储(如redis或memcache )中,哪种方法是最好的?开发者社区
redis gzip
https://cloud.tencent.com/developer/ask/sof/178103
坚强的煎鸡蛋
2 年前
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
提问

问 将json压缩为存储在基于内存的存储(如redis或memcache )中,哪种方法是最好的?

Stack Overflow用户
提问于 2013-03-20 22:06:47
EN

要求:具有2-3层嵌套的Python对象,包含基本数据类型,如整数、字符串、列表和字典。(没有日期等),需要在redis中根据键存储为json。为了减少内存占用,将json压缩为字符串的最佳方法是什么?目标对象不是很大,平均只有1000个小元素,转换为JSON时大约有15000个字符。

例如:

>>> my_dict
{'details': {'1': {'age': 13, 'name': 'dhruv'}, '2': {'age': 15, 'name': 'Matt'}}, 'members': ['1', '2']}
>>> json.dumps(my_dict)
'{"details": {"1": {"age": 13, "name": "dhruv"}, "2": {"age": 15, "name": "Matt"}}, "members": ["1", "2"]}'
### SOME BASIC COMPACTION ###
>>> json.dumps(my_dict, separators=(',',':'))
'{"details":{"1":{"age":13,"name":"dhruv"},"2":{"age":15,"name":"Matt"}},"members":["1","2"]}'

1/有没有其他更好的方法来压缩json以节省redis中的内存(也可以确保之后的轻量级解码)。

2/候选人的msgpack http://msgpack.org/会有多好?

3/我是否也应该考虑像泡菜这样的选项?

5 29.1K 0 票数 22
EN
python
json
redis
msgpack

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-03-21 00:34:26

我们只是使用 gzip 作为一个压缩器。

import gzip
import cStringIO
def decompressStringToFile(value, outputFile):
  decompress the given string value (which must be valid compressed gzip
  data) and write the result in the given open file.
  stream = cStringIO.StringIO(value)
  decompressor = gzip.GzipFile(fileobj=stream, mode='r')
  while True:  # until EOF
    chunk = decompressor.read(8192)
    if not chunk:
      decompressor.close()
      outputFile.close()
      return 
    outputFile.write(chunk)
def compressFileToString(inputFile):
  read the given open file, compress the data and return it as string.
  stream = cStringIO.StringIO()
  compressor = gzip.GzipFile(fileobj=stream, mode='w')
  while True:  # until EOF
    chunk = inputFile.read(8192)
    if not chunk:  # EOF?
      compressor.close()
      return stream.getvalue()
    compressor.write(chunk)

在我们的用例中,我们将结果存储为文件,正如您可以想象的那样。要只使用内存中的字符串,也可以使用 cStringIO.StringIO() 对象作为文件的替代。

票数 19
EN

Stack Overflow用户

发布于 2018-08-27 05:31:40

基于上面@Alfe的 answer ,这里有一个版本,它将内容保存在内存中(用于网络I/O任务)。我还做了一些更改以支持Python3。

import gzip
from io import StringIO, BytesIO
def decompressBytesToString(inputBytes):
  decompress the given byte array (which must be valid 
  compressed gzip data) and return the decoded text (utf-8).
  bio = BytesIO()
  stream = BytesIO(inputBytes)
  decompressor = gzip.GzipFile(fileobj=stream, mode='r')
  while True:  # until EOF
    chunk = decompressor.read(8192)
    if not chunk:
      decompressor.close()
      bio.seek(0)
      return bio.read().decode("utf-8")
    bio.write(chunk)
  return None
def compressStringToBytes(inputString):
  read the given string, encode it in utf-8,
  compress the data and return it as a byte array.
  bio = BytesIO()
  bio.write(inputString.encode("utf-8"))
  bio.seek(0)
  stream = BytesIO()
  compressor = gzip.GzipFile(fileobj=stream, mode='w')
  while True:  # until EOF
    chunk = bio.read(8192)
    if not chunk:  # EOF?
 
推荐文章
八块腹肌的酱牛肉  ·  Search Page 1/15: Neoster Global Inc Reviews
3 月前
聪明的鞭炮  ·  实验室发布智能化工大模型-辽宁滨海实验室
1 年前
威武的菠菜  ·  metc治疗效果_39健康网
1 年前
文质彬彬的红金鱼  ·  广场舞网盘资源 - 百度
1 年前
逼格高的橙子  ·  SPSS工具:时间序列分析---商业销量预测_spss销售分析-CSDN博客
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号