compress message dictionaries in memcached using zlib.

This decreases the average size of the message dicts in memcached by
about half, without any significant change in the overall performance
of the query.  Since these message dicts are a significant fraction of
what we put in memcached, this seems like a worthwhile optimization.

(imported from commit 3896328074aa4344b8ac7c7ba7685f0a167ec7ad)
This commit is contained in:
Tim Abbott 2013-06-19 15:41:29 -04:00
parent 631a9048f7
commit 0cc9960ff5
1 changed files with 3 additions and 2 deletions

View File

@ -16,6 +16,7 @@ from django.contrib.sessions.models import Session
from django.utils.html import escape from django.utils.html import escape
from zephyr.lib.timestamp import datetime_to_timestamp from zephyr.lib.timestamp import datetime_to_timestamp
from django.db.models.signals import post_save from django.db.models.signals import post_save
import zlib
from bitfield import BitField from bitfield import BitField
import ujson import ujson
@ -275,10 +276,10 @@ def linebreak(string):
return string.replace('\n\n', '<p/>').replace('\n', '<br/>') return string.replace('\n\n', '<p/>').replace('\n', '<br/>')
def extract_message_dict(message_str): def extract_message_dict(message_str):
return ujson.loads(message_str) return ujson.loads(zlib.decompress(message_str))
def stringify_message_dict(message_dict): def stringify_message_dict(message_dict):
return ujson.dumps(message_dict) return zlib.compress(ujson.dumps(message_dict))
def to_dict_cache_key_id(message_id, apply_markdown, rendered_content=None): def to_dict_cache_key_id(message_id, apply_markdown, rendered_content=None):
return 'message_dict:%d:%d' % (message_id, apply_markdown) return 'message_dict:%d:%d' % (message_id, apply_markdown)