有趣的是,雷迪斯的表现并不是那么好。要么是Python做了一些神奇的事情(存储文件),要么是我的redis版本慢得惊人。
我不知道这是不是因为我的代码的结构方式,或者什么,但我希望redis做得比它做得更好。
为了制作一个redis缓存,我将我的二进制数据(在本例中是一个HTML页面)设置为一个从文件名派生的密钥,过期时间为5分钟。
在所有情况下,文件处理都是用f.read()完成的(这比f.readlines()快3x,而且我需要二进制blob)。
是我在比较中遗漏了什么,还是Redis真的不是磁盘的对手?Python是否将文件缓存在某个地方,并每次重新访问它?为什么这比访问Redis要快得多?
我使用的是Redis2.8、Python2.7和redis-py,它们都是在64位Ubuntu系统上使用的。
一个函数,用于查看redis对象是否仍在内存中,加载它,或缓存新文件(单个或多个redis实例)。
创建生成器的函数,生成器从redis数据库(具有redis的单个和多个实例)产生结果。
最后,将文件存储在内存中并永远生成它。
import redis
import time
def load_file(fp, fpKey, r, expiry):
with open(fp, "rb") as f:
data = f.read()
p = r.pipeline()
p.set(fpKey, data)
p.expire(fpKey, expiry)
p.execute()
return data
def cache_or_get_gen(fp, expiry=300, r=redis.Redis(db=5)):
fpKey = "cached:"+fp
while True:
yield load_file(fp, fpKey, r, expiry)
t = time.time()
while time.time() - t - expiry < 0:
yield r.get(fpKey)
def cache_or_get(fp, expiry=300, r=redis.Redis(db=5)):
fpKey = "cached:"+fp
if r.exists(fpKey):
return r.get(fpKey)
else:
with open(fp, "rb") as f:
data = f.read()
p = r.pipeline()
p.set(fpKey, data)
p.expire(fpKey, expiry)
p.execute()
return data
def mem_cache(fp):
with open(fp, "rb") as f:
data = f.readlines()
while True:
yield data
def stressTest(fp, trials = 10000):
# Read the file x number of times
a = time.time()
for x in range(trials):
with open(fp, "rb") as f:
data = f.read()
b = time.time()
readAvg = trials/(b-a)
# Generator version
# Read the file, cache it, read it with a new instance each time
a = time.time()
gen = cache_or_get_gen(fp)
for x in range(trials):
data = next(gen)
b = time.time()
cachedAvgGen = trials/(b-a)
# Read file, cache it, pass in redis instance each time
a = time.time()
r = redis.Redis(db=6)
gen = cache_or_get_gen(fp, r=r)
for x in range(trials):
data = next(gen)
b = time.time()
inCachedAvgGen = trials/(b-a)
# Non generator version
# Read the file, cache it, read it with a new instance each time
a = time.time()
for x in range(trials):
data = cache_or_get(fp)
b = time.time()
cachedAvg = trials/(b-a)
# Read file, cache it, pass in redis instance each time
a = time.time()
r = redis.Redis(db=6)
for x in range(trials):
data = cache_or_get(fp, r=r)
b = time.time()
inCachedAvg = trials/(b-a)
# Read file, cache it in python object
a = time.time()
for x in range(trials):
data = mem_cache(fp)
b = time.time()
memCachedAvg = trials/(b-a)
print "\n%s file reads: %.2f reads/second\n" %(trials, readAvg)
print "Yielding from generators for data:"
print "multi redis instance: %.2f reads/second (%.2f percent)" %(cachedAvgGen, (100*(cachedAvgGen-readAvg)/(readAvg)))
print "single redis instance: %.2f reads/second (%.2f percent)" %(inCachedAvgGen, (100*(inCachedAvgGen-readAvg)/(readAvg)))
print "Function calls to get data:"
print "multi redis instance: %.2f reads/second (%.2f percent)" %(cachedAvg, (100*(cachedAvg-readAvg)/(readAvg)))
print "single redis instance: %.2f reads/second (%.2f percent)" %(inCachedAvg, (100*(inCachedAvg-readAvg)/(readAvg)))
print "python cached object: %.2f reads/second (%.2f percent)" %(memCachedAvg, (100*(memCachedAvg-readAvg)/(readAvg)))
if __name__ == "__main__":
fileToRead = "templates/index.html"
stressTest(fileToRead)
10000 file reads: 30971.94 reads/second
Yielding from generators for data:
multi redis instance: 8489.28 reads/second (-72.59 percent)
single redis instance: 8801.73 reads/second (-71.58 percent)
Function calls to get data:
multi redis instance: 5396.81 reads/second (-82.58 percent)
single redis instance: 5419.19 reads/second (-82.50 percent)
python cached object: 1522765.03 reads/second (4816.60 percent)
编辑:一些更多的信息和测试。
我将函数替换为
data = r.get(fpKey)
if data:
return r.get(fpKey)
结果与
if r.exists(fpKey):
data = r.get(fpKey)
Function calls to get data using r.exists as test
multi redis instance: 5320.51 reads/second (-82.34 percent)
single redis instance: 5308.33 reads/second (-82.38 percent)
python cached object: 1494123.68 reads/second (5348.17 percent)
Function calls to get data using if data as test
multi redis instance: 8540.91 reads/second (-71.25 percent)
single redis instance: 7888.24 reads/second (-73.45 percent)
python cached object: 1520226.17 reads/second (5132.01 percent)
Total number of files: 700
10000 file reads: 274.28 reads/second
Yielding from generators for data:
multi redis instance: 15393.30 reads/second (5512.32 percent)
single redis instance: 13228.62 reads/second (4723.09 percent)
Function calls to get data:
multi redis instance: 11213.54 reads/second (3988.40 percent)
single redis instance: 14420.15 reads/second (5157.52 percent)
python cached object: 607649.98 reads/second (221446.26 percent)
Total number of files: 700
40000 file reads: 1168.23 reads/second
Yielding from generators for data:
multi redis instance: 14900.80 reads/second (1175.50 percent)
single redis instance: 14318.28 reads/second (1125.64 percent)
Function calls to get data:
multi redis instance: 13563.36 reads/second (1061.02 percent)
single redis instance: 13486.05 reads/second (1054.40 percent)
python cached object: 587785.35 reads/second (50214.25 percent)
Total number of files: 700
10000 file reads: 284.48 reads/second
Yielding from generators for data:
single redis instance: 11627.56 reads/second (3987.36 percent)
Function calls to get data:
single redis instance: 14615.83 reads/second (5037.81 percent)
python cached object: 580285.56 reads/second (203884.21 percent)
这是苹果和橘子的比较。参见http://redis.io/topics/benchmarks
Redis是一个高效的远程数据存储。每次在Redis上执行命令时,都会向Redis服务器发送一条消息,如果客户端是同步的,则阻塞等待回复。因此,除了命令本身的成本外,您还需要支付网络往返或IPC的费用。
在现代硬件上,与其他操作相比,网络往返或IPC的成本高得惊人。这是由于几个因素:
比较使用生成器的实现和使用函数调用的实现,它们生成到Redis的往返次数并不相同。对于生成器,您只需:
while time.time() - t - expiry < 0:
yield r.get(fpKey)
所以每次迭代要往返一次。使用该函数,您可以:
if r.exists(fpKey):
return r.get(fpKey)
所以每次迭代2次往返。怪不得发电机更快。
问题内容: 我想在python中创建一个redis缓存,作为任何自尊的科学家,我都做了一个基准测试性能。 有趣的是,redis的表现并不那么好。Python做一些不可思议的事情(存储文件),或者我的redis版本太慢了。 我不知道这是否是因为我的代码的结构方式或原因,但是我希望redis比它做得更好。 为了进行Redis缓存,我将二进制数据(在本例中为HTML页面)设置为从文件名派生的密钥,有效期
我必须使用StackExhange.redis C#在redis缓存中频繁添加N个(独立的)项,每个项都有不同的过期时间,以便在客户端有最小的时间,在服务器端有最小的阻塞和成本。Redis服务器每秒将收到数百个get请求,所以我不想打乱get时间。 我已经阅读了这里的文档并在这里回答。我找不到一个执行此操作的方法。考虑到不同的选择: null
更新:为了更明显地说明我正在努力做的事情:我将拥有5000万以上的设备流媒体音频。流平均为100KB,峰值流量时为200K流/分钟。我正在寻找一种存储解决方案来满足这种需求。我一直在研究Bookkeeper、Kafka、Ignite、Cassandra和Redis。到目前为止,我只对redis和ignite进行了基准测试,但我很惊讶ignite这么慢。
问题内容: 我还没有使用过Redis,但我听说过它,并打算尝试将其作为缓存存储。 我听说Redis使用内存作为缓存存储数据库,那么如果我使用变量作为对象或字典数据类型来存储数据有什么区别?喜欢: Redis有什么优势? 问题答案: Redis是一个 远程 数据结构服务器。这肯定比仅将数据存储在本地内存中要慢(因为它涉及套接字往返来获取/存储数据)。但是,它也带来了一些有趣的属性: 应用程序的所有进
问题内容: 包括官方Redis文档在内的许多资料都指出,由于可能会阻塞,在生产环境中使用该命令是一个坏主意。如果已知数据集的大致大小,相对于而言,是否有任何优势? 例如,考虑一个数据库,该数据库最多具有100个以下形式的键:其中,是整数。如果要检索所有这些,可以使用命令。这会比使用慢得多吗?还是在这种情况下这两个命令基本相同?可以说这是更好的选择,因为它可以防止出现意外的大集合返回的情况,这是否更
我的spring boot项目有以下配置。 此外,我还关注maven对POM的依赖。 我有一个单独的redis服务器运行在我的本地机器上的定义端口。另外,在我的服务类中,我有像@cacheable、@cacheput这样的注释来支持缓存。