yukaizhao
yukaizhao

Reputation: 690

use Flask-Cache in webpy. i got error <type 'exceptions.RuntimeError'> at xx working outside of request context

initial cache object code as follow:

pageCache = Cache()
cacheDir = os.path.join(path.dirname(path.dirname(__file__)),'pageCache')
pageCache.init_app(flaskApp,config={'CACHE_TYPE': 'filesystem','CACHE_THRESHOLD':1>>10>>10,'CACHE_DIR': cacheDir })

I use pageCache as follow:

class CodeList:
    """
    show code list
    """
    @pageCache.cached(timeout=60)
    def GET(self):
        i = web.input()
        sort = i.get('sort','newest')
        pageNo = int(i.get('page','1'))
        if i.get('pageSize'):
            pageSize = int(i.get('pageSize'))
        else:
            pageSize = DEFAULT_LIST_PAGE_SIZE
        if pageSize > 50:
            pageSize = 50
        items = csModel.getCodeList(sort=sort,pageNo=pageNo,pageSize=pageSize)
        totalCount = csModel.getCodeCount()
        pageInfo = (pageNo,pageSize,totalCount)
        return render.code.list(items,pageInfo)

when I request this page, I got an exception:

type 'exceptions.RuntimeError' at /code-snippet/ working outside of request context

Python C:\Python27\lib\site-packages\flask-0.9-py2.7.egg\flask\globals.py in >_lookup_object, line 18

Upvotes: 0

Views: 519

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318508

Flask-Cache is - as the name suggests - a Flask extension. So you cannot properly use it if you do not use Flask.

You can use werkzeug.cache instead - Flask-Cache is using it, too. However, depending on your needs it might be a better idea to use e.g. memcached directly - when using a wrapper such as werkzeug.cache you lose all advanced features of your caching engine because it's wrapped with a rather simple/minimalistic API.

Upvotes: 1

Related Questions