Python redis.Redis() Examples

The following are 30 code examples of redis.Redis(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module redis , or try the search function .
Example #1
Source File: redisession.py    From video2commons with GNU General Public License v3.0 8 votes vote down vote up
def save_session(self, app, session, response):
        """Save session to Redis."""
        domain = self.get_cookie_domain(app)
        path = url_for('main', _external=False)

        if not session:
            self.redis.delete(self.prefix + session.sid)
            if session.modified:
                response.delete_cookie(app.session_cookie_name,
                                       domain=domain, path=path)
        else:
            redis_exp = self.get_redis_expiration_time(app, session)
            cookie_exp = self.get_expiration_time(app, session)
            if session.modified:
                val = self.serializer.dumps(dict(session))
                self.redis.setex(self.prefix + session.sid,
                                 int(redis_exp.total_seconds()), val)
            else:
                self.redis.expire(self.prefix + session.sid,
                                  int(redis_exp.total_seconds()))
            response.set_cookie(app.session_cookie_name, session.sid,
                                expires=cookie_exp, httponly=True,
                                domain=domain, path=path, secure=True) 
Example #2
Source File: stats_handler.py    From scrapy-cluster with MIT License 6 votes vote down vote up
def handle(self, dict):
        '''
        Processes a vaild stats request

        @param dict: a valid dictionary object
        '''
        # format key
        key = "statsrequest:{stats}:{appid}".format(
                stats=dict['stats'],
                appid=dict['appid'])

        self.redis_conn.set(key, dict['uuid'])

        dict['parsed'] = True
        dict['valid'] = True
        self.logger.info('Added stat request to Redis', extra=dict) 
Example #3
Source File: rate_limit.py    From api-pycon2014 with MIT License 6 votes vote down vote up
def __init__(self, key_prefix, limit, per):
        global redis
        if redis is None and current_app.config['USE_RATE_LIMITS']:
            if current_app.config['TESTING']:
                redis = FakeRedis()
            else:
                redis = Redis()

        self.reset = (int(time.time()) // per) * per + per
        self.key = key_prefix + str(self.reset)
        self.limit = limit
        self.per = per
        p = redis.pipeline()
        p.incr(self.key)
        p.expireat(self.key, self.reset + self.expiration_window)
        self.current = min(p.execute()[0], limit) 
Example #4
Source File: cache.py    From lambda-packs with MIT License 6 votes vote down vote up
def __init__(self, host='localhost', port=6379, password=None,
                 db=0, default_timeout=300, key_prefix=None, **kwargs):
        BaseCache.__init__(self, default_timeout)
        if host is None:
            raise ValueError('RedisCache host parameter may not be None')
        if isinstance(host, string_types):
            try:
                import redis
            except ImportError:
                raise RuntimeError('no redis module found')
            if kwargs.get('decode_responses', None):
                raise ValueError('decode_responses is not supported by '
                                 'RedisCache.')
            self._client = redis.Redis(host=host, port=port, password=password,
                                       db=db, **kwargs)
        else:
            self._client = host
        self.key_prefix = key_prefix or '' 
Example #5
Source File: redis.py    From cachelib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, host='localhost', port=6379, password=None,
                 db=0, default_timeout=300, key_prefix=None, **kwargs):
        BaseCache.__init__(self, default_timeout)
        if host is None:
            raise ValueError('RedisCache host parameter may not be None')
        if isinstance(host, string_types):
            try:
                import redis
            except ImportError:
                raise RuntimeError('no redis module found')
            if kwargs.get('decode_responses', None):
                raise ValueError('decode_responses is not supported by '
                                 'RedisCache.')
            self._client = redis.Redis(host=host, port=port, password=password,
                                       db=db, **kwargs)
        else:
            self._client = host
        self.key_prefix = key_prefix or '' 
Example #6
Source File: redispub.py    From mqttwarn with Eclipse Public License 2.0 6 votes vote down vote up
def plugin(srv, item):
    """ redispub. Expects addrs to contain (channel) """

    srv.logging.debug("*** MODULE=%s: service=%s, target=%s", __file__, item.service, item.target)

    host = item.config.get('host', 'localhost')
    port = int(item.config.get('port', 6379))

    try:
        rp = redis.Redis(host, port)
    except Exception as e:
        srv.logging.warn("Cannot connect to redis on %s:%s : %s" % (host, port, e))
        return False

    channel = item.addrs[0]
    text = item.message

    try:
        rp.publish(channel, text)
    except Exception as e:
        srv.logging.warn("Cannot publish to redis on %s:%s : %s" % (host, port, e))
        return False

    return True 
Example #7
Source File: straw_app.py    From straw with MIT License 6 votes vote down vote up
def clear_user(self, uid):
        redis_connection = redis.Redis(connection_pool=self.app.pool)
        # print("Trying to clean for user {0}".format(uid))
        # find all the queries to which the user is subscribed
        # and remove them from the subscribers list for each query.
        for qid in redis_connection.lrange(uid+"-queries", 0, -1):
            try:
                self.app.user_channels[qid].remove(uid)
            except KeyError:
                pass
                
        # remove the user-queries
        redis_connection.delete(uid+"-queries")

        # remove the stored results
        redis_connection.delete(uid) 
Example #8
Source File: __init__.py    From automatron with Apache License 2.0 6 votes vote down vote up
def connect(self):
        ''' Open Connection to Redis '''
        if "password" not in self.config['datastore']['plugins']['redis'].keys():
            password = None
        else:
            password = self.config['datastore']['plugins']['redis']['password']
        try:
            self.dbc = redis.Redis(
                host=self.config['datastore']['plugins']['redis']['host'],
                port=self.config['datastore']['plugins']['redis']['port'],
                password=None,
                db=self.config['datastore']['plugins']['redis']['db'])
        except Exception as e:
            raise Exception("Failed to connect to Redis: {0}".format(e.message))
        self.initialize_db()
        return True 
Example #9
Source File: cache.py    From jbox with MIT License 6 votes vote down vote up
def __init__(self, host='localhost', port=6379, password=None,
                 db=0, default_timeout=300, key_prefix=None, **kwargs):
        BaseCache.__init__(self, default_timeout)
        if isinstance(host, string_types):
            try:
                import redis
            except ImportError:
                raise RuntimeError('no redis module found')
            if kwargs.get('decode_responses', None):
                raise ValueError('decode_responses is not supported by '
                                 'RedisCache.')
            self._client = redis.Redis(host=host, port=port, password=password,
                                       db=db, **kwargs)
        else:
            self._client = host
        self.key_prefix = key_prefix or '' 
Example #10
Source File: kafka_monitor.py    From scrapy-cluster with MIT License 6 votes vote down vote up
def _setup_stats(self):
        '''
        Sets up the stats collection
        '''
        self.stats_dict = {}

        redis_conn = redis.Redis(host=self.settings['REDIS_HOST'],
                                 port=self.settings['REDIS_PORT'],
                                 db=self.settings.get('REDIS_DB'))

        try:
            redis_conn.info()
            self.logger.debug("Connected to Redis in StatsCollector Setup")
            self.redis_conn = redis_conn
        except ConnectionError:
            self.logger.warn("Failed to connect to Redis in StatsCollector"
                             " Setup, no stats will be collected")
            return

        if self.settings['STATS_TOTAL']:
            self._setup_stats_total(redis_conn)

        if self.settings['STATS_PLUGINS']:
            self._setup_stats_plugins(redis_conn) 
Example #11
Source File: header.py    From PyOne with Mozilla Public License 2.0 6 votes vote down vote up
def CheckServer():
    mongo_cmd='lsof -i:27017 | grep LISTEN'
    redis_cmd='lsof -i:6379 | grep LISTEN'
    p1=subprocess.Popen(mongo_cmd,shell=True,stdout=subprocess.PIPE)
    p2=subprocess.Popen(redis_cmd,shell=True,stdout=subprocess.PIPE)
    r1=len(p1.stdout.readlines())
    r2=len(p2.stdout.readlines())
    msg=''
    if r1==0:
        msg+='<p><B>MongoDB未运行<B></p>'
    if r2==0:
        msg+='<p><B>Redis未运行<B></p>'
    p1.terminate()
    p2.terminate()
    if r1==0 or r2==0:
        return msg,False
    else:
        return msg,True 
Example #12
Source File: celery_task.py    From release-bot with GNU General Public License v3.0 6 votes vote down vote up
def handle_pr(webhook_payload, db):
    """
    Handler for merged PR
    :param webhook_payload: json data from webhook
    :param db: Redis instance
    :return:
    """
    release_bot, logger = set_configuration(webhook_payload, db=db, issue=False)

    logger.info("Resolving opened PR")
    release_bot.git.pull()
    try:
        release_bot.load_release_conf()
        if release_bot.find_newest_release_pull_request():
            release_bot.make_new_github_release()
            # Try to do PyPi release regardless whether we just did github release
            # for case that in previous iteration (of the 'while True' loop)
            # we succeeded with github release, but failed with PyPi release
            release_bot.make_new_pypi_release()
    except ReleaseException as exc:
        logger.error(exc)

    msg = ''.join(release_bot.github.comment)
    release_bot.project.pr_comment(release_bot.new_release.pr_number, msg)
    release_bot.github.comment = []  # clean up 
Example #13
Source File: action_handler.py    From scrapy-cluster with MIT License 6 votes vote down vote up
def handle(self, dict):
        '''
        Processes a vaild action request

        @param dict: a valid dictionary object
        '''
        # format key
        key = "{action}:{spiderid}:{appid}".format(
                action=dict['action'],
                spiderid=dict['spiderid'],
                appid=dict['appid'])

        if "crawlid" in dict:
            key = key + ":" + dict['crawlid']

        self.redis_conn.set(key, dict['uuid'])

        dict['parsed'] = True
        dict['valid'] = True
        self.logger.info('Added action to Redis', extra=dict) 
Example #14
Source File: zookeeper_handler.py    From scrapy-cluster with MIT License 6 votes vote down vote up
def setup(self, settings):
        '''
        Setup redis and tldextract
        '''
        self.extract = tldextract.TLDExtract()
        self.redis_conn = redis.Redis(host=settings['REDIS_HOST'],
                                      port=settings['REDIS_PORT'],
                                      db=settings.get('REDIS_DB'))

        try:
            self.redis_conn.info()
            self.logger.debug("Connected to Redis in ZookeeperHandler")
        except ConnectionError:
            self.logger.error("Failed to connect to Redis in ZookeeperHandler")
            # plugin is essential to functionality
            sys.exit(1) 
Example #15
Source File: cache.py    From orion-server with MIT License 6 votes vote down vote up
def set(self, key, value, ttl):
        """
        Set the value for a key. Dark writes to the backup in-memory store are always performed
        to synchronize the state of the in-memory store with Redis, so that read failovers do not
        sacrifice the consistency of the underlying data.

        :param key: Raw key.
        :param value: Associated value.
        :param ttl: Time to live, in milliseconds.
        """
        try:
            return self.redis.set(key, value, px=ttl)
        except (ConnectionError, TimeoutError):
            pass
        finally:
            return self.memory.set(key, value, ttl) 
Example #16
Source File: celery_task.py    From release-bot with GNU General Public License v3.0 6 votes vote down vote up
def handle_issue(webhook_payload, db):
    """
    Handler for newly opened issues
    :param webhook_payload: json data from webhook
    :param db: Redis instance
    :return:
    """
    release_bot, logger = set_configuration(webhook_payload, db=db, issue=True)

    logger.info("Resolving opened issue")
    release_bot.git.pull()
    try:
        release_bot.load_release_conf()
        if (release_bot.new_release.trigger_on_issue and
                release_bot.find_open_release_issues()):
            if release_bot.new_release.labels is not None:
                release_bot.project.add_issue_labels(
                    release_bot.new_pr.issue_number,
                    release_bot.new_release.labels)
            release_bot.make_release_pull_request()
    except ReleaseException as exc:
        logger.error(exc) 
Example #17
Source File: redisession.py    From video2commons with GNU General Public License v3.0 6 votes vote down vote up
def open_session(self, app, request):
        """Get session from Redis / start a new session."""
        sid = request.cookies.get(app.session_cookie_name)
        if sid:
            val = self.redis.get(self.prefix + sid)
            if val is not None:
                try:
                    data = self.serializer.loads(val)
                except ValueError:
                    pass
                else:
                    return self.session_class(data, sid=sid)

        # SECURITY: If the session id is invalid, we create a new one, to
        # prevent cookie-injection.
        # https://www.usenix.org/system/files/conference/usenixsecurity15/sec15-paper-zheng.pdf
        sid = self.generate_sid()
        return self.session_class(sid=sid, new=True) 
Example #18
Source File: scraper_handler.py    From scrapy-cluster with MIT License 6 votes vote down vote up
def setup(self, settings):
        '''
        Setup redis and tldextract
        '''
        self.extract = tldextract.TLDExtract()
        self.redis_conn = redis.Redis(host=settings['REDIS_HOST'],
                                      port=settings['REDIS_PORT'],
                                      db=settings.get('REDIS_DB'))

        try:
            self.redis_conn.info()
            self.logger.debug("Connected to Redis in ScraperHandler")
        except ConnectionError:
            self.logger.error("Failed to connect to Redis in ScraperHandler")
            # plugin is essential to functionality
            sys.exit(1) 
Example #19
Source File: diskover_connections.py    From diskover with Apache License 2.0 6 votes vote down vote up
def connect_to_redis():
    from diskover import config
    global redis_conn

    # use connection pools
    # use socket
    if config['redis_socket']:
        pool = ConnectionPool(connection_class=UnixDomainSocketConnection, path=config['redis_socket'],
                        password=config['redis_password'], db=config['redis_db'])
        redis_conn = Redis(connection_pool=pool)

    # use host/port
    else:
        pool = ConnectionPool(host=config['redis_host'], port=config['redis_port'],
                        password=config['redis_password'], db=config['redis_db'])
        redis_conn = Redis(connection_pool=pool) 
Example #20
Source File: Slave.py    From crawler_examples with Apache License 2.0 5 votes vote down vote up
def get_task(self):
        """
        get task URL from Redis queue and translate it into UTF-8 encoding.
        """
        _url = self.conn.blpop("UrlQueue")
        self.url = _url[1].decode("utf-8") 
Example #21
Source File: transaction_service.py    From safe-relay-service with MIT License 5 votes vote down vote up
def __init__(self, gas_station: GasStation, ethereum_client: EthereumClient, redis: Redis,
                 safe_valid_contract_addresses: Set[str], proxy_factory_address: str, tx_sender_private_key: str):
        self.gas_station = gas_station
        self.ethereum_client = ethereum_client
        self.redis = redis
        self.safe_valid_contract_addresses = safe_valid_contract_addresses
        self.proxy_factory = ProxyFactory(proxy_factory_address, self.ethereum_client)
        self.tx_sender_account = Account.from_key(tx_sender_private_key) 
Example #22
Source File: cache.py    From selene-backend with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self):
        # should the variables host and port be in the config class?
        redis_host = os.environ['REDIS_HOST']
        redis_port = int(os.environ['REDIS_PORT'])
        self.redis = Redis(host=redis_host, port=redis_port) 
