~homeworkprod/byceps

ref: 6083c64abf465c011eb6b8963e0fd7d010da0d58 byceps/byceps/redis.py -rw-r--r-- 791 bytes
6083c64a — Jochen Kupperschmidt Pre-select bank transfer as the default payment method for orders in the mark-as-paid form 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
byceps.redis
~~~~~~~~~~~~

Redis_ integration.

.. _Redis: http://redis.io/

:Copyright: 2006-2017 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from flask import _app_ctx_stack as stack
from redis import StrictRedis


EXTENSION_KEY = 'byceps_redis'

CONTEXT_ATTRIBUTE_NAME = 'redis_client'


class Redis:

    def init_app(self, app):
        url = app.config['REDIS_URL']
        self._client = StrictRedis.from_url(url)

        app.extensions[EXTENSION_KEY] = self

    @property
    def client(self):
        ctx = stack.top

        if ctx is not None:
            if not hasattr(ctx, CONTEXT_ATTRIBUTE_NAME):
                setattr(ctx, CONTEXT_ATTRIBUTE_NAME, self._client)

            return getattr(ctx, CONTEXT_ATTRIBUTE_NAME)


redis = Redis()