2011-08-17 59 views
3

我正在寫一個快速的應用程序來查看一個巨大的XML文件,其中一些AJAX樣式調用到viewgroup。我的問題是session['groups']不會持續。我有一些只有4個成員的舊數組(cookie?..)。該值在調用view時出現。然後,我使用最近打開的包含20多個成員的xml文件中的信息覆蓋該會話成員。瓶頸會話成員不會持續請求

但是,當調用viewgroup時,會話變量已恢復爲只有4個成員的舊值!

代碼後面跟着輸出。注3個sessionStatus()電話

def sessionStatus(): 
    print "# of groups in session = " + str(len(session['groups'])) 

@app.route('/') 
def index(): 
    cams = [file for file in os.listdir('xml/') if file.lower().endswith('xml')] 
    return render_template('index.html', cam_files=cams) 

@app.route('/view/<xmlfile>') 
def view(xmlfile): 
    path = 'xml/' + secure_filename(xmlfile) 
    print 'opening ' + path 
    xmlf = open(path, 'r') 
    tree = etree.parse(xmlf) 
    root = tree.getroot() 
    p = re.compile(r'Group') 
    groups = [] 
    for g in root: 
     if (p.search(g.tag) is not None) and (g.attrib['Comment'] != 'Root'): 
      groups.append(Group(g.attrib['Comment'])) 
    sessionStatus() 
    session['groups'] = groups 
    sessionStatus() 
    return render_template('view.html', xml=xmlfile, groups=groups) 

@app.route('/viewgroup/<name>') 
def viewGroup(name): 
    groups = session['groups'] 
    sessionStatus()   
    if groups is None or len(groups) == 0: 
     raise Exception('invalid group name') 
    groups_filtered = [g for g in groups if g.name == name] 
    if len(groups_filtered) != 1: 
     raise Exception('invalid group name', groups_filtered) 
    group = groups_filtered[0] 
    prop_names = [p.name for p in group.properties] 
    return prop_names 

輸出

opening xml/d.xml 
# of groups in session = 5 
# of groups in session = 57 
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /view/d.xml HTTP/1.1" 200 - 
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /static/ivtl.css HTTP/1.1" 304 - 
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /static/jquery.js HTTP/1.1" 304 - 
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /static/raphael-min.js HTTP/1.1" 304 - 
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /static/ivtl.css HTTP/1.1" 304 - 
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /favicon.ico HTTP/1.1" 404 - 
# of groups in session = 5 
127.0.0.1 - - [17/Aug/2011 17:27:31] "GET /viewgroup/DeviceInformation HTTP/1.1" 200 - 

我需要所有57組呆在身邊。任何提示?

回答

8

數據太大而無法序列化到會話中。現在我將密鑰生成爲全局字典並將該密鑰存儲在會話中。

gXmlData[path] = groups  

有一個問題,即全球字典將永遠留在越來越多的鑰匙,但過程並不意味着長壽。

+4

您可以使用redis後端來存儲字典。有一個很好的功能來將數據存儲在有效期限的密鑰(http://redis.io/commands/setex)中。因此,在第一次設置時,您可以定義會話超時類型比每個得到你延長過期或返回會話過期(http://redis.io/commands/expire)。價值數據大小限制是相當大的(http://stackoverflow.com/questions/5606106/what-is-the-maximum-value-size-you-can-store-in-redis)。 – Munhitsu