Example #1
0
def getUserDataInfo(username):
    blogList = Blog.objects.filter(auther=User.objects.get(
        username=username)).filter(draft=False)
    followkey = generateKey(username, RedisKey['FOLLOWKEY'])
    fanskey = generateKey(username, RedisKey['FANSKEY'])
    followcount = 0
    fanscount = 0
    blogCount = 0
    commentCount = 0
    pool = ConnectionPool(host='localhost', port='6379', db=0)
    redis = StrictRedis(connection_pool=pool)
    if redis.exists(followkey):
        followcount = redis.scard(followkey)
    if redis.exists(fanskey):
        fanscount = redis.scard(fanskey)

    for blog in blogList:
        blogCount = blogCount + 1
        commentCount = commentCount + blog.commentcount
    messagekey = generateKey(username, RedisKey['UNREADMSGKEY'])
    if redis.exists(messagekey):
        msgcount = redis.llen(messagekey)
    else:
        msgcount = 0
    pool.disconnect()
    userdataInfo = {
        'followcount': followcount,
        'fanscount': fanscount,
        'blogcount': blogCount,
        'commentcount': commentCount,
        'msgcount': msgcount
    }
    return userdataInfo
Example #2
0
    def __init__(self, name='huey', blocking=True, read_timeout=1,
                 connection_pool=None, url=None, client_name=None,
                 **connection_params):

        if Redis is None:
            raise ConfigurationError('"redis" python module not found, cannot '
                                     'use Redis storage backend. Run "pip '
                                     'install redis" to install.')

        # Drop common empty values from the connection_params.
        for p in ('host', 'port', 'db'):
            if p in connection_params and connection_params[p] is None:
                del connection_params[p]

        if sum(1 for p in (url, connection_pool, connection_params) if p) > 1:
            raise ConfigurationError(
                'The connection configuration is over-determined. '
                'Please specify only one of the following: '
                '"url", "connection_pool", or "connection_params"')

        if url:
            connection_pool = ConnectionPool.from_url(
                url, decode_components=True)
        elif connection_pool is None:
            connection_pool = ConnectionPool(**connection_params)

        self.pool = connection_pool
        self.conn = self.redis_client(connection_pool=connection_pool)
        self.connection_params = connection_params

        super(RedisStorage, self).__init__(name, blocking, read_timeout, **connection_params)
Example #3
0
 def __init__(self):
     conf = cper.read_yaml_file('redis')
     host = conf['host']
     port = conf['port']
     db = conf['db']
     max_conn = conf['max_conn']
     self.redis_pool = ConnectionPool(host=host, port=port, max_connections=max_conn, db=db)
Example #4
0
class DBAdaptor(object):
    def __init__(self):
        conf = cper.read_yaml_file('redis')
        host = conf['host']
        port = conf['port']
        db = conf['db']
        max_conn = conf['max_conn']
        self.redis_pool = ConnectionPool(host=host, port=port, max_connections=max_conn, db=db)

    def create_conn(self, db_type='influxdb'):
        if db_type == 'redis':
            return RedisDB(self.redis_pool)
        elif db_type == 'influxdb':
            return InfluxDB()
        elif db_type == 'mysql':
            return MysqlDB()
        else:
            print('Error! Please specify the type of database.')
            return None

    def saveall(self, data):
        pass

    def createtab(self):
        pass

    def get_data(self):
        print('factory')

    def __del__(self):
        self.redis_pool.disconnect()
Example #5
0
def clearRedis(keys):
    pool = ConnectionPool(host='localhost', port='6379', db=0)
    redis = StrictRedis(connection_pool=pool)
    for key in keys:
        if redis.exists(key):
            redis.delete(key)
    pool.disconnect()
