<commit_before>#
#	dp for Tornado
#		<NAME> (<EMAIL>)
#		2014.10.23
#


from engine.cache import CacheDriver as dpCacheDriver
from ..model import ModelSingleton as dpModelSingleton
from ..engine import Engine as dpEngine


class MemoryCacheDriver(dpEngine, dpCacheDriver):
    @staticmethod
    def getpool(config_dsn=None, host=None, port=None, database=None, user=None, password=None):
        MemoryCacheDriver._create_table(config_dsn)
        driver = MemoryCacheDriver(config_dsn=config_dsn)
        driver.config_dsn = config_dsn
        return driver

    @staticmethod
    def _table_name(config_dsn):
        return config_dsn.replace('/', '_').replace('.', '_')

    @staticmethod
    def _create_table(config_dsn):
        table_name = MemoryCacheDriver._table_name(config_dsn)
        index_name = '%s_index' % table_name

        proxy = dpModelSingleton().begin(config_dsn, cache=True)
        proxy.execute("""
            CREATE TABLE %(table_name)s (
                key TEXT PRIMARY KEY ASC NOT NULL,
                val TEXT,
                expire_at integer DEFAULT NULL
            )
        """ % {'table_name': table_name})
        proxy.execute('CREATE INDEX %(index_name)s ON %(table_name)s (expire_at);' % {'table_name': table_name, 'index_name': index_name})
        proxy.commit()

    def getconn(self):
        return self

    def get(self, key):
        result = dpModelSingleton().row(
            'SELECT '
            '   * '
            'FROM {table_name} WHERE key = ?'.replace('{table_name}', MemoryCacheDriver._table_name(self.config_dsn)),
            key, self.config_dsn, cache=True)

        if result:
            if result['expire_at'] and result['expire_at'] < self.helper.datetime.current_time():
                return None
            else:
                return result['val']

        else:
            return None

    def set(self, key, val, expire_in=None):
        if expire_in is not None:
            expire_in = self.helper.datetime.current_time() + expire_in

        if expire_in:
            return dpModelSingleton().execute(
                'INSERT OR REPLACE INTO {table_name} (key, val, expire_at) '
                '   VALUES (?, ?, ?)'.replace('{table_name}', MemoryCacheDriver._table_name(self.config_dsn)),
                (key, val, expire_in), self.config_dsn, cache=True)

        else:
            return dpModelSingleton().execute(
                'INSERT OR REPLACE INTO {table_name} (key, val, expire_at) '
                '   VALUES (?, ?, (SELECT expire_at FROM {table_name} WHERE key = ?))'.replace('{table_name}', MemoryCacheDriver._table_name(self.config_dsn)),
                (key, val, key), self.config_dsn, cache=True)

    def increase(self, key, amount, expire_in=None):
        if expire_in is not None:
            expire_in = self.helper.datetime.current_time() + expire_in

        if expire_in:
            return dpModelSingleton().execute(
                'UPDATE {table_name} '
                '   SET '
                '       val = val + ?,'
                '       expire_at = ? '
                '   WHERE '
                '       key = ?'.replace('{table_name}', MemoryCacheDriver._table_name(self.config_dsn)),
                (amount, expire_in, key), self.config_dsn, cache=True)

        else:
            return dpModelSingleton().execute(
                'UPDATE {table_name} '
                '   SET '
                '       val = val + ?'
                '   WHERE '
                '       key = ?'.replace('{table_name}', MemoryCacheDriver._table_name(self.config_dsn)),
                (amount, key), self.config_dsn, cache=True)<commit_msg>In-Memory cache supported caching list and, not only str, int.<commit_after>#
#	dp for Tornado
#		<NAME> (<EMAIL>)
#		2014.10.23
#


from engine.cache import CacheDriver as dpCacheDriver
from ..model import ModelSingleton as dpModelSingleton
from ..engine import Engine as dpEngine


