示例#1
0
def from_url(url, db=None, **kwargs):
    """Returns an active Redis client generated from the given database URL.

    Will attempt to extract the database id from the path url fragment, if
    none is provided.
    """
    return Redis.from_url(url, db, **kwargs)
示例#2
0
def from_url(url, db=None, **kwargs):
    """Returns an active Redis client generated from the given database URL.

    Will attempt to extract the database id from the path url fragment, if
    none is provided.
    """
    return Redis.from_url(url, db, **kwargs)
示例#3
0
def _get_current_redis_client() -> Redis:
    global _redis_clients
    client = _redis_clients.current_client

    if client is None:
        assert _connection_url is not None, "please set connection string first"
        client = _redis_clients.current_client = Redis.from_url(
            _connection_url)

    return client
示例#4
0
文件: redis.py 项目: jimzhan/tornext
    def __init__(self, urls, **settings):
        """
        Args:
            urls: list of connection urls for `redis.client.StrictRedis#from_url`.
            settings: additional settings for redis client.

        Attributes:
            mapping (dict): maintain the connection url & corresponding Redis client mapping.
            sharding: `tornext.sharding.Sharding` instance for dispatching requests.
        """
        self.sharding = Sharding(urls)
        self.mapping  = dict([(url, Redis.from_url(url)) for url in urls])
示例#5
0
def create_redis_client(url, pool_size=10):
    """
    Creates connection pool for running Redis commands.

    Parameters
    ----------
    url : str
        A URL to connect to redis.
    pool_size : int
        Size of connection pool.

    Returns
    -------
    client : redis.Redis
        A client to run redis command.
    """
    return Redis.from_url(url, max_connections=pool_size)
示例#6
0
        def __init__(self,
                     redis: Redis = None,
                     url: str = None,
                     host: str = '127.0.0.1',
                     port: int = 6379,
                     db: int = 0,
                     password: str = None,
                     logger_name: str = None):
            """
            Instantiates a new Redis Config Store using one of three methods:

            1. When a Redis instance is passed (using the parameter "redis"), it will simply reuse it

            2. When a Redis URL is passed (using the parameter "url"), it will try to establish a connection using said URL

            3. In all other cases, it will try to connect to a Redis instance with the provided data

            :param redis: Redis instance to use (default: None)
            :param url: Redis URL to connect to (default: None)
            :param host: Redis Host (default: 127.0.0.1)
            :param port: Redis Host Port (default: 6379)
            :param db: Redis Database (default: 0)
            :param password: Redis Host Password (default: None)
            :param logger_name: optional custom name provided in Log
            """
            super().__init__(logger_name if logger_name
                             else (url if url
                                   else ("Redis {}".format(host) if host
                                         else None)))

            # Setup instance vars
            self._redis: Redis = None

            if redis:
                # If Redis instance was provided, use it
                r = redis
            elif url:
                # If Redis URL was provided, create new instance with it
                r = Redis.from_url(url)
            else:
                # Else, try setting up a new Redis instance with primitive information
                r = RedisStore._build_redis(host, port, db, password)

            # Assign Redis instance to class
            self._redis = r
示例#7
0
def from_url(url, db=None, master_for_args={}, **kwargs):
    """
    Returns an active Redis client generated from the given database URL.

    Will attempt to extract the database id from the path url fragment, if
    none is provided.

    When url is starting with sentinels://, a sentinel object will be returned
    if no query parameter service_name in url, otherwise the master redis
    client will be returned. master_for_args will be applied to
    Sentinel.master_for.
    >>> import redis
    >>> redis.from_url('sentinels://node1:17700,node2:17700')
    Sentinel<sentinels=[node1:17700,node2:17700]>

    >>> redis.from_url(
        'sentinels://node1:17700,node2:17700?service_name=mymaster', db=1)
    StrictRedis<SentinelConnectionPool<service=mymaster(master)>

    >>> redis.from_url(
       'sentinels://node1:17700,node2:17700?service_name=mymaster&db=3', db=1)
    StrictRedis<SentinelConnectionPool<service=mymaster(master)>

    >>> redis.from_url(
        'sentinels://node1:17700,node2:17700?service_name=mymaster',
        db=1,
        master_for_args={'redis_class':redis.Redis})
    Redis<SentinelConnectionPool<service=mymaster(master)>
    """
    parse_result = urlparse(url)
    if parse_result.scheme == 'sentinels':
        from sentinel import Sentinel
        sentinel, db_from_url, service_name = Sentinel.from_url(url, **kwargs)

        if not service_name:
            return sentinel
        else:
            return sentinel.master_for(service_name,
                                       db=(db_from_url or db or 0),
                                       **master_for_args)
    from redis.client import Redis
    return Redis.from_url(url, db, **kwargs)