Example #23
Source File: utils.py    From goodbye-mihome with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_store():
    global store
    if store is None:
        store = Redis(host=config.REDIS_HOST, port=config.REDIS_PORT, db=config.REDIS_DB)
    return store 
Example #24
Source File: Slave.py    From crawler_examples with Apache License 2.0 5 votes vote down vote up
def main():
    global slave_name

    # 记录登录时间
    conn = redis.Redis(host=val['redis']['host'], port=val['redis']['port'])
    conn.rpush("users", slave_name + " " + time.ctime())

    # 启动
    if val['proxy_on']:
        mode_multi(val['thread_num'], val['proxies'])
    else:
        mode_no_proxy(val['thread_num']) 
Example #25
Source File: redis_stats_middleware.py    From scrapy-cluster with MIT License 5 votes vote down vote up
def setup(self, settings):
        '''
        Does the actual setup of the middleware
        '''
        # set up the default sc logger
        my_level = settings.get('SC_LOG_LEVEL', 'INFO')
        my_name = settings.get('SClogger_NAME', 'sc-logger')
        my_output = settings.get('SC_LOG_STDOUT', True)
        my_json = settings.get('SC_LOG_JSON', False)
        my_dir = settings.get('SC_LOG_DIR', 'logs')
        my_bytes = settings.get('SC_LOG_MAX_BYTES', '10MB')
        my_file = settings.get('SC_LOG_FILE', 'main.log')
        my_backups = settings.get('SC_LOG_BACKUPS', 5)

        self.logger = LogFactory.get_instance(json=my_json,
                                         name=my_name,
                                         stdout=my_output,
                                         level=my_level,
                                         dir=my_dir,
                                         file=my_file,
                                         bytes=my_bytes,
                                         backups=my_backups)

        self.settings = settings
        self.stats_dict = {}

        # set up redis
        self.redis_conn = redis.Redis(host=settings.get('REDIS_HOST'),
            port=settings.get('REDIS_PORT'),
            db=settings.get('REDIS_DB'))

        try:
            self.redis_conn.info()
            self.logger.debug("Connected to Redis in ScraperHandler")
        except ConnectionError:
            self.logger.error("Failed to connect to Redis in Stats Middleware")
            # plugin is essential to functionality
            sys.exit(1) 
