python - 在python中缓存
问题描述
最近,我尝试了一些关于缓存的代码。我有 2 个代码,第一个使用字典。其次是使用缓存库。我不明白为什么运行时间如此不同。我认为使用缓存库的第二个与第一个相同。
一:导入时间导入日期时间
class new():
def __init__(self):
self.cache = {}
def get_candy_price(self, candy_id):
# let's use a sleep to simulate the time your function spends trying to connect to
# the web service, 5 seconds will be enough.
time.sleep(5)
if candy_id % 2 not in self.cache:
if candy_id % 2 == 0:
self.cache[candy_id % 2] = 1.5
else:
self.cache[candy_id % 2] = 1
# let's pretend that the price returned by the web service is $1 for candies with a
# odd candy_id and $1,5 for candies with a even candy_id
return (datetime.datetime.now().strftime("%c"), self.cache[candy_id % 2])
# now, let's simulate 20 customers in your show.
# They are asking for candy with id 2 and candy with id 3...
for i in range(0,20):
n = new()
print(n.get_candy_price(2))
print(n.get_candy_price(3))
由于每个人都想要输出:
输出:
('Thu Oct 25 16:14:27 2018', 1.5)
('Thu Oct 25 16:14:32 2018', 1)
('Thu Oct 25 16:14:37 2018', 1.5)
('Thu Oct 25 16:14:42 2018', 1)
('Thu Oct 25 16:14:47 2018', 1.5)
('Thu Oct 25 16:14:52 2018', 1)
('Thu Oct 25 16:14:57 2018', 1.5)
('Thu Oct 25 16:15:02 2018', 1)
('Thu Oct 25 16:15:07 2018', 1.5)
('Thu Oct 25 16:15:12 2018', 1)
('Thu Oct 25 16:15:17 2018', 1.5)
('Thu Oct 25 16:15:22 2018', 1)
('Thu Oct 25 16:15:27 2018', 1.5)
('Thu Oct 25 16:15:32 2018', 1)
('Thu Oct 25 16:15:37 2018', 1.5)
('Thu Oct 25 16:15:42 2018', 1)
('Thu Oct 25 16:15:47 2018', 1.5)
('Thu Oct 25 16:15:52 2018', 1)
('Thu Oct 25 16:15:57 2018', 1.5)
('Thu Oct 25 16:16:02 2018', 1)
('Thu Oct 25 16:16:07 2018', 1.5)
('Thu Oct 25 16:16:12 2018', 1)
('Thu Oct 25 16:16:17 2018', 1.5)
('Thu Oct 25 16:16:22 2018', 1)
('Thu Oct 25 16:16:27 2018', 1.5)
('Thu Oct 25 16:16:32 2018', 1)
('Thu Oct 25 16:16:37 2018', 1.5)
('Thu Oct 25 16:16:42 2018', 1)
('Thu Oct 25 16:16:47 2018', 1.5)
('Thu Oct 25 16:16:52 2018', 1)
('Thu Oct 25 16:16:57 2018', 1.5)
('Thu Oct 25 16:17:02 2018', 1)
('Thu Oct 25 16:17:07 2018', 1.5)
('Thu Oct 25 16:17:12 2018', 1)
('Thu Oct 25 16:17:17 2018', 1.5)
('Thu Oct 25 16:17:22 2018', 1)
('Thu Oct 25 16:17:27 2018', 1.5)
('Thu Oct 25 16:17:32 2018', 1)
('Thu Oct 25 16:17:37 2018', 1.5)
('Thu Oct 25 16:17:42 2018', 1)
real 3m20.145s
user 0m0.031s
sys 0m0.016s
第二:
import time
import datetime
from cachetools import cached, TTLCache # 1 - let's import the "cached" decorator and the "TTLCache" object from cachetools
cache = TTLCache(maxsize=100, ttl=300) # 2 - let's create the cache object.
@cached(cache)
def get_candy_price(candy_id):
# let's use a sleep to simulate the time your function spends trying to connect to
# the web service, 5 seconds will be enough.
time.sleep(5)
# let's pretend that the price returned by the web service is $1 for candies with a
# odd candy_id and $1,5 for candies with a even candy_id
price = 1.5 if candy_id % 2 == 0 else 1
return (datetime.datetime.now().strftime("%c"), price)
# now, let's simulate 20 customers in your show.
# They are asking for candy with id 2 and candy with id 3...
for i in range(0,20):
print(get_candy_price(2))
print(get_candy_price(3))
输出:
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
('Thu Oct 25 16:32:28 2018', 1.5)
('Thu Oct 25 16:32:33 2018', 1)
real 0m10.121s
user 0m0.031s
sys 0m0.016s
谁能以更理解的方式向我展示第二个缓存的代码示例?例如,是否可以将其更改为更容易查看库如何运行以进行缓存的内容,例如首先?相差约3分钟。
我的目标:
是为了理解python中的缓存,所以我尝试实现两种不同的方式;一个有图书馆,另一个没有。那么cache with library其实和自己制作字典一样吗?那么缓存是否意味着我们制作了一个充当查找表的字典?
解决方案
你总是可以和代码老兄一起玩,
您的第一个示例很难理解缓存机制,您正在制作
您的缓存元素将一次又一次地形成,因为n = new()正在重新初始化自己。丢失所有缓存。这就是为什么它需要太长时间。让它像,
n = new()
for i in range(0,20):
print(n.get_candy_price(2))
print(n.get_candy_price(3))
另一件事,
# let's use a sleep to simulate the time your function spends trying to connect to
# the web service, 5 seconds will be enough.
time.sleep(5)
你想缓存,对吧?那么为什么每次都会调用这个网络电话。仅当答案不在缓存中时才应始终调用它,我正在重新定义您的get_candy_price函数,
def get_candy_price(self, candy_id):
# for observing the results
print (self.cache, candy_id)
if candy_id % 2 not in self.cache:
# as this wasn't already in cache
# let's use a sleep to simulate the time your function spends trying to connect to
# the web service, 1 seconds will be enough.
time.sleep(5)
if candy_id % 2 == 0:
self.cache[candy_id % 2] = 1.5
else:
self.cache[candy_id % 2] = 1
else:
# as we already have cached it
return self.cache[candy_id % 2]
return (datetime.datetime.now().strftime("%c"), self.cache[candy_id % 2])
在第二个示例中,使用 ttl 参数
cache = TTLCache(maxsize=100, ttl=5)
请注意,它现在没有那么快是吗?这是因为您的 ttl(Time to live) 参数将结果缓存了一段时间。谷歌了解更多!
推荐阅读
- python - 尝试在 python 中使用 ioctl 读取驱动器属性时出现溢出错误
- javascript - 同时捕获文本和文本区域字段
- sql - SQL query to extract words from a text field
- c# - 引发 System.Runtime.InteropService.COMException 并关闭 Internet Explorer
- c++ - 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- html - 如何使用 CSS 使输入占位符的颜色与 div 颜色相同?
- java - 打印数组错误 java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length
- c++ - Cout 在 for 循环中打印了很多次
- reactjs - React Native TextInput 确实将值传递给状态
- java - 如何将 A 类与 B 类关联,并从 A 类中的方法返回对 B 类的引用?