<commit_before>from pathlib import Path
import sqlite3
from datetime import datetime, timedelta
from .schema import cache_meta as cache_meta_schema

class CacheBase:
    should_refresh: bool = False
    cache_meta_filename: str = 'cache-meta.db'
    refresh_interval: int = 86400

    def init(self, store_path: Path):
        self.meta_path = store_path / self.cache_meta_filename
        if not self.meta_path.exists():
            self.should_refresh = True
            store_path.mkdir(parents=True, exist_ok=True)
            self.cache_meta_conn = self.get_cache_meta_conn(self.meta_path)
            self.cache_meta_conn.execute(cache_meta_schema)
            self.cache_meta_conn.commit()
            self.update_next_refresh()
        else:
            self.cache_meta_conn = self.get_cache_meta_conn(self.meta_path)
            cur = self.cache_meta_conn.cursor()
            cur.execute("SELECT next_refresh FROM cache_meta")
            next_refresh, = cur.fetchone()
            self.should_refresh = next_refresh < datetime.now()
            cur.close()
    
    def __del__(self):
        self.cache_meta_conn.close()
    
    def update_next_refresh(self):
        self.cache_meta_conn = self.get_cache_meta_conn(self.meta_path)
        self.cache_meta_conn.execute(cache_meta_schema)
        self.cache_meta_conn.execute("DELETE FROM cache_meta")
        self.cache_meta_conn.execute('INSERT INTO cache_meta VALUES (?)', (datetime.now()+timedelta(seconds=self.refresh_interval), ))
        self.cache_meta_conn.commit()
    
    @staticmethod
    def get_cache_meta_conn(db_path: Path) -> sqlite3.Connection:
        return sqlite3.connect(db_path, detect_types=sqlite3.PARSE_DECLTYPES)<commit_msg>genericize get conn<commit_after>from pathlib import Path
import sqlite3
from datetime import datetime, timedelta
from .schema import cache_meta as cache_meta_schema

class CacheBase:
    should_refresh: bool = False
    cache_meta_filename: str = 'cache-meta.db'
    refresh_interval: int = 86400

    def init(self, store_path: Path):
        self.meta_path = store_path / self.cache_meta_filename
        if not self.meta_path.exists():
            self.should_refresh = True
            store_path.mkdir(parents=True, exist_ok=True)
            self.cache_meta_conn = self.get_db_conn(self.meta_path)
            self.cache_meta_conn.execute(cache_meta_schema)
            self.cache_meta_conn.commit()
            self.update_next_refresh()
        else:
            self.cache_meta_conn = self.get_db_conn(self.meta_path)
            cur = self.cache_meta_conn.cursor()
            cur.execute("SELECT next_refresh FROM cache_meta")
            next_refresh, = cur.fetchone()
            self.should_refresh = next_refresh < datetime.now()
            cur.close()
    
    def __del__(self):
        self.cache_meta_conn.close()
    
    def update_next_refresh(self):
        self.cache_meta_conn = self.get_db_conn(self.meta_path)
        self.cache_meta_conn.execute(cache_meta_schema)
        self.cache_meta_conn.execute("DELETE FROM cache_meta")
        self.cache_meta_conn.execute('INSERT INTO cache_meta VALUES (?)', (datetime.now()+timedelta(seconds=self.refresh_interval), ))
        self.cache_meta_conn.commit()
    
    @staticmethod
    def get_db_conn(db_path: Path) -> sqlite3.Connection:
        return sqlite3.connect(db_path, detect_types=sqlite3.PARSE_DECLTYPES)
{'fraction_non_alphanumeric': 0.08871915393654524, 'fraction_numerical': 0.005287896592244419, 'mean_word_length': 3.0200708382526567, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '7114936', 'n_tokens_mistral': 1054, 'n_tokens_neox': 1032, 'n_words': 175}