Example #6
0
def follow(request, followusername):
    try:
        currentUser = request.user
    except Users.DoesNotExist:
        return HttpResponseRedirect(reverse('users:pleaselogin'))
    except KeyError:
        return HttpResponseRedirect(reverse('users:pleaselogin'))
    currentUsername = currentUser.username
    if currentUser:
        followkey = generateKey(currentUsername, RedisKey['FOLLOWKEY'])
        fanskey = generateKey(followusername, RedisKey['FANSKEY'])
        pool = ConnectionPool(host='localhost', port='6379', db=0)
        redis = StrictRedis(connection_pool=pool)
        if redis.exists(followkey):
            if redis.sismember(followkey, followusername):
                return HttpResponseRedirect(
                    reverse('blogs:content',
                            kwargs={'blogId': request.session['currblogId']}))
            else:
                redis.sadd(followkey, followusername)
                redis.sadd(fanskey, currentUsername)
        else:
            redis.sadd(followkey, followusername)
            redis.sadd(fanskey, currentUsername)
        pool.disconnect()
    return HttpResponseRedirect(
        reverse('blogs:content',
                kwargs={'blogId': request.session['currblogId']}))
Example #7
0
 def __init__(self):
     if REDISPASSWORD:
         self.pool = ConnectionPool(host=REDISHOST,
                                    port=REDISPORT,
                                    password=REDISPASSWORD)
     else:
         self.pool = ConnectionPool(host=REDISHOST,
                                    port=REDISPORT)
Example #8
0
    def __init__(self, connection_pool=None, url=None, **connection_params):
        if url:
            connection_pool = ConnectionPool.from_url(url,
                                                      decode_components=True)
        elif connection_pool is None:
            connection_pool = ConnectionPool(**connection_params)

        self.pool = connection_pool
        self.conn = self.redis_client(connection_pool=connection_pool)
Example #9
0
File: app.py Project: 8dspaces/wiwi
def configure_extensions(app):
    # flask-MongoEngine
    db.init_app(app)
    db.register_connection(**app.config.get('ORDER_DB_CONFIG'))
    db.register_connection(**app.config.get('INVENTORY_DB_CONFIG'))
    db.register_connection(**app.config.get('CART_DB_CONFIG'))
    db.register_connection(**app.config.get('CONTENT_DB_CONFIG'))
    db.register_connection(**app.config.get('LOG_DB_CONFIG'))

    mongo_inventory.init_app(app, config_prefix='MONGO_INVENTORY')

    redis.connection_pool = ConnectionPool(**app.config.get('REDIS_CONFIG'))
    session_redis.connection_pool = ConnectionPool(
        **app.config.get('SESSION_REDIS'))

    # server side session
    app.session_interface = RedisSessionInterface(session_redis)

    # flask-mail
    mail.init_app(app)
    mail.app = app

    # flask-cache
    cache.init_app(app)

    # flask-bcrypt
    bcrypt.init_app(app)

    # flask-babel
    babel.init_app(app)

    # flask-assets
    assets.init_app(app)

    # flask_debugtoolbar
    toolbar.init_app(app)

    # flask-login (not configured will raise 401 error)
    login_manager.login_view = 'frontend.login'
    # login_manager.refresh_view = 'frontend.reauth'

    @login_manager.user_loader
    def load_user(id):
        import application.models as Models
        return Models.User.objects(id=id, is_deleted=False).first()

    login_manager.init_app(app)
    login_manager.login_message = _('Please log in to access this page.')
    login_manager.needs_refresh_message = _(
        'Please reauthenticate to access this page.')

    # flask-principal (must be configed after flask-login!!!)
    principal.init_app(app)

    @identity_loaded.connect_via(app)
    def on_identity_loaded(sender, identity):
        principal_on_identity_loaded(sender, identity)
Example #10
0
def redis_init():
    settings = get_project_settings()
    if settings["REDIS_PARAMS"]:
        pool = ConnectionPool(host=settings["REDIS_HOST"], port=settings["REDIS_PORT"],
                              password=settings["REDIS_PARAMS"]['password'])
    else:
        pool = ConnectionPool(host=settings["REDIS_HOST"], port=settings["REDIS_PORT"])
    conn = Redis(connection_pool=pool)
    return conn
Example #11
0
 def get_redis_queue(self):
     redis_config = self.get_config("redis")
     try:
         pool = ConnectionPool(host=redis_config["host"])
         pool.make_connection().connect()
         queue = Queue("high", connection=Redis(connection_pool=pool))
         return queue
     except RedisError as e:
         self.logger.exception(e)
         raise DarterException("Not possible connection on Redis")
