<commit_before>#!/usr/bin/env python3

from akvo.cache import cache_with_key, delete_cache_data
from akvo.rest.serializers import ProjectDirectorySerializer
from akvo.rsr.models.project import Project, project_directory_cache_key

# FIXME: Should we use a DB cache?
PROJECT_DIRECTORY_CACHE = 'default'


@cache_with_key(project_directory_cache_key, timeout=None, cache_name=PROJECT_DIRECTORY_CACHE)
def serialized_project(project_id):
    project = Project.objects.only(
        'id', 'title', 'subtitle',
        'primary_location__id',
        'primary_organisation__id',
        'primary_organisation__name',
        'primary_organisation__long_name'<commit_msg>[#4187] Start using the database cache for project directory data<commit_after>#!/usr/bin/env python3

from akvo.cache import cache_with_key, delete_cache_data
from akvo.rest.serializers import ProjectDirectorySerializer
from akvo.rsr.models.project import Project, project_directory_cache_key

PROJECT_DIRECTORY_CACHE = 'database'


# NOTE: The data doesn't timeout, since we expect the data to be invalidated
# when the data is updated from the project editor. Also, the script to fill the
# cache should be able to clear the cache and create new values.
@cache_with_key(project_directory_cache_key, timeout=None, cache_name=PROJECT_DIRECTORY_CACHE)
def serialized_project(project_id):
    project = Project.objects.only(
        'id', 'title', 'subtitle',
        'primary_location__id',
        'primary_organisation__id',
        'primary_organisation__name',
        'primary_organisation__long_name'
{'fraction_non_alphanumeric': 0.07702100572883513, 'fraction_numerical': 0.0038192234245703373, 'mean_word_length': 5.364372469635628, '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': '4501495', 'n_tokens_mistral': 463, 'n_tokens_neox': 455, 'n_words': 119}