Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:Flask缓存无法在特定时间范围内工作_Python_Caching_Flask - Fatal编程技术网

Python:Flask缓存无法在特定时间范围内工作

Python:Flask缓存无法在特定时间范围内工作,python,caching,flask,Python,Caching,Flask,我创建的Flask应用程序只有在超出时间范围时才能工作,但在时间范围内时返回错误(if路径) 我在控制台中运行时在两个不同模块中观察到的差异,从def thtop(),app.cache.get('last_response')开始,不返回任何内容。但是,cache.get('last_response')returnabcc 问题是,当在web应用程序中运行时,它会导致如上所示的错误。这句话似乎是正确的:如果now\u time>=time(3,30)和now\u time每当now\u ti

我创建的Flask应用程序只有在超出时间范围时才能工作,但在时间范围内时返回错误(if路径)

我在控制台中运行时在两个不同模块中观察到的差异,从
def thtop()
app.cache.get('last_response')
开始,不返回任何内容。但是,
cache.get('last_response')
return
abcc


问题是,当在web应用程序中运行时,它会导致如上所示的错误。

这句话似乎是
正确的
如果now\u time>=time(3,30)和now\u time每当
now\u time>=time(3,30)和now\u time与您的答案出现相同错误时,您就会收到错误。您可以将测试时间更改为任意,但如果语句if为True,它仍然返回我显示的错误。否则,它将打印字符串。因此,唯一的问题是在
缓存期间。get
。我不确定它是否有效,因为我没有找到任何好的例子。我正在使用flask.ext.cache import cache
@lotteryman的
,也许这会让你得到一个答案:
from flask\u cache import cache
没有效果。还是同样的失败请详细解释你的目标是什么。不知道为什么要使用flask.set和get。你想做什么?可能您没有正确使用缓存。您不需要设置缓存。Flask cache将在您设置为60秒的给定时间段内记住您的最后一个响应(缓存它)。如果它每分钟都被清除,可能不是一个非常有用的缓存,但是depends@Shamik,如果链路超出时间范围,则功能应从链路获取数据。如果在时间范围内,它将使用缓存。另外,
@app.cache.cached(timeout=60)
在60秒内只允许一个查询,这样我就不会被linkhm阻塞了,我得到了你正在尝试的东西。只是猜测而已。方法中的超时是否会清除其上下文中设置的所有内容?你能删除它并检查错误是否仍然存在吗?我已经尝试了很多方法,删除了每一行。我发现我唯一发现的是在if路径中,它将失败。暂停?您指的是哪一个超时?
@app.cache.cached(timeout=60)
肯定不会影响结果,因为如果没有它,它仍然会失败。hi Matt,
rv=None是否重要?我跳过它,它似乎在这种情况下工作。你应该在这里声明rv,因为现在的条件为false,那么rv将永远不会被声明,“如果不是rv”条件将导致UnboundLocalError
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.cache import Cache
from datetime import datetime, time
app.config['CACHE_TYPE'] = 'simple'
app.cache = Cache(app)

    @app.route('/thtop', methods=['GET'])
    @app.cache.cached(timeout=60)
    def thtop():
        now = datetime.now()
        now_time = now.time()
        if now_time >= time(3,30) and now_time <= time(16,30):
            rv = app.cache.get('last_response')
        else:
           rv = 'abcc'
            app.cache.set('last_response', rv, timeout=3600)
        return rv
from flask.ext.sqlalchemy import SQLAlchemy
from flask_caching import Cache
from datetime import datetime, time

cache = Cache(app, config={'CACHE_TYPE': 'simple'})

@app.route('/thtop', methods=['GET'])
@cache.cached(timeout=60)
def thtop():
    now = datetime.now()
    now_time = now.time()
    if now_time >= time(3,30) and now_time <= time(14,30):
        rv = cache.get('last_response')
    else:
        rv = 'abcc'
        cache.set('last_response', rv, timeout=3600)

    return rv
from flask import Flask                                                         
from flask_caching import Cache                                                 
from datetime import datetime, time                                             

app = Flask(__name__)                                                           

app.config['SECRET_KEY'] = 'secret!'                                            
app.config['DEBUG'] = True                                                      

cache = Cache(app, config={'CACHE_TYPE': 'simple'})                             


@app.route('/thtop', methods=['GET'])                                           
@cache.cached(timeout=60)                                                       
def thtop():                                                                    

    now = datetime.now()                                                        
    now_time = now.time()                                                       

    rv = None                                                                   

    if now_time >= time(3, 30) and now_time <= time(14, 30):                    
        rv = cache.get('last_response')                                         

    if not rv:                                                                  
        rv = 'abcc'                                                             
        cache.set('last_response', rv, timeout=3600)                            

    return rv                                                                   


if __name__ == '__main__':                                                      
    app.run()