Example #12
0
class PoolRedisDispatcher(AbstractRedisDispatcher):
    redis_pool = None

    def __init__(self, *pool_args, **pool_kwargs):
        self.redis_pool = ConnectionPool(*pool_args, **pool_kwargs)
        AbstractRedisDispatcher.__init__(self)

    def get_redis(self):
        return Redis(connection_pool=self.redis_pool)

    def close(self):
        self.redis_pool.disconnect()
Example #13
0
 def __init__(self,
              host: str,
              port: int = 6379,
              db: int = 0,
              password: str = None,
              patterns: Iterable[str] = ('.*', )):
     if password is None:
         self.redis_pool = ConnectionPool(host=host, port=port, db=db)
     else:
         self.redis_pool = ConnectionPool(host=host,
                                          port=port,
                                          db=db,
                                          password=password)
     self.patterns = patterns
Example #14
0
 def __init__(self, redis_host, redis_port, redis_password, db=0):
     if isinstance(redis_host, list) and len(redis_host) == 1:
         redis_host = redis_host[0]
     if isinstance(redis_host, str):
         conn_pool = ConnectionPool(host=redis_host,
                                    port=redis_port,
                                    db=db,
                                    password=redis_password)
         self.redis_eng = StrictRedis(
             connection_pool=conn_pool,
             max_connections=10,
         )
     elif isinstance(redis_host, list):
         if isinstance(redis_port, int):
             startup_nodes = [{
                 "host": host,
                 "port": redis_port
             } for host in redis_host]
         elif isinstance(redis_port, list):
             startup_nodes = [{
                 "host": host,
                 "port": port
             } for host, port in zip(redis_host, redis_port)]
         self.redis_eng = StrictRedisCluster(startup_nodes=startup_nodes,
                                             password=redis_password)
Example #15
0
 def __init__(self):
     if not RedisConnect.pool:
         RedisConnect.pool = ConnectionPool(host=DevConfig.REDIS_URI,
                                            port=DevConfig.REDIS_PORT,
                                            db=0,
                                            decode_responses=True)
     self.r_connect = StrictRedis(connection_pool=RedisConnect.pool)
Example #16
0
 def setUpClass(cls):
     pool = ConnectionPool()
     cls.r = Redis(connection_pool=pool)
     cls.r.flushdb()
     cls.rr = RedisResource(connection_pool=pool,
                            schema=_schema,
                            id_schema=_schema.properties["id"])
Example #17
0
    def __init__(self, manager):
        self.manager = manager
        self._logger = logging.getLogger("redis_backend.backend")
        settings = manager.settings
        port = settings.get('REDIS_PORT')
        host = settings.get('REDIS_HOST')
        self._min_hosts = settings.get('BC_MIN_HOSTS')
        self._max_requests_per_host = settings.get('BC_MAX_REQUESTS_PER_HOST')

        self.queue_partitions = settings.get('SPIDER_FEED_PARTITIONS')
        self._logger.info("RedisBackend started with {} partitions".format(
            self.queue_partitions))
        self.pool = ConnectionPool(host=host, port=port, db=0)
        self._metadata = None
        self._queue = None
        self._states = None
Example #18
0
 def __init__(self):
     self.pool = ConnectionPool(
         host=REDIS_HOST,
         port=REDIS_PORT,
         password=REDIS_PASSWD
     )
     self.redisTemplate = StrictRedis(connection_pool=self.pool)
Example #19
0
    def __init__(self, *args, **kwargs):
        self.rooms = kwargs.pop('rooms', [])
        self.nickname = kwargs.pop('nickname', 'Dropbot')
        self.cmd_prefix = kwargs.pop('cmd_prefix', '!')
        self.kos_url = kwargs.pop('kos_url', 'http://kos.cva-eve.org/api/')
        self.hidden_commands = ['cmd_prefix']
        self.last_killdate = datetime.utcnow()
        self.kill_corps = [int(x) for x in kwargs.pop('kill_corps', [])]
        self.kills_disabled = kwargs.pop('kills_disabled', '0') == '1'
        self.kills_muted = False
        self.office_api_key_keyid = kwargs.pop('office_api_keyid', None)
        self.office_api_key_vcode = kwargs.pop('office_api_vcode', None)

        self.redis_pool = ConnectionPool.from_url(
            kwargs.pop('redis_url', 'redis://localhost:6379/0'))
        self.redis = Redis(connection_pool=self.redis_pool)
        self.map = Map.from_json(pkgutil.get_data('dropbot', 'data/map.json'))

        jid = kwargs.pop('jid', None)
        password = kwargs.pop('password', None)

        super(DropBot, self).__init__(jid, password)

        self.register_plugin('xep_0030')  # Service Discovery
        self.register_plugin('xep_0045')  # Multi-User Chat
        self.register_plugin('xep_0199')  # XMPP Ping

        # Basic bot auto config
        self.auto_subscribe = False
        self.auto_authorize = True

        # Handlers
        self.add_event_handler('session_start', self.handle_session_start)
        self.add_event_handler('message', self.handle_message)
