本文整理汇总了Python中fakeredis.FakeStrictRedis方法的典型用法代码示例。如果您正苦于以下问题:Python fakeredis.FakeStrictRedis方法的具体用法?Python fakeredis.FakeStrictRedis怎么用?Python fakeredis.FakeStrictRedis使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fakeredis
的用法示例。
在下文中一共展示了fakeredis.FakeStrictRedis方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import fakeredis [as 别名]
# 或者: from fakeredis import FakeStrictRedis [as 别名]
def __init__(self, env, host: str, port: int = 6379, db: int = 0):
if env.config.get(ConfigKeys.TESTING, False) or host == 'mock':
from fakeredis import FakeStrictRedis
self.redis_pool = None
self.redis_instance = FakeStrictRedis(host=host, port=port, db=db)
else:
self.redis_pool = redis.ConnectionPool(host=host, port=port, db=db)
self.redis_instance = None
self.cache = MemoryCache()
args = sys.argv
for a in ['--bind', '-b']:
bind_arg_pos = [i for i, x in enumerate(args) if x == a]
if len(bind_arg_pos) > 0:
bind_arg_pos = bind_arg_pos[0]
break
self.listen_port = 'standalone'
if bind_arg_pos is not None and not isinstance(bind_arg_pos, list):
self.listen_port = args[bind_arg_pos + 1].split(':')[1]
self.listen_host = socket.gethostname().split('.')[0]
示例2: test_basic
# 需要导入模块: import fakeredis [as 别名]
# 或者: from fakeredis import FakeStrictRedis [as 别名]
def test_basic(sentry_init, capture_events):
sentry_init(integrations=[RedisIntegration()])
events = capture_events()
connection = FakeStrictRedis()
connection.get("foobar")
capture_message("hi")
(event,) = events
(crumb,) = event["breadcrumbs"]
assert crumb == {
"category": "redis",
"message": "GET 'foobar'",
"data": {"redis.key": "foobar", "redis.command": "GET"},
"timestamp": crumb["timestamp"],
"type": "redis",
}
示例3: test_basic
# 需要导入模块: import fakeredis [as 别名]
# 或者: from fakeredis import FakeStrictRedis [as 别名]
def test_basic(sentry_init, capture_events):
sentry_init(integrations=[RqIntegration()])
events = capture_events()
queue = rq.Queue(connection=FakeStrictRedis())
worker = rq.SimpleWorker([queue], connection=queue.connection)
queue.enqueue(crashing_job, foo=42)
worker.work(burst=True)
(event,) = events
(exception,) = event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"
assert exception["mechanism"]["type"] == "rq"
assert exception["stacktrace"]["frames"][-1]["vars"]["foo"] == "42"
assert event["transaction"] == "tests.integrations.rq.test_rq.crashing_job"
assert event["extra"]["rq-job"] == {
"args": [],
"description": "tests.integrations.rq.test_rq.crashing_job(foo=42)",
"func": "tests.integrations.rq.test_rq.crashing_job",
"job_id": event["extra"]["rq-job"]["job_id"],
"kwargs": {"foo": 42},
}
示例4: test_transport_shutdown
# 需要导入模块: import fakeredis [as 别名]
# 或者: from fakeredis import FakeStrictRedis [as 别名]
def test_transport_shutdown(sentry_init, capture_events_forksafe):
sentry_init(integrations=[RqIntegration()])
events = capture_events_forksafe()
queue = rq.Queue(connection=FakeStrictRedis())
worker = rq.Worker([queue], connection=queue.connection)
queue.enqueue(crashing_job, foo=42)
worker.work(burst=True)
event = events.read_event()
events.read_flush()
(exception,) = event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"
示例5: _GetRedisClient
# 需要导入模块: import fakeredis [as 别名]
# 或者: from fakeredis import FakeStrictRedis [as 别名]
def _GetRedisClient(self):
"""Creates a Redis client for testing.
This method will attempt to use a Redis server listening on localhost and
fallback to a fake Redis client if no server is availble or the connection
timed out.
Returns:
Redis: a Redis client.
"""
try:
redis_client = redis.from_url(self._REDIS_URL, socket_timeout=60)
redis_client.ping()
except redis.exceptions.ConnectionError:
redis_client = fakeredis.FakeStrictRedis()
return redis_client
示例6: test_batch_staticmethod
# 需要导入模块: import fakeredis [as 别名]
# 或者: from fakeredis import FakeStrictRedis [as 别名]
def test_batch_staticmethod():
r = fakeredis.FakeStrictRedis()
r.flushall()
cache = RedisCache(conn=r)
class ABS(object):
count = 0
@cache.batch()
@staticmethod
def list(*ids):
result = []
for i in ids:
result.append(i + random.randint(1, 100))
return result
assert ABS.list(3, 4) == ABS.list(3, 4) == ABS().list(3, 4)
示例7: test_not_cache_None
# 需要导入模块: import fakeredis [as 别名]
# 或者: from fakeredis import FakeStrictRedis [as 别名]
def test_not_cache_None():
r = fakeredis.FakeStrictRedis()
r.flushall()
cache = RedisCache(conn=r)
global i
i = 0
@cache.cached(should_cache_fn=lambda value: value is not None)
def incr(by):
global i
i += 1
return by
incr(None)
incr(None)
assert i == 2
incr(1)
incr(1)
assert i == 3
示例8: test_cache_classmethod
# 需要导入模块: import fakeredis [as 别名]
# 或者: from fakeredis import FakeStrictRedis [as 别名]
def test_cache_classmethod():
r = fakeredis.FakeStrictRedis()
r.flushall()
cache = RedisCache(conn=r)
class AC(object):
count = 0
@cache.cached()
@classmethod
def add(cls, a, b):
return a + b + random.randint(1, 100)
@cache.cached()
@classmethod
def plus(cls, a, b):
cls.__name__ = 'AD'
return a + b + random.randint(1, 100)
assert AC.add(3, 4) == AC.add(3, 4) == AC().add(3, 4)
assert AC.plus(3, 4) != AC.plus(3, 4)
示例9: test_multi_tag
# 需要导入模块: import fakeredis [as 别名]
# 或者: from fakeredis import FakeStrictRedis [as 别名]
def test_multi_tag():
r = fakeredis.FakeStrictRedis()
r.flushall()
cache = RedisCache(conn=r)
class A(object):
@cache.cached(tags=["a:{0}", "b:{1}", "c"])
def add(self, a, b):
return a + b + random.randint(1, 10000)
add_result1 = A().add(5, 6)
add_result2 = A().add(5, 7)
add_result3 = A().add(1, 8)
add_result4 = A().add(1, 8)
add_result5 = A().add(2, 9)
A().add.invalidate_tag("a:5")
A().add.invalidate_tag("b:8")
assert add_result1 != A().add(5, 6)
assert add_result2 != A().add(5, 7)
assert add_result3 != A().add(1, 8)
assert add_result4 != A().add(1, 8)
assert add_result5 == A().add(2, 9)
A().add.invalidate_tag("c")
assert add_result5 != A().add(2, 9)
示例10: test_multi_tag_kwargs
# 需要导入模块: import fakeredis [as 别名]
# 或者: from fakeredis import FakeStrictRedis [as 别名]
def test_multi_tag_kwargs():
r = fakeredis.FakeStrictRedis()
r.flushall()
cache = RedisCache(conn=r)
class A(object):
@cache.cached(key_func=kwargs_key_generator, tags=["a:{a}", "b:{b}", "c"])
def add(self, a, b):
return a + b + random.randint(1, 10000)
add_result1 = A().add(a=5, b=6)
add_result2 = A().add(a=5, b=7)
add_result3 = A().add(a=1, b=8)
add_result4 = A().add(a=1, b=8)
add_result5 = A().add(a=2, b=9)
A().add.invalidate_tag("a:5")
A().add.invalidate_tag("b:8")
assert add_result1 != A().add(a=5, b=6)
assert add_result2 != A().add(a=5, b=7)
assert add_result3 != A().add(a=1, b=8)
assert add_result4 != A().add(a=1, b=8)
assert add_result5 == A().add(a=2, b=9)
A().add.invalidate_tag("c")
assert add_result5 != A().add(a=2, b=9)
示例11: test_function_tag
# 需要导入模块: import fakeredis [as 别名]
# 或者: from fakeredis import FakeStrictRedis [as 别名]
def test_function_tag():
r = fakeredis.FakeStrictRedis()
r.flushall()
cache = RedisCache(conn=r)
@cache.cached(tags=[lambda *args, **kwargs: "add:{0}".format(args[0] + args[1])])
def add(a, b):
return a + b + random.randint(1, 10000)
add_result1 = add(5, 6)
add_result2 = add(4, 7)
add_result3 = add(5, 8)
# cache 生效
assert add(5, 6) == add_result1
assert add(4, 7) == add_result2
assert add(5, 8) == add_result3
add.invalidate_tag("add:11")
assert add(5, 6) != add_result1
assert add(4, 7) != add_result2
assert add(5, 8) == add_result3
示例12: test_function_tag_kwargs
# 需要导入模块: import fakeredis [as 别名]
# 或者: from fakeredis import FakeStrictRedis [as 别名]
def test_function_tag_kwargs():
r = fakeredis.FakeStrictRedis()
r.flushall()
cache = RedisCache(conn=r)
@cache.cached(key_func=kwargs_key_generator, tags=[lambda *args, **kwargs: "add:{0}".format(kwargs['a'] + kwargs['b'])])
def add(a, b):
return a + b + random.randint(1, 10000)
add_result1 = add(a=5, b=6)
add_result2 = add(a=4, b=7)
add_result3 = add(a=5, b=8)
# cache 生效
assert add(a=5, b=6) == add_result1
assert add(a=4, b=7) == add_result2
assert add(a=5, b=8) == add_result3
add.invalidate_tag("add:11")
assert add(a=5, b=6) != add_result1
assert add(a=4, b=7) != add_result2
assert add(a=5, b=8) == add_result3
示例13: _patch_redis
# 需要导入模块: import fakeredis [as 别名]
# 或者: from fakeredis import FakeStrictRedis [as 别名]
def _patch_redis(self):
redis.StrictRedis = FakeStrictRedis
if not self.cache_dir:
return
if self.inputs:
self.cache_dir = os.path.join(os.path.dirname(self.inputs[0]), self.cache_dir)
try:
os.makedirs(self.cache_dir)
except OSError:
pass
name = os.path.basename(self.inputs[0]) +'-cache.json.gz'
cache_db = os.path.join(self.cache_dir, name)
self.serializer = FakeRedisSerializer(cache_db, self.inputs)
示例14: test_redis_cache_tags_push_forever_keys_correctly
# 需要导入模块: import fakeredis [as 别名]
# 或者: from fakeredis import FakeStrictRedis [as 别名]
def test_redis_cache_tags_push_forever_keys_correctly(self):
store = flexmock(RedisStore(redis_class=FakeStrictRedis))
tag_set = flexmock(TagSet(store, ['foo', 'bar']))
tag_set.should_receive('get_namespace').and_return('foo|bar')
redis = RedisTaggedCache(store, tag_set)
store.should_receive('get_prefix').and_return('prefix:')
conn = flexmock()
store.should_receive('connection').and_return(conn)
conn.should_receive('lpush').once()\
.with_args('prefix:foo:forever', 'prefix:%s:key1' % hashlib.sha1(b'foo|bar').hexdigest())
conn.should_receive('lpush').once()\
.with_args('prefix:bar:forever', 'prefix:%s:key1' % hashlib.sha1(b'foo|bar').hexdigest())
store.should_receive('forever').with_args(hashlib.sha1(b'foo|bar').hexdigest() + ':key1', 'key1:value')
redis.forever('key1', 'key1:value')
示例15: __enter__
# 需要导入模块: import fakeredis [as 别名]
# 或者: from fakeredis import FakeStrictRedis [as 别名]
def __enter__(self):
# type: () -> optuna.storages.BaseStorage
if self.storage_specifier == "inmemory":
return optuna.storages.InMemoryStorage()
elif self.storage_specifier == "sqlite":
self.tempfile = tempfile.NamedTemporaryFile()
url = "sqlite:///{}".format(self.tempfile.name)
return optuna.storages.RDBStorage(
url, engine_kwargs={"connect_args": {"timeout": SQLITE3_TIMEOUT}},
)
elif self.storage_specifier == "cache":
self.tempfile = tempfile.NamedTemporaryFile()
url = "sqlite:///{}".format(self.tempfile.name)
return optuna.storages._CachedStorage(
optuna.storages.RDBStorage(
url, engine_kwargs={"connect_args": {"timeout": SQLITE3_TIMEOUT}},
)
)
elif self.storage_specifier == "redis":
storage = optuna.storages.RedisStorage("redis://localhost")
storage._redis = fakeredis.FakeStrictRedis()
return storage
else:
assert False