<commit_before>from sanic import Sanic
from aioredis import create_pool


class SanicRedis:
    def __init__(self, app: Sanic=None, redis_config: dict=None):
        self.app = app
        self.config = redis_config

        if app:
            self.init_app(app=app)

    def init_app(self, app: Sanic, redis_config: dict=None):
        self.app = app
        self.config = redis_config

        @app.listener('before_server_start')
        async def aio_redis_configure(_app, loop):
            _c = dict(loop=loop)
            if self.config:
                config = self.config
            else:
                config = _app.config.get('REDIS')
            for key in ['address', 'db', 'password', 'ssl', 'encoding', 'minsize',
                        'maxsize', 'create_connection_timeout']:
                if key in config:
                    _c.update({key: config.get(key)})
            _redis = await create_pool(**_c)

            _app.redis = _redis

        @app.listener('after_server_stop')
        async def close_redis(_app, loop):
            _app.redis.close()
            await _app.redis.wait_closed()

    async def acquire(self):
        with await getattr(self.app, 'redis') as r:
            return r<commit_msg>fixed high-level interface for newer aioredis<commit_after>from sanic import Sanic
from aioredis import create_pool


class SanicRedis:
    def __init__(self, app: Sanic=None, redis_config: dict=None):
        self.app = app
        self.config = redis_config

        if app:
            self.init_app(app=app)

    def init_app(self, app: Sanic, redis_config: dict=None):
        self.app = app
        self.config = redis_config

        @app.listener('before_server_start')
        async def aio_redis_configure(_app, loop):
            _c = dict(loop=loop)
            if self.config:
                config = self.config
            else:
                config = _app.config.get('REDIS')
            for key in ['address', 'db', 'password', 'ssl', 'encoding', 'minsize',
                        'maxsize', 'create_connection_timeout']:
                if key in config:
                    _c.update({key: config.get(key)})
            _redis = await create_redis_pool(**_c)

            _app.redis = _redis

        @app.listener('after_server_stop')
        async def close_redis(_app, loop):
            _app.redis.close()
            await _app.redis.wait_closed()

    async def acquire(self):
        with await getattr(self.app, 'redis') as r:
            return r
{'fraction_non_alphanumeric': 0.10444797458300238, 'fraction_numerical': 0.0, 'mean_word_length': 1.9256678281068524, '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': '6469049', 'n_tokens_mistral': 773, 'n_tokens_neox': 746, 'n_words': 180}