def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    db.init_app(app)
    migrate.init_app(app, db)
    login.init_app(app)
    mail.init_app(app)
    bootstrap.init_app(app)
    moment.init_app(app)
    babel.init_app(app)

    from app.errors import bp as errors_blueprint
    app.register_blueprint(errors_blueprint)

    from app.auth import bp as auth_blueprint
    app.register_blueprint(auth_blueprint, url_prefix='/auth')

    from app.main import bp as main_blueprint
    app.register_blueprint(main_blueprint)

    from app.api import bp as api_bp
    app.register_blueprint(api_bp, url_prefix='/api')

    app.elasticsearch = Elasticsearch([app.config['ELASTICSEARCH_URL']]) \
        if app.config['ELASTICSEARCH_URL'] else None

    app.redis = Redis.from_url(app.config['REDIS_URL'])
    app.task_queue = rq.Queue(app.config['MICROBLOG_QUEUE_NAME'],
                              connection=app.redis)

    if not app.debug and not app.testing:
        if app.config['MAIL_SERVER']:
            auth = None
            if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
                auth = (app.config['MAIL_USERNAME'],
                        app.config['MAIL_PASSWORD'])
            secure = None
            if app.config['MAIL_USE_TLS']:
                secure = ()
            mail_handler = SMTPHandler(
                mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
                fromaddr=f'no-reply@{app.config["MAIL_SERVER"]}',
                toaddrs=app.config['ADMINS'],
                subject='[Microblog] Failure',
                credentials=auth,
                secure=secure)
            mail_handler.setLevel(logging.ERROR)
            app.logger.addHandler(mail_handler)

        if app.config['LOG_TO_STDOUT']:
            stream_handler = logging.StreamHandler()
            stream_handler.setLevel(logging.INFO)
            app.logger.addHandler(stream_handler)
        else:
            if not os.path.exists('logs'):
                os.mkdir('logs')
            file_handler = RotatingFileHandler('logs/microblog.log',
                                               maxBytes=10240,
                                               backupCount=10)
            file_handler.setLevel(logging.INFO)
            file_handler.setFormatter(
                logging.Formatter('%(asctime)s %(levelname)s: %(message)s '
                                  '[in %(pathname)s:%(lineno)d]'))
            app.logger.addHandler(file_handler)

        app.logger.setLevel(logging.INFO)
        app.logger.info('Microblog startup')

    return app
示例#9
0
文件: bot.py 项目: aar118/CoAPartyBot
    if prefix_info:
        prefix = prefix_info['prefix']
    else:
        prefix_info = {
            'guild_id': guild_id,
            'prefix': prefix
        }
        await bot.db.prefixes.insert_one(prefix_info)
    return commands.when_mentioned_or(prefix)(bot, message)

bot = commands.Bot(command_prefix=get_prefix, activity=discord.Game('Default prefix: !?'))
bot.launch_time = datetime.utcnow()

for filename in os.listdir('./cogs'):
    if filename.endswith('.py'):
        bot.load_extension(f'cogs.{filename[:-3]}')

with open('settings.json', 'r') as f:
    settings = json.load(f)

token = settings['token']
bot.owner_id = settings['owner_id']
bot.leaderboards_api_url = settings['leaderboards_api_url']
motor_client = AsyncIOMotorClient(settings['mongo_uri'])
bot.db = motor_client['coa']
player_cache = Redis.from_url(settings['redis_url'], db=0)
max_page_cache = Redis.from_url(settings['redis_url'], db=1)
bot.player_cache = player_cache
bot.max_page_cache = max_page_cache
bot.run(token)
示例#10
0
 def __init__(self, redis_url):
     self._client = Redis.from_url(redis_url, decode_responses=True)