cache: Use getattr to access _cache.

`_cache` is not an attribute defined on `BaseCache`, but an
implementation detail of django_bmemcache.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Zixuan James Li 2022-07-18 18:14:23 -04:00 committed by Tim Abbott
parent beb09cdf38
commit 22ebf701aa
3 changed files with 18 additions and 5 deletions

View File

@ -6,6 +6,7 @@ import shutil
from mimetypes import guess_type
from typing import Any, Dict, Iterable, List, Optional, Tuple
import bmemcached
import orjson
from bs4 import BeautifulSoup
from django.conf import settings
@ -876,7 +877,9 @@ def import_uploads(
process_avatars(record)
else:
connection.close()
cache._cache.disconnect_all()
_cache = getattr(cache, "_cache")
assert isinstance(_cache, bmemcached.Client)
_cache.disconnect_all()
with multiprocessing.Pool(processes) as p:
for out in p.imap_unordered(process_avatars, records):
pass

View File

@ -3,6 +3,7 @@ import multiprocessing
import os
from mimetypes import guess_type
import bmemcached
from django.conf import settings
from django.core.cache import cache
from django.db import connection
@ -40,7 +41,9 @@ def transfer_avatars_to_s3(processes: int) -> None:
_transfer_avatar_to_s3(user)
else: # nocoverage
connection.close()
cache._cache.disconnect_all()
_cache = getattr(cache, "_cache")
assert isinstance(_cache, bmemcached.Client)
_cache.disconnect_all()
with multiprocessing.Pool(processes) as p:
for out in p.imap_unordered(_transfer_avatar_to_s3, users):
pass
@ -71,7 +74,9 @@ def transfer_message_files_to_s3(processes: int) -> None:
_transfer_message_files_to_s3(attachment)
else: # nocoverage
connection.close()
cache._cache.disconnect_all()
_cache = getattr(cache, "_cache")
assert isinstance(_cache, bmemcached.Client)
_cache.disconnect_all()
with multiprocessing.Pool(processes) as p:
for out in p.imap_unordered(_transfer_message_files_to_s3, attachments):
pass
@ -101,7 +106,9 @@ def transfer_emoji_to_s3(processes: int) -> None:
_transfer_emoji_to_s3(realm_emoji)
else: # nocoverage
connection.close()
cache._cache.disconnect_all()
_cache = getattr(cache, "_cache")
assert isinstance(_cache, bmemcached.Client)
_cache.disconnect_all()
with multiprocessing.Pool(processes) as p:
for out in p.imap_unordered(_transfer_emoji_to_s3, realm_emojis):
pass

View File

@ -1,5 +1,6 @@
from typing import Any
import bmemcached
from django.conf import settings
from django.core.cache import cache
from django.core.management.base import BaseCommand
@ -14,4 +15,6 @@ class Command(BaseCommand):
def handle(self, *args: Any, **options: Any) -> None:
assert settings.DEVELOPMENT
UserMessage.objects.all().update(flags=F("flags").bitand(~UserMessage.flags.read))
cache._cache.flush_all()
_cache = getattr(cache, "_cache")
assert isinstance(_cache, bmemcached.Client)
_cache.flush_all()