Example #20
0
 def open_connection(self, host, port, **params):
     pool_key = self._format_pool_key(host, port)
     if pool_key not in self.connection_pools:
         self.connection_pools[pool_key] = ConnectionPool(
             host=host, port=port, db=self.db, password=self.dbpass)
     self.db_conn = StrictRedis(
         connection_pool=self.connection_pools[pool_key], **params)
Example #21
0
 def __init__(self, host, db, pwd):
     self.pool = ConnectionPool(host=host,
                                port=6379,
                                db=db,
                                password=pwd,
                                decode_responses=True)
     self.rdb = Redis(connection_pool=self.pool)
Example #22
0
def get_redis_pool(config):
    url = config.get('session.url', '172.23.16.12:6379')

    host, port = url.split(':')
    redispool = ConnectionPool(host=host, port=int(port))

    return redispool
Example #23
0
 def make(self, dependency_resources):
     host = '127.0.0.1'
     # Use a broken port to simulate Redis being unavailable.
     port = 9999
     db = 1  # Use DB number 1 instead of 0 for testing purposes.
     connectionPool = ConnectionPool(host=host, port=port, db=db)
     setCacheConnectionPool(connectionPool)
Example #24
0
 def clean(self, client):
     host = '127.0.0.1'
     # Use a broken port to simulate Redis being unavailable.
     port = 9999
     db = 1  # Use DB number 1 instead of 0 for testing purposes.
     connectionPool = ConnectionPool(host=host, port=port, db=db)
     setCacheConnectionPool(connectionPool)
Example #25
0
 def __init__(self) -> None:
     self.logger = logging.getLogger(f'{self.__class__.__name__}')
     self.logger.setLevel(get_config('generic', 'loglevel'))
     self.redis_pool: ConnectionPool = ConnectionPool(
         connection_class=UnixDomainSocketConnection,
         path=get_socket_path('indexing'),
         decode_responses=True)
Example #26
0
 def redis_client(self):
     pool = ConnectionPool(host=RedisConfigs.HOST.value,
                           port=RedisConfigs.PORT.value,
                           db=RedisConfigs.DB.value,
                           password=RedisConfigs.PASSWORD.value,
                           decode_responses=True)
     return Redis(connection_pool=pool)
Example #27
0
def get_redis_client():
    """
    获取一个redis连接
    :return:
    """
    server_url = settings.REDIS_SERVER_URL
    return StrictRedis(connection_pool=ConnectionPool.from_url(server_url))
Example #28
0
    def __init__(self, debug=False):
        settings = {
            "cookie_secret":
            "61oEdfTzKXdQAGaYddkL5fgEdf123mGeJJFfusYh7EQnp2fXdTP1o/Vo=",
            'login_url': '/login.html',
            'xsrf_cookies': False,
            'max_age_days': 0.1,
            'debug': debug,
        }
        handles = [
            #(r'/ansible/status', CheckHandler),
            # (r'/ansible/task', TaskHandler),
        ]
        self.async_session_pool = ClientPool(max_size=100,
                                             client_timeout=30,
                                             host=options.redis_host,
                                             port=options.redis_port,
                                             db=options.redis_session_db,
                                             autoconnect=True)

        self.yum_source_pool = ClientPool(max_size=100,
                                          client_timeout=30,
                                          host=options.redis_host,
                                          port=options.redis_port,
                                          db=options.redis_yum_source_db,
                                          autoconnect=True)

        self.sync_session_pool = ConnectionPool(max_connections=10,
                                                host=options.redis_host,
                                                port=options.redis_port,
                                                db=options.redis_session_db)

        super(App, self).__init__(handles, **settings)
