Compress API results before caching them

Some result sets may be larger than the default cache's 1 MB max item size. This change is a bandaid meant to allow us to resume use of memcached while we work on a longer-term fix.

LEARNER-682
This commit is contained in:
Renzo Lucioni
2017-04-25 12:50:38 -04:00
parent 602b737b87
commit ef606d6f0a

View File

@@ -8,6 +8,7 @@ from django.core.exceptions import ImproperlyConfigured
from edx_rest_api_client.client import EdxRestApiClient
from provider.oauth2.models import Client
from openedx.core.lib.cache_utils import zpickle, zunpickle
from openedx.core.lib.token_utils import JwtBuilder
@@ -47,10 +48,11 @@ def get_edx_api_data(api_config, user, resource, api=None, resource_id=None,
if cache_key:
cache_key = '{}.{}'.format(cache_key, resource_id) if resource_id is not None else cache_key
cache_key += '.zpickled'
cached = cache.get(cache_key)
if cached:
return cached
return zunpickle(cached)
try:
if not api:
@@ -89,7 +91,8 @@ def get_edx_api_data(api_config, user, resource, api=None, resource_id=None,
return no_data
if cache_key:
cache.set(cache_key, results, api_config.cache_ttl)
zdata = zpickle(results)
cache.set(cache_key, zdata, api_config.cache_ttl)
return results