bbelyeu/Flask-SQLAlchemy-Caching

I am trying to read a value from a newly created table entry, but this entry cannot be found when caching is enabled.

yoshapihoff opened this issue · 2 comments

Hello.
Most likely, I'm doing something wrong. Please help me figure it out.

At first I'm creating a key:

def generate_key():
    if free_keys_left() == 0:
        return False

    key_str = _generate_key_string(4)
    while _key_exists(key_str):
        key_str = _generate_key_string(4)

    key = Key(key_str)
    db.session.add(key)
    db.session.commit()
    return key_str

Now key is in the db, right?
In the next step, I try to check if this key exists:

def _key_exists(key_str):
    return Key.query.filter(Key.value == key_str).options(FromCache(cache)).scalar() is not None

But key_exists method gave me False. I'm using filesystem cache type.

You can view the full code here: https://github.com/yoshapihoff/keygen_api

Looking at your github code, it looks like the Flask-Caching object wasn't setup correctly. I believe you need to change line 19 of __init__.py to something like cache = Cache(app, config={'CACHE_TYPE': 'filesystem'}). Or maybe I missed an iteration?

It works! Thank you :)