Example #29
0
    def __init__(self, index_name, host='localhost', port=6379):
        self.host = host
        self.port = port
        self.index_name = index_name

        self.redis = Redis(
            connection_pool = ConnectionPool(host=host, port=port))
Example #30
0
def list():
    #列表方法l开头,少数r开头
    pool = ConnectionPool(host='localhost', port=6379, password="", db=1)
    redis = StrictRedis(connection_pool=pool)

    redis.rpush('lst', 1, 2, 3, 4, 5)  #右边加列表值
    redis.lpush('lst', 0)  #左边加列列表值
Example #31
0
def connect_to_redis():
    u'''
    (Lazily) connect to Redis.

    The connection is set up but not actually established. The latter
    happens automatically once the connection is used.

    :returns: A lazy Redis connection.
    :rtype: ``redis.Redis``

    .. seealso:: :py:func:`is_redis_available`
    '''
    global _connection_pool
    if _connection_pool is None:
        url = config.get(REDIS_URL_SETTING_NAME, REDIS_URL_DEFAULT_VALUE)
        log.debug(u'Using Redis at {}'.format(url))
        _connection_pool = ConnectionPool.from_url(url)
    return Redis(connection_pool=_connection_pool)
Example #32
0
def create_redis(registry: Registry, connection_url=None, redis_client=StrictRedis, max_connections=16, **redis_options) -> StrictRedis:
    """Sets up Redis connection pool once at the start of a process.

    Connection pool life cycle is the same as Pyramid registry which is the life cycle of a process (all threads).

    :param max_connections: Default per-process connection pool limit
    """

    # if no url passed, try to get it from pyramid settings
    url = registry.settings.get('redis.sessions.url') if connection_url is None else connection_url
    # otherwise create a new connection
    if url is not None:
        # remove defaults to avoid duplicating settings in the `url`
        redis_options.pop('password', None)
        redis_options.pop('host', None)
        redis_options.pop('port', None)
        redis_options.pop('db', None)
        # the StrictRedis.from_url option no longer takes a socket
        # argument. instead, sockets should be encoded in the URL if
        # used. example:
        #     unix://[:password]@/path/to/socket.sock?db=0
        redis_options.pop('unix_socket_path', None)

        # connection pools are also no longer a valid option for
        # loading via URL
        redis_options.pop('connection_pool', None)

        process_name = os.getpid()
        thread_name = threading.current_thread().name

        logger.info("Creating a new Redis connection pool. Process %s, thread %s, max_connections %d", process_name, thread_name, max_connections)

        connection_pool = ConnectionPool.from_url(url, max_connections=max_connections, **redis_options)
        redis = StrictRedis(connection_pool=connection_pool)
    else:
        raise RuntimeError("Redis connection options missing. Please configure redis.sessions.url")

    return redis
Example #33
0
    def __init__(self, *args, **kwargs):
        self.rooms = kwargs.pop('rooms', [])
        self.nickname = kwargs.pop('nickname', 'Dropbot')
        self.cmd_prefix = kwargs.pop('cmd_prefix', '!')
        self.kos_url = kwargs.pop('kos_url', 'http://kos.cva-eve.org/api/')
        self.hidden_commands = ['cmd_prefix']
        self.last_killdate = datetime.utcnow()
        self.kill_corps = [int(x) for x in kwargs.pop('kill_corps', [])]
        self.kills_disabled = kwargs.pop('kills_disabled', '0') == '1'
        self.kills_muted = False
        self.office_api_key_keyid = kwargs.pop('office_api_keyid', None)
        self.office_api_key_vcode = kwargs.pop('office_api_vcode', None)
        self.market_systems = kwargs.pop('market_systems', ['Jita', 'Amarr', 'Rens', 'Dodixie', 'Hek'])

        if 'redis_url' in kwargs:
            self.redis_pool = ConnectionPool.from_url(kwargs.pop('redis_url', 'redis://localhost:6379/0'))
            self.redis = Redis(connection_pool=self.redis_pool)
        else:
            logging.warning('No DROPBOT_REDIS_URL defined, EVE API calls will not be cached!')
            self.redis = None
        self.map = Map.from_json(pkgutil.get_data('dropbot', 'data/map.json'))

        jid = kwargs.pop('jid', None)
        password = kwargs.pop('password', None)

        super(DropBot, self).__init__(jid, password)

        self.register_plugin('xep_0030')  # Service Discovery
        self.register_plugin('xep_0045')  # Multi-User Chat
        self.register_plugin('xep_0199')  # XMPP Ping

        # Basic bot auto config
        self.auto_subscribe = False
        self.auto_authorize = True

        # Handlers
        self.add_event_handler('session_start', self.handle_session_start)
        self.add_event_handler('message', self.handle_message)
