<commit_before>"""
Utility methods for cache clear.
Used to somehow partially backport http://code.djangoproject.com/ticket/12671
to Django < 1.2
"""

from django.core.cache import cache
from django.db import connection

import shutil

###### clear functions

def clear_db(cache):
    cursor = connection.cursor()
    cursor.execute('DELETE FROM %s' % cache._table)

def clear_filebased(cache):
    try:
        shutil.rmtree(cache._dir)
    except (IOError, OSError):
        pass

def clear_memcached(cache):
    cache._cache.flush_all()

def clear_locmem(cache):
    cache._cache.clear()
    cache._expire_info.clear()

###### end of clear functions


# map

BACKEND_CLEAR_MAP = {
    'db' : clear_db,
    'dummy' : lambda x: x,
    'filebased' : clear_filebased,
    'memcached' : clear_memcached,
    'locmem' : clear_locmem,
}

# utility methods

def get_cache_class():
    return ''

def flush_django_cache():
    try:
        cache.clear()
    except AttributeError:
        # Django < 1.2, backport
        backend_name = cache.__module__.split(".")[-1:][0]
        
        if backend_name not in BACKEND_CLEAR_MAP:
            raise ValueError("Don't know how to clear cache for %s backend" % backend_name)

        BACKEND_CLEAR_MAP[backend_name](cache)<commit_msg>Allow specificationof non-default cache to clear<commit_after>"""
Utility methods for cache clear.
Used to somehow partially backport http://code.djangoproject.com/ticket/12671
to Django < 1.2
"""

from django.core.cache import cache
from django.db import connection

import shutil

###### clear functions

def clear_db(cache):
    cursor = connection.cursor()
    cursor.execute('DELETE FROM %s' % cache._table)

def clear_filebased(cache):
    try:
        shutil.rmtree(cache._dir)
    except (IOError, OSError):
        pass

def clear_memcached(cache):
    cache._cache.flush_all()

def clear_locmem(cache):
    cache._cache.clear()
    cache._expire_info.clear()

###### end of clear functions


# map

BACKEND_CLEAR_MAP = {
    'db' : clear_db,
    'dummy' : lambda x: x,
    'filebased' : clear_filebased,
    'memcached' : clear_memcached,
    'locmem' : clear_locmem,
}

# utility methods

def get_cache_class():
    return ''

def flush_django_cache(backend_name=None):
    try:
        cache.clear()
    except AttributeError:
        # Django < 1.2, backport
        backend_name = backend_name or cache.__module__.split(".")[-1:][0]

        if backend_name not in BACKEND_CLEAR_MAP:
            raise ValueError("Don't know how to clear cache for %s backend" % backend_name)

        BACKEND_CLEAR_MAP[backend_name](cache)
{'fraction_non_alphanumeric': 0.11400153022188217, 'fraction_numerical': 0.008416220351951033, 'mean_word_length': 3.3366500829187395, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5493977', 'n_tokens_mistral': 869, 'n_tokens_neox': 833, 'n_words': 208}