<commit_before>"""Cache management.

Functions in this module manage the Stan Program cache and related caches.
"""
import asyncio
import logging
import lzma
import os
import sqlite3

import appdirs


logger = logging.getLogger('httpstan')


async def init_cache(app):
    """Store reference to opened cache database in app.

    Objects stored in the aiohttp.web.Application instance can be accessed by
    all request handlers.

    This function is intended to be added to the ``on_startup`` functions
    associated with an ``aiohttp.web.Application``.

    This function is a coroutine.

    Arguments:
        app (aiohttp.web.Application): The current application.

    """
    cache_path = appdirs.user_cache_dir('httpstan')
    os.makedirs(cache_path, exist_ok=True)
    logging.info(f'Opening cache in `{cache_path}`.')
    conn = sqlite3.connect(os.path.join(cache_path, 'cache.db'), check_same_thread=False)
    # create tables if they do not exist
    conn.execute("""CREATE TABLE IF NOT EXISTS models (key BLOB PRIMARY KEY, value BLOB);""")
    conn.execute("""CREATE TABLE IF NOT EXISTS models (key BLOB PRIMARY KEY, value BLOB);""")
    app['db'] = conn


async def close_cache(app):
    """Close cache.

    This function is intended to be added to the ``on_cleanup`` functions<commit_msg>sqlite3 fixes: turn on write-ahead-log (#24)<commit_after>"""Cache management.

Functions in this module manage the Stan Program cache and related caches.
"""
import asyncio
import logging
import lzma
import os
import sqlite3

import appdirs


logger = logging.getLogger('httpstan')


async def init_cache(app):
    """Store reference to opened cache database in app.

    Objects stored in the aiohttp.web.Application instance can be accessed by
    all request handlers.

    This function is intended to be added to the ``on_startup`` functions
    associated with an ``aiohttp.web.Application``.

    This function is a coroutine.

    Arguments:
        app (aiohttp.web.Application): The current application.

    """
    cache_path = appdirs.user_cache_dir('httpstan')
    os.makedirs(cache_path, exist_ok=True)
    logging.info(f'Opening cache in `{cache_path}`.')
    conn = sqlite3.connect(os.path.join(cache_path, 'cache.sqlite3'), check_same_thread=False)
    # create tables if they do not exist
    conn.execute("""PRAGMA journal_mode=WAL;""")  # use write-ahead-log, available since sqlite 3.7.0
    with conn:
        conn.execute("""CREATE TABLE IF NOT EXISTS models (key BLOB PRIMARY KEY, value BLOB);""")
        conn.execute("""CREATE TABLE IF NOT EXISTS models (key BLOB PRIMARY KEY, value BLOB);""")
    app['db'] = conn


async def close_cache(app):
    """Close cache.

    This function is intended to be added to the ``on_cleanup`` functions
{'fraction_non_alphanumeric': 0.1002524341868013, 'fraction_numerical': 0.003966822935448972, 'mean_word_length': 3.875219683655536, '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': '7174580', 'n_tokens_mistral': 849, 'n_tokens_neox': 816, 'n_words': 292}