我怎样才不需要每次查询数据库?

时间:2017-10-19 10:39:00

标签: python django django-queryset

我怎样才不需要每次都查询数据库?

从波纹管快照:

enter image description here

我有五个标签,名称为:云主机云硬盘云主机快照云硬盘快照安全组

在列表的底部,有<<<>>>GO按钮可以计算page_num。

然后我可以使用localhost:8000/app_admin/myServers-1-1-1-1-1类似链接来查询数据。

1-1-1-1-1代表云主机云硬盘云主机快照云硬盘快照安全组&page 39um。

在views.py中,有关键代码:

def myServers(request, server_nid,disk_nid,snapshot_server_nid, snapshot_block_nid,security_group_nid, tab_nid):

    data = get_res_myserver(request, server_nid,disk_nid,snapshot_server_nid, snapshot_block_nid,security_group_nid, tab_nid)

    return render(request, 'app_admin/my-server.html',  {"data":data})  

...
def get_res_myserver(request, server_nid,disk_nid,snapshot_server_nid, snapshot_block_nid,security_group_nid, tab_nid):

    # query all the data, and paginator there
    ...
    return data

但是,我的问题是,每次查询localhost:8000/app_admin/myServers-x-x-x-x-x时,都需要很长时间,有时超过8秒(时间不能更短),用户体验需要很长时间。< / p>

那么,是否有一种方法我只查询一次数据,那么paginator可以多次?

编辑

这是get_res_myserver方法的详细信息:

def get_res_myserver(request, server_nid,disk_nid,snapshot_server_nid, snapshot_block_nid,security_group_nid, tab_nid):
    page_size = 5
    # 取出分页
    server_page_num = 1 if server_nid == None else server_nid
    disk_page_num = 1 if disk_nid == None else disk_nid
    ss_server_page_num = 1 if snapshot_server_nid == None else snapshot_server_nid  # server snapshot
    ss_block_page_num = 1 if snapshot_block_nid == None else snapshot_block_nid  # block snapshot
    sg_page_num = 1 if security_group_nid == None else security_group_nid  # security_group
    tab_nid_num = 1 if tab_nid == None else tab_nid

    data = {}

    # 云主机

    # conn找到虚拟机

    op_conn = OpenstackConn.OpenstackConn()
    server_op_list = list(op_conn.conn.compute.servers())

    import json

    server_app_list = app_admin_models.Instance.objects.filter(user=get_user(request))

    server_paginator = Paginator(server_op_list, page_size)

    try:
        server_op_page_list = server_paginator.page(server_page_num)
    except PageNotAnInteger:
        server_op_page_list = server_paginator.page(1)
        server_page_num = 1
    except EmptyPage:
        server_op_page_list = server_paginator.page(server_paginator.num_pages)
        server_page_num = server_paginator.num_pages

    server_app_page_list = []

    server_data_list = []  # data封装op和app的server. 结构 [{"op_server":op_server_instance, "app_server":app_server_instance}]

    for server_op_page in server_op_page_list:
        for server_app in server_app_list:
            if server_app.id == server_op_page.id:
                server_app_page_list.append(server_app)
                server_data_list.append({"op_server": server_op_page, "app_server": server_app})

    # 云硬盘
    # TODO: server_disk (还没有安装)

    server_disk_list = []  # list(op_conn.conn.block_store.volumes())

    disk_paginator = Paginator(server_disk_list, page_size)
    try:
        server_disk_page_list = disk_paginator.page(disk_page_num)
    except PageNotAnInteger:
        server_disk_page_list = disk_paginator.page(1)
        disk_page_num = 1
    except EmptyPage:
        server_disk_page_list = disk_paginator.page(disk_paginator.num_pages)
        disk_page_num = disk_paginator.num_pages

    # 快照

    snapshot_server_generator_ori = op_conn.conn.compute.images()

    snapshot_server_list_ori = list(snapshot_server_generator_ori)
    import copy
    snapshot_server_list_ori_cp = copy.copy(snapshot_server_list_ori)

    for snapshot_server in snapshot_server_list_ori_cp:
        if "snapshot" not in snapshot_server.name:
            snapshot_server_list_ori.remove(snapshot_server)

    snapshot_server_filtered_list = snapshot_server_list_ori

    snapshot_server_paginator = Paginator(snapshot_server_filtered_list, page_size)

    try:
        snapshot_server_page_list = snapshot_server_paginator.page(ss_server_page_num)
    except PageNotAnInteger:
        snapshot_server_page_list = snapshot_server_paginator.page(1)
        ss_server_page_num = 1
    except EmptyPage:
        snapshot_server_page_list = snapshot_server_paginator.page(snapshot_server_paginator.num_pages)
        ss_server_page_num = snapshot_server_paginator.num_pages

    # TODO:  (云主机的块存储快照功能SDK还没有实现)
    snapshot_block_list = []  # list(op_conn.conn.block_store.snapshots())

    block_paginator = Paginator(snapshot_block_list, page_size)
    try:
        snapshot_block_page_list = block_paginator.page(disk_page_num)  # 块存储
    except PageNotAnInteger:
        snapshot_block_page_list = block_paginator.page(1)
        ss_block_page_num = 1
    except EmptyPage:
        snapshot_block_page_list = block_paginator.page(block_paginator.num_pages)
        ss_block_page_num = block_paginator.num_pages

    # 安全组
    security_groups_list = list(op_conn.conn.network.security_groups())

    security_groups_paginator = Paginator(security_groups_list, page_size)
    try:
        security_groups_page_list = security_groups_paginator.page(disk_page_num)
    except PageNotAnInteger:
        security_groups_page_list = security_groups_paginator.page(1)
        sg_page_num = 1
    except EmptyPage:
        security_groups_page_list = security_groups_paginator.page(security_groups_paginator.num_pages)
        sg_page_num = security_groups_paginator.num_pages

    data['server_data_list'] = server_data_list  # VM
    data['server_disk_list'] = server_disk_page_list  # 云硬盘
    data['snapshot_server_list'] = snapshot_server_page_list  # VM的快照 (所有VM的快照)
    data['snapshot_block_list'] = snapshot_block_page_list  # 块存储的快照
    data['security_groups_list'] = security_groups_page_list  # 安全组
    data['settings_data'] = settings.OPENSTACK_USER_NETWORK
    data['tab_nid'] = tab_nid_num  # 这个是选中的哪个tab
    data['server_page_num'] = server_page_num
    data['disk_page_num'] = disk_page_num
    data['ss_server_page_num'] = ss_server_page_num
    data['ss_block_page_num'] = ss_block_page_num
    data['sg_page_num'] = sg_page_num  # 安全组

    print ("myserver_data", data)

    return data

修改-2

这是我的op_conn get,这是一个单身人士:

op_conn = OpenstackConn.OpenstackConn()

0 个答案:

没有答案