我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用redis.Redis()。
def test_size_allocation(self): """Make sure we can allocate a bloom filter that would take more than 512MB (the string size limit in Redis)""" included = sample_strings(20, 5000) excluded = sample_strings(20, 5000) # Add only the included strings self.bloom.update(included) self.assertEqual(len(included), len(self.bloom.intersection(included))) false_positives = self.bloom.intersection(excluded) false_rate = float(len(false_positives)) / len(excluded) self.assertTrue(false_rate <= 0.00001, 'False positive error rate exceeded!') # We also need to know that we can access all the keys we need self.assertEqual(self.bloom.keys(), [b'pyreBloomTesting.0', b'pyreBloomTesting.1'])
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 ''
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
def exists(self, key): ############################################################################################ # START common safety net for high availability connectivity handling # This class will automatically retry connections for Redis Instances that are not available success = False while not success: try: # END common safety net for high availability connectivity handling ############################################################################################ return self.m_redis.exists(key) ############################################################################################ # START common safety net for high availability connectivity handling success = True except Exception,R: # try to reconnect with a throttle self.retry_throttled_connection(R) # end try/ex # end of while not successful # END common safety net for high availability connectivity handling ############################################################################################ # end of exists
def get_queues(*queue_names, **kwargs): """ Return queue instances from specified queue names. All instances must use the same Redis connection. """ from .settings import QUEUES autocommit = kwargs.get('autocommit', None) queue_class = kwargs.get('queue_class', DjangoRQ) if len(queue_names) == 0: # Return "default" queue if no queue name is specified return [get_queue(autocommit=autocommit)] if len(queue_names) > 1: queue_params = QUEUES[queue_names[0]] connection_params = filter_connection_params(queue_params) for name in queue_names: if connection_params != filter_connection_params(QUEUES[name]): raise ValueError( 'Queues must have the same redis connection.' '"{0}" and "{1}" have ' 'different connections'.format(name, queue_names[0])) return [get_queue(name, autocommit=autocommit, queue_class=queue_class) for name in queue_names]
def test_auth(self): redis_server = redislite.Redis( serverconfig={ 'requirepass': 'test', 'port': self.redis_test_port+1 }, password='test' ) # This shouldn't generate an exception try: redis_client = redis.Redis(host='127.0.0.1', port=self.redis_test_port+1, password='test') uredis_client = uredis.Redis(host='127.0.0.1', port=self.redis_test_port+1, password='test') finally: redis_server.shutdown()
def authorize_POST(): client_id = request.form.get("client_id") if not client_id: return render_template("oauth-authorize.html", errors="Missing client_id") client = OAuthClient.query.filter(OAuthClient.client_id == client_id).first() if not client: abort(404) salt = os.urandom(40) code = hashlib.sha256(salt).hexdigest()[:10] r = redis.Redis() r.setex("oauth.exchange.client." + code, client_id, 600) # expires in 10 minutes r.setex("oauth.exchange.user." + code, current_user.id, 600) params = { "code": code } parts = list(urllib.parse.urlparse(client.redirect_uri)) parsed = urllib.parse.parse_qs(parts[4]) parsed.update(params) parts[4] = urllib.parse.urlencode(parsed) return redirect(urllib.parse.urlunparse(parts))
def set(self, key, value): """Set the value at key ``key`` to ``value`` >>> dc = Dictator() >>> dc['s0'] = 'string value' >>> dc['s0'] 'string value' >>> dc.set('l0', ['abc', 123]) >>> dc['l0'] ['abc', '123'] >>> dc.set([1, 2, 3], ['a', 'b']) >>> dc['[1, 2, 3]'] ['a', 'b'] >>> dc.clear() :param key: any value (will be converted to string in Redis) :type key: Any :param value: Any :return: None :rtype None """ self.__setitem__(key, value)
def base_usage(): print r.dbsize() #r.flushdb() print r.dbsize() print r.exists('house') #r = redis.Redis(host='183.232.57.39', port=7001, db=0,password='jiguang') for i in range(10): r.set(i,i) #???????? for i in range(10): x=r.get(i) print x print type(x) #print r.get('test') #?????
def run_watcher(): site = pywikibot.Site(user="Embedded Data Bot") redis = Redis(host="tools-redis") signal.signal(signal.SIGALRM, on_timeout) signal.alarm(TIMEOUT) rc = site_rc_listener(site) for change in rc: signal.alarm(TIMEOUT) if ( change['type'] == 'log' and change['namespace'] == 6 and change['log_type'] == 'upload' ): redis.rpush(REDIS_KEY, json.dumps(change)) pywikibot.output("Exit - THIS SHOULD NOT HAPPEN")
def zabbix_api_lvs(): try: t = time.strftime('%H',time.localtime()) tm = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) Key = 'lvs_internet' Key1 = 'lvs_intranet' if t == '00': Redis.delete(Key) Redis.delete(Key1) key='lvs[80]' history = 3 method = 'history.get' host = '172.16.16.8' v8 = zabbix_api.GET_value(host,key,method,history) host = '172.16.16.4' v4 = zabbix_api.GET_value(host,key,method,history) host = '172.16.16.5' v5 = zabbix_api.GET_value(host,key,method,history) if v4 and v8: lvs_conn = int(v4)+int(v8) Redis.lpush(Key,[tm,lvs_conn]) if v5: Redis.lpush(Key1,[tm,int(v5)]) except Exception as e: logging.error(e)
def __init__(self, server='localhost:6379', db=None, debug=False, session_expiry=False, with_lock=False, password=None): """session_expiry can be an integer, in seconds, to set the default expiration of sessions. The corresponding record will be deleted from the redis instance, and there's virtually no need to run sessions2trash.py """ self.server = server self.password = password self.db = db or 0 host, port = (self.server.split(':') + ['6379'])[:2] port = int(port) self.debug = debug if current and current.request: self.app = current.request.application else: self.app = '' self.r_server = redis.Redis(host=host, port=port, db=self.db, password=self.password) if with_lock: RedisClient._release_script = \ self.r_server.register_script(_LUA_RELEASE_LOCK) self.tablename = None self.session_expiry = session_expiry self.with_lock = with_lock
def get_stats(url, spids): stats = {} conf = parse_redis_url(url) r = redis.Redis(host=conf.host, port=conf.port, db=conf.database) for spid in spids: n = None try: n = r.get(spid) r.delete(spid) except redis.exceptions.ConnectionError: logger.error('Error in get_stats failed to connect redis server') n = 0 if n is None else int(n) stats[spid] = n return stats
def worker(): with Connection(Redis("jobqueue.local")): qs = sys.argv[1:] or ['default'] print("foo") w = Worker(qs) w.work()
def _redis(): redis_config = config['redis'] return redis.Redis(**redis_config)
def setup(self, bottom, top): assert len(bottom) == 0 assert len(self.__class__.spider.attr) == len(top) self.process_num = self.__class__.process_num self.max_cache_item_num = self.__class__.max_cache_item_num self.wait_time = self.__class__.wait_time self.processes = [] self.redis = redis.Redis() self.redis.delete(config.solver_prototxt) for _ in range(self.process_num): self.processes.append(Process(target=self.fetch, args=(self.spider(),))) self.processes[_].start()
def __init__(self, prefix, url, clear_age): self.logger = logging.getLogger(self.__class__.__name__) self.prefix = prefix self.clear_age = clear_age or 24 * 60 * 60 if url: self.client = redis.Redis.from_url(url) else: self.client = redis.Redis() self.key_ids = self.prefix + ':ids' self.key_settings = self.prefix + ':settings' # ids
def __init__(self): self._conn = redis.Redis( host='localhost', port=6379, db=0)
def __init__(self): self._conn = redis.Redis( host='localhost', port=6379, db=1)
def setUp(self): self.bloom = PyreBloom(self.KEY, self.CAPACITY, self.ERROR_RATE) self.redis = Redis()
def from_settings(settings): url = settings.get('REDIS_URL', REDIS_URL) host = settings.get('REDIS_HOST', REDIS_HOST) port = settings.get('REDIS_PORT', REDIS_PORT) # REDIS_URL takes precedence over host/port specification. if url: return redis.from_url(url) else: return redis.Redis(host=host, port=port)
def from_settings_filter(settings): url = settings.get('FILTER_URL', FILTER_URL) host = settings.get('FILTER_HOST', FILTER_HOST) port = settings.get('FILTER_PORT', FILTER_PORT) db = settings.get('FILTER_DB', FILTER_DB) if url: return redis.from_url(url) else: return redis.Redis(host=host, port=port, db=db)
def server(self): if not hasattr(self, '_redis'): self._redis = redis.Redis(REDIS_HOST, REDIS_PORT) return self._redis
def setUp(self): ''' Setup mocked data ''' self.dbc = redis.Redis( host="redis", port=6379, password=None, db=0)
def get_pool(cls, redis_host, redis_port, redis_db): """build a redis connection :returns: a valid connection """ try: pool = redis.ConnectionPool(host=redis_host, port=redis_port, db=redis_db) return redis.Redis(connection_pool=pool) except Exception as e: logging.error('connection redis error[%s]' % (e)) raise
def __init__(self, name): """""" self.poped_name = self.POPED_FMT % (name) self.pushed_name = self.PUSHED_FMT % (name) pool = redis.ConnectionPool(host=self.HOST, port=self.PORT, db=self.DB) self.conn = redis.Redis(connection_=pool)
def run(): define('port', default=8090, type=int, help='') define('debug', default=False, type=bool, help='') parse_command_line() settings['debug'] = options.debug if settings['debug']: print 'debug mode' ''' connect mongodb ''' try: client = MotorClient(settings['database']['address']) settings['connection'] = client[settings['database']['db']] except: print 'can not connect MongoDB' sys.exit(0) ''' connect redis ''' try: client = redis.Redis(host=settings['redis']['host'], port=settings['redis']['port'], db=settings['redis']['db']) settings['redis_conn'] = client except: print 'can not connect redis' sys.exit(0) application = Application( handlers=urlpattern, **settings ) http_server = HTTPServer(application, xheaders=True) http_server.listen(options.port) IOLoop.instance().start()
def from_settings(settings): """ :param: settings object :return: Channel object """ connection_type = settings.get('RABBITMQ_CONNECTION_TYPE', RABBITMQ_CONNECTION_TYPE) connection_parameters = settings.get('RABBITMQ_CONNECTION_PARAMETERS', RABBITMQ_CONNECTION_PARAMETERS) connection = { 'blocking': pika.BlockingConnection, 'libev': pika.LibevConnection, 'select': pika.SelectConnection, 'tornado': pika.TornadoConnection, 'twisted': pika.TwistedConnection }[connection_type](pika.ConnectionParameters(**connection_parameters)) channel = connection.channel() channel.basic_qos(prefetch_count=1) url = settings.get('REDIS_URL', REDIS_URL) host = settings.get('REDIS_HOST', REDIS_HOST) port = settings.get('REDIS_PORT', REDIS_PORT) # REDIS_URL takes precedence over host/port specification. if url: redis_server = redis.from_url(url) else: redis_server = redis.Redis(host=host, port=port) return channel, redis_server
def getRdb(): if hss.REDIS_HOST is not None: try: rdb = redis.Redis( host=hss.REDIS_HOST, port=hss.REDIS_PORT) # Test server connection rdb.ping() return rdb except ConnectionError: return EmptyRDB() else: return EmptyRDB()
def getRedisConnection(db): '''?????????Redis???''' if db==APBase.REDSI_POOL: args = settings.REDSI_KWARGS_LPUSH if settings.REDSI_LPUSH_POOL == None: settings.REDSI_LPUSH_POOL = redis.ConnectionPool(host=args.get('host'), port=args.get('port'), db=args.get('db')) pools = settings.REDSI_LPUSH_POOL connection = redis.Redis(connection_pool=pools) return connection
def redis_instance(): """ This fixture ensures that a Redis instance is launched and yield the DSN to connecto to it """ host = "127.0.0.1" port = "6379" # Is an Redis server launched? sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if sock.connect_ex((host, int(port))) == 0: yield "redis://{}:{}".format(host, port) # NO Redis server found. Launch and instance else: redis_bin = os.path.join(os.path.dirname(inspect.getfile(redislite)), "bin", "redis-server") # Launch Redis server p = subprocess.Popen([redis_bin, "--port", port, "--bind", host]) r = redis.Redis(host=host, port=int(port)) # Wait until redis is available while True: if r.ping(): break time.sleep(0.5) del r yield "redis://{}:{}".format(host, port) p.kill()
def test_file_drop_GZHandler(): """ Tests the GZZHandler for file drop """ a = file_drop.GZHandler() class Event: """ Creates a mock event object for tests """ event_type = None src_path = None is_directory = None q = None r = None def __init__(self, event_type, src_path, is_directory): """ initializes necessary variables for the object """ self.event_type = event_type self.src_path = src_path self.is_directory = is_directory self.q = Queue(connection=Redis(host='localhost'), default_timeout=86400) self.r = StrictRedis(host='localhsot', port=6379, db=0) b = Event("created", "/dev/null", False) c = Event("modified", "/etc/hosts", False) a.process(b) a.process(b) a.process(b) a.on_created(b) a.on_modified(c)
def redis(exec_states, host, database): conn = Redis(host=host) # TODO: obj = RedisConnectionObject(conn) obj.set_database(database) return obj
def get_redis(self, host='127.0.0.1', port=6379): try: # Seems no need to try Redis self.client = redis.Redis(host = host, port = port, db = 0) print("dbsize=", self.client.dbsize()) return self.client except Exception as e: print("-" * 40) print(e) print("-" * 40) exit()
def setKey(self,key): r = redis.Redis(connection_pool=self.redispool) r.set(key,'1')