class MemoryCacheDriver(dpEngine, dpCacheDriver):
    @staticmethod
    def getpool(config_dsn=None, host=None, port=None, database=None, user=None, password=None):
        MemoryCacheDriver._create_table(config_dsn)
        driver = MemoryCacheDriver(config_dsn=config_dsn)
        driver._config_dsn = config_dsn
        return driver

    @staticmethod
    def _table_name(config_dsn):
        return config_dsn.replace('/', '_').replace('.', '_')

    @staticmethod
    def _create_table(config_dsn):
        table_name = MemoryCacheDriver._table_name(config_dsn)

        proxy = dpModelSingleton().begin(config_dsn, cache=True)
        proxy.execute("""
            CREATE TABLE %(table_name)s (
                key TEXT PRIMARY KEY ASC NOT NULL,
                val TEXT,
                type INT,
                expire_at integer DEFAULT NULL
            )
        """ % {'table_name': table_name})
        proxy.execute('CREATE INDEX %(table_name)s_idx ON %(table_name)s (expire_at);' % {'table_name': table_name})
        proxy.commit()

    @staticmethod
    def _types():
        return {
            1: str,
            2: int,
            3: list,
            4: dict
        }

    @staticmethod
    def _types_required_json():
        return {
            3: list,
            4: dict
        }

    @staticmethod
    def _type_to_key(t):
        for key, val in MemoryCacheDriver._types().items():
            if t == val:
                return key

        return None

    @staticmethod
    def _key_to_type(key):
        types = MemoryCacheDriver._types()

        return types[key] if key in types else None

    @staticmethod
    def _val_to_type(val):
        return MemoryCacheDriver._type_to_key(val.__class__)

    def getconn(self):
        return self

    def get(self, key):
        result = dpModelSingleton().row(
            'SELECT '
            '   * '
            'FROM {table_name} WHERE key = ?'.replace('{table_name}', MemoryCacheDriver._table_name(self._config_dsn)),
            key, self._config_dsn, cache=True)

        if result:
            if result['expire_at'] and result['expire_at'] < self.helper.datetime.current_time():
                return None
            else:
                if result['type'] in MemoryCacheDriver._types_required_json():
                    try:
                        import json
                        return json.loads(result['val'])

                    except ValueError:
                        return False

                else:
                    return result['val']

        else:
            return None

    def set(self, key, val, expire_in=None):
        if expire_in is not None:
            expire_in = self.helper.datetime.current_time() + expire_in

        type = MemoryCacheDriver._val_to_type(val)

        if isinstance(val, (list, dict)):
            import json
            val = json.dumps(val, separators=(',', ':'))

        if expire_in:
            return dpModelSingleton().execute(
                'INSERT OR REPLACE INTO {table_name} (key, val, type, expire_at) '
                '   VALUES (?, ?, ?, ?)'
                .replace('{table_name}', MemoryCacheDriver._table_name(self._config_dsn)),
                (key, val, type, expire_in), self._config_dsn, cache=True)

        else:
            return dpModelSingleton().execute(
                'INSERT OR REPLACE INTO {table_name} (key, val, type, expire_at) '
                '   VALUES (?, ?, ?, (SELECT expire_at FROM {table_name} WHERE key = ?))'
                .replace('{table_name}', MemoryCacheDriver._table_name(self._config_dsn)),
                (key, val, type, key), self._config_dsn, cache=True)

    def increase(self, key, amount, expire_in=None):
        if expire_in is not None:
            expire_in = self.helper.datetime.current_time() + expire_in

        if expire_in:
            return dpModelSingleton().execute(
                'UPDATE {table_name} '
                '   SET '
                '       val = val + ?,'
                '       expire_at = ? '
                '   WHERE '
                '       key = ?'.replace('{table_name}', MemoryCacheDriver._table_name(self._config_dsn)),
                (amount, expire_in, key), self._config_dsn, cache=True)

        else:
            return dpModelSingleton().execute(
                'UPDATE {table_name} '
                '   SET '
                '       val = val + ?'
                '   WHERE '
                '       key = ?'.replace('{table_name}', MemoryCacheDriver._table_name(self._config_dsn)),
                (amount, key), self._config_dsn, cache=True)
{'fraction_non_alphanumeric': 0.1098529068409993, 'fraction_numerical': 0.0025682932523931824, 'mean_word_length': 1.9199045671438308, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '5289009', 'n_tokens_mistral': 2577, 'n_tokens_neox': 2435, 'n_words': 547}