revsys/django-friendship

MemcacheServerError: b'object too large for cache' in FriendshipManager

Closed this issue · 0 comments

acjh commented

Stack trace:

Traceback (most recent call last):
  ...
  File "/path/to/friendship/models.py, line 209, in sent_requests
    cache.set(key, requests)
  File "/path/to/django/core/cache/backends/memcached.py, line 82, in set
    if not self._cache.set(key, value, self.get_backend_timeout(timeout)):
  File "/path/to/pymemcache/client/hash.py, line 358, in set
    return self._run_cmd('set', key, False, *args, **kwargs)
  File "/path/to/pymemcache/client/hash.py, line 334, in _run_cmd
    return self._safely_run_func(
  File "/path/to/pymemcache/client/hash.py, line 214, in _safely_run_func
    result = func(*args, **kwargs)
  File "/path/to/pymemcache/client/base.py, line 462, in set
    return self._store_cmd(b'set', {key: value}, expire, noreply,
  File "/path/to/pymemcache/client/base.py, line 1107, in _store_cmd
    self._raise_errors(line, name)
  File "/path/to/pymemcache/client/base.py, line 940, in _raise_errors
    raise MemcacheServerError(error)
MemcacheServerError: b'object too large for cache'

This is caused by FriendshipManager methods doing select_related queries, which result in large cache items:

  • requests
  • sent_requests
  • unread_requests
  • read_requests
  • rejected_requests
  • unrejected_requests

qs = FriendshipRequest.objects.select_related(
"from_user", "to_user"
).filter(from_user=user)
requests = list(qs)
cache.set(key, requests)