Example #26
Source File: Slave.py    From crawler_examples with Apache License 2.0 5 votes vote down vote up
def __init__(self, thread_grade, proxy = None):
        """
        connect to Redis.init session and proxy.
        """
        self.thread_grade = thread_grade
        self.conn = redis.Redis(host=val['redis']['host'], port=val['redis']['port'])
        self.session = requests.Session()
        self.proxy = {"http":proxy}
        threading.Thread.__init__(self) 
Example #27
Source File: RunSpider.py    From crawler_examples with Apache License 2.0 5 votes vote down vote up
def crawlIP():
    # 连接至Redis
    conn = redis.Redis()
    while True:
        temp =  conn.blpop("link_queue") 
        linkUrl = temp[1].decode('utf-8')
        linkObj = getLinks(linkUrl)
        GetLocal.storeIPinfo(linkObj) 
Example #28
Source File: online.py    From scrapy-cluster with MIT License 5 votes vote down vote up
def setUp(self):
        self.redis_monitor = RedisMonitor("localsettings.py")
        self.redis_monitor.settings = self.redis_monitor.wrapper.load("localsettings.py")
        self.redis_monitor.logger = MagicMock()
        self.redis_monitor.settings['KAFKA_TOPIC_PREFIX'] = "demo_test"
        self.redis_monitor.settings['STATS_TOTAL'] = False
        self.redis_monitor.settings['STATS_PLUGINS'] = False
        self.redis_monitor.settings['PLUGINS'] = {
            'plugins.info_monitor.InfoMonitor': None,
            'plugins.stop_monitor.StopMonitor': None,
            'plugins.expire_monitor.ExpireMonitor': None,
            'tests.online.CustomMonitor': 100,
        }
        self.redis_monitor.redis_conn = redis.Redis(
            host=self.redis_monitor.settings['REDIS_HOST'],
            port=self.redis_monitor.settings['REDIS_PORT'],
            db=self.redis_monitor.settings['REDIS_DB'])

        self.redis_monitor._load_plugins()
        self.redis_monitor.stats_dict = {}

        self.consumer = KafkaConsumer(
            "demo_test.outbound_firehose",
            bootstrap_servers=self.redis_monitor.settings['KAFKA_HOSTS'],
            group_id="demo-id",
            auto_commit_interval_ms=10,
            consumer_timeout_ms=5000,
            auto_offset_reset='earliest'
        )
        sleep(1) 
