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'])
python类Redis()的实例源码
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 __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 __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 __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 __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 __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 __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 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 __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 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 __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 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 __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 __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 __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 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, 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 __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 __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 __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 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 __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 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)