Example #34
0
    def __init__(self, name='huey', blocking=True, read_timeout=1,
                 connection_pool=None, url=None, client_name=None,
                 **connection_params):

        if Redis is None:
            raise ConfigurationError('"redis" python module not found, cannot '
                                     'use Redis storage backend. Run "pip '
                                     'install redis" to install.')

        if sum(1 for p in (url, connection_pool, connection_params) if p) > 1:
            raise ConfigurationError(
                'The connection configuration is over-determined. '
                'Please specify only one of the the following: '
                '"url", "connection_pool", or "connection_params"')

        if url:
            connection_pool = ConnectionPool.from_url(
                url, decode_components=True)
        elif connection_pool is None:
            connection_pool = ConnectionPool(**connection_params)

        self.pool = connection_pool
        self.conn = self.redis_client(connection_pool=connection_pool)
        self.connection_params = connection_params
        self._pop = self.conn.register_script(SCHEDULE_POP_LUA)

        self.name = self.clean_name(name)
        self.queue_key = 'huey.redis.%s' % self.name
        self.schedule_key = 'huey.schedule.%s' % self.name
        self.result_key = 'huey.results.%s' % self.name
        self.error_key = 'huey.errors.%s' % self.name

        if client_name is not None:
            self.conn.client_setname(client_name)

        self.blocking = blocking
        self.read_timeout = read_timeout
Example #35
0
_signal_encoder = import_string(settings.WEBSOCKET_SIGNAL_ENCODER)
_topic_serializer = import_string(settings.WEBSOCKET_TOPIC_SERIALIZER)

__values = {
    "host": settings.WEBSOCKET_REDIS_HOST,
    "port": ":%s" % settings.WEBSOCKET_REDIS_PORT
    if settings.WEBSOCKET_REDIS_PORT
    else "",
    "db": settings.WEBSOCKET_REDIS_DB,
    "password": "******" % settings.WEBSOCKET_REDIS_PASSWORD
    if settings.WEBSOCKET_REDIS_PASSWORD
    else "",
}
redis_connection_pool = ConnectionPool.from_url(
    "redis://%(password)s%(host)s%(port)s/%(db)s" % __values
)


def get_websocket_redis_connection():
    """Return a valid Redis connection, using a connection pool."""
    return StrictRedis(connection_pool=redis_connection_pool)


def set_websocket_topics(request, *topics):
    """Use it in a Django view for setting websocket topics. Any signal sent to one of these topics will be received
by the client.

    :param request: :class:`django.http.request.HttpRequest`
    :param topics: list of topics that will be subscribed by the websocket (can be any Python object).
    """
Example #36
0
# CONNECT TO redis METHOD 1) 
redis = StrictRedis(host='localhost', port=6397, db=0)

# CONNECT TO redis METHOD 2) 
pool = ConnectionPool(host='localhost', port=6397, db=0)
redis = StrictRedis(ConnectionPool)



# CONNECT TO redis METHOD 3)  : via URL
# redis = StrictRedis.from_url(url='redis://*****:*****@localhost:6379/1')
redis = StrictRedis(connection_pool=pool)






# ------------------------------------------------------
# OP demo 

# 1)  simple set key-value and query 
import redis
r = redis.Redis(host='127.0.0.1', port=6379)
r.set('key1', 'abc')
print (r.get('key1'))   
Example #37
0
 def from_url(cls, url, db=None, **kwargs):
     namespace = kwargs.pop('namespace')
     connection_pool = ConnectionPool.from_url(url, db=db, **kwargs)
     return cls(connection_pool=connection_pool, namespace=namespace)