简体   繁体   中英

Flask-Cache keeps missing cache for instance methods

I have the following class:

class Account(db.Model):
    __tablename__ = 'accounts'
    __versioned__ = {
        'exclude': ['VPSs']
    }

    id = db.Column(db.Integer, primary_key=True)

    def __repr__(self):
        return "%s(%s)" % (self.__class__.__name__, self.id)

    @property
    @cache.memoize(timeout=86400)
    def days_to_topup(self):
        """Rather than using inline model form, just reflect this
        """
        return self.vendor.days_to_topup

I had a feeling I wasn't hitting cache at all. So I added the following in my library file:

def get(self, key):
    try:
        expires, value = self._cache[key]
        if expires == 0 or expires > time():
            return pickle.loads(value)
    except (KeyError, pickle.PickleError):
        print("Key missing: %s" % key)
        return None

The first time I accessed this method, I see the following being printed in my console:

Key missing: portal.account.models.Account.days_to_topup.Account25_memver
Key missing: pAZujNOzo2JX6Tz6T4O3UdS7nzAw
Key missing: portal.account.models.Account.days_to_topup.Account24_memver
Key missing: s3GWC1YnjUGIW/QDT4O3UdwsxOfQ
Key missing: HHb45l3zEGDTepyCT4O3UdTninm7
Key missing: portal.account.models.Account.days_to_topup.Account20_memver
Key missing: tW0ezIHoB3mRHHd4T4O3UduywXbT
Key missing: portal.account.models.Account.days_to_topup.Account19_memver
Key missing: 73KXuVicSsFECnXcT4O3UdDYt/2Z

...

And then I decided to access this method again, I realized I am seeing the same "Key Missing":

Key missing: portal.account.models.Account.days_to_topup.Account25_memver
Key missing: pAZujNOzo2JX6Tz6njTvFgk2N2jF
Key missing: portal.account.models.Account.days_to_topup.Account24_memver
Key missing: s3GWC1YnjUGIW/QDnjTvFg2r/Ip2
Key missing: portal.account.models.Account.days_to_topup.Account22_memver
Key missing: HHb45l3zEGDTepyCnjTvFgU/EEE6
Key missing: portal.account.models.Account.days_to_topup.Account20_memver
Key missing: tW0ezIHoB3mRHHd4njTvFgQpKev8
Key missing: portal.account.models.Account.days_to_topup.Account19_memver

So I don't understand why @cache.memoization is not working as it is intended to.

At first I thought it is because the variable self kept changing, but that wouldn't make sense since I have repr defined

Why am I missing my cache hit all the time?

Actually I found out that I have so much to cache that the default CACHE_THRESHOLD=500 was getting filled up too quickly resulting in a lot of cache misses.

Increasing THRESHOLD to a larger value solved my problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM