<commit_before>import logging
from django.core.cache import cache, get_cache
from datetime import datetime

def _get_discussion_cache():
    return get_cache('mongo_metadata_inheritance')


def get_discussion_cache_key(course_id):
    return 'discussion_items_{0}'.format(course_id)


def get_discussion_cache_entry(modulestore, course_id):
    cache_entry = None
    cache = _get_discussion_cache()
    
    if cache is not None:
        cache_entry = cache.get(get_discussion_cache_key(course_id), None)
        # @todo: add expiry here
    
    if cache_entry is None:
        cache_entry = generate_discussion_cache_entry(modulestore, course_id)

    return cache_entry.get('modules',[])


def generate_discussion_cache_entry(modulestore, course_id):
    components = course_id.split('/')
    all_discussion_modules = modulestore.get_items(['i4x', components[0], components[1], 'discussion', None], 
        course_id=course_id)

    cache = _get_discussion_cache()
    entry = {'modules': all_discussion_modules, 'timestamp': datetime.now()}
    logging.debug('**** entry = {0}'.format(entry))
    if cache is not None:
        cache.set(get_discussion_cache_key(course_id), entry)
    return entry


def modulestore_update_signal_handler(modulestore = None, course_id = None, location = None, **kwargs):
    """called when there is an write event in our modulestore
    """
    if location.category == 'discussion':
        logging.debug('******* got modulestore update signal. Regenerating discussion cache for {0}'.format(course_id))
        # refresh the cache entry if we've changed a discussion module<commit_msg>disable the discussion cache update signal until we get solution for pickling<commit_after>import logging
from django.core.cache import cache, get_cache
from datetime import datetime

def _get_discussion_cache():
    return get_cache('mongo_metadata_inheritance')


def get_discussion_cache_key(course_id):
    return 'discussion_items_{0}'.format(course_id)


def get_discussion_cache_entry(modulestore, course_id):
    cache_entry = None
    cache = _get_discussion_cache()
    
    if cache is not None:
        cache_entry = cache.get(get_discussion_cache_key(course_id), None)
        # @todo: add expiry here
    
    if cache_entry is None:
        cache_entry = generate_discussion_cache_entry(modulestore, course_id)

    return cache_entry.get('modules',[])


def generate_discussion_cache_entry(modulestore, course_id):
    # make this a NOP for now. We have to figure out how to pickle the result set
    return
    
    components = course_id.split('/')
    all_discussion_modules = modulestore.get_items(['i4x', components[0], components[1], 'discussion', None], 
        course_id=course_id)

    cache = _get_discussion_cache()
    entry = {'modules': all_discussion_modules, 'timestamp': datetime.now()}
    if cache is not None:
        cache.set(get_discussion_cache_key(course_id), entry)
    return entry


def modulestore_update_signal_handler(modulestore = None, course_id = None, location = None, **kwargs):
    """called when there is an write event in our modulestore
    """
    return
    if location.category == 'discussion':
        logging.debug('******* got modulestore update signal. Regenerating discussion cache for {0}'.format(course_id))
        # refresh the cache entry if we've changed a discussion module
{'fraction_non_alphanumeric': 0.09528049866429207, 'fraction_numerical': 0.003265063817156426, 'mean_word_length': 4.332278481012659, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6074939', 'n_tokens_mistral': 1029, 'n_tokens_neox': 976, 'n_words': 264}