# django数据库提高效率(queryset何时会缓存)
1 min read
queryset = Entry.objects.all()print([p.headline for p in queryset]) # 当遍历一次时,会进行缓存print([p.pub_date for p in queryset]) # 这次调用就使用了上面的缓存,较少数据库IOqueryset = Entry.objects.all()print(queryset[5]) # 查询数据库,此时是切片,切片不会被缓存print(queryset[5]) # 因为上面没有被缓存,所以这次是再次查询数据库queryset = Entry.objects.all()[entry for entry in queryset] # 查询数据库,因为遍历了,所以进行缓存print(queryset[5]) # 使用缓存print(queryset[5]) # 使用缓存