Example #29
Source File: online.py    From scrapy-cluster with MIT License 5 votes vote down vote up
def setUp(self):
        self.settings = get_project_settings()
        self.settings.set('KAFKA_TOPIC_PREFIX', "demo_test")
        # set up redis
        self.redis_conn = redis.Redis(host=self.settings['REDIS_HOST'],
                                      port=self.settings['REDIS_PORT'],
                                      db=self.settings['REDIS_DB'])
        try:
            self.redis_conn.info()
        except ConnectionError:
            print("Could not connect to Redis")
            # plugin is essential to functionality
            sys.exit(1)

        # clear out older test keys if any
        keys = self.redis_conn.keys("test-spider:*")
        for key in keys:
            self.redis_conn.delete(key)

        # set up kafka to consumer potential result
        self.consumer = KafkaConsumer(
            "demo_test.crawled_firehose",
            bootstrap_servers=self.settings['KAFKA_HOSTS'],
            group_id="demo-id",
            auto_commit_interval_ms=10,
            consumer_timeout_ms=5000,
            auto_offset_reset='earliest'
        )
        time.sleep(1) 
Example #30
Source File: redislistdriver.py    From sniffer with Apache License 2.0 5 votes vote down vote up
def redis_loop(self):
        r = redis.Redis(host=self.host, port=self.port, password=self.password)
        total_count = 0
        idle_count = 0

        while self.running:
            p = r.pipeline()
            p.lrange("traffic_records", 0, 100)
            p.ltrim("traffic_records", 100, -1)
            result = p.execute()
            if result and result[0]:
                records = result[0]
            else:
                records = []

            if records:
                idle_count = 0
            else:
                idle_count += 1
            if idle_count > 5:
                # no data available
                gevent.sleep(0.5)

            for record in records:
                try:
                    self.process_record(record)
                except:
                    pass

            total_count += len(records)
            if 0 < self.max_count < total_count:
                break