<commit_before>from hashlib import md5
import logging

from django.utils import six
from django.db.models import ObjectDoesNotExist
from django.db.models.signals import post_save, post_delete
from django.core.cache import cache
from django.core.cache.backends.dummy import DummyCache
from django.http import Http404
from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import smart_str
from django.conf import settings

from ella.utils import get_model

log = logging.getLogger('ella.core.cache.utils')

KEY_PREFIX = 'ella.obj'
CACHE_TIMEOUT = getattr(settings, 'CACHE_TIMEOUT', 10 * 60)


def invalidate_cache(sender, instance, **kwargs):
    invalidate_cache_for_object(instance)


def connect_invalidation_signals():
    post_save.connect(invalidate_cache)
    post_delete.connect(invalidate_cache)


def invalidate_cache_for_object(obj):
    key = _get_key(KEY_PREFIX, ContentType.objects.get_for_model(obj), pk=obj.pk, version_key=True)
    try:
        cache.incr(key)
    except ValueError:
        cache.set(key, 1, timeout=CACHE_TIMEOUT)


def normalize_key(key):
    if len(key) < 250:<commit_msg>used try except in invalidate_cache function for no errors during running migrations<commit_after>from hashlib import md5
import logging

from django.utils import six
from django.db.models import ObjectDoesNotExist
from django.db.models.signals import post_save, post_delete
from django.core.cache import cache
from django.core.cache.backends.dummy import DummyCache
from django.http import Http404
from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import smart_str
from django.conf import settings

from ella.utils import get_model

log = logging.getLogger('ella.core.cache.utils')

KEY_PREFIX = 'ella.obj'
CACHE_TIMEOUT = getattr(settings, 'CACHE_TIMEOUT', 10 * 60)


def invalidate_cache(sender, instance, **kwargs):
    try:
        invalidate_cache_for_object(instance)
    except Exception:
        log.warning("Can not invalidate cache for object", exc_info=True)


def connect_invalidation_signals():
    post_save.connect(invalidate_cache)
    post_delete.connect(invalidate_cache)


def invalidate_cache_for_object(obj):
    key = _get_key(KEY_PREFIX, ContentType.objects.get_for_model(obj), pk=obj.pk, version_key=True)
    try:
        cache.incr(key)
    except ValueError:
        cache.set(key, 1, timeout=CACHE_TIMEOUT)


def normalize_key(key):
    if len(key) < 250:
{'fraction_non_alphanumeric': 0.08252032520325203, 'fraction_numerical': 0.00975609756097561, 'mean_word_length': 5.198992443324937, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2192915', 'n_tokens_mistral': 770, 'n_tokens_neox': 748, 'n_words': 188}