pythoncachingflaskflask-cache

Flask-cache generate different keys for int and long parameters


Flask-cache use function parameters to generate cache key, however it makes different keys for long and int type parameters:

@cache.memoize(3600)
def foo(a):
    return a

foo(1) and foo(1L) will generate different cache keys, what could I do to assign their return value to a same cache key?


Solution

  • You can convert integer number to long by subclass.

    eg.

    class CustomCache(Cache):
        def _memoize_kwargs_to_args(self, f, *args, **kwargs):
            keyargs, keykwargs = super(CardCache, self) \
                ._memoize_kwargs_to_args(f, *args, **kwargs)
    
            new_args = []
            for arg in keyargs:
                if isinstance(arg, numbers.Integral):
                    arg = long(arg)
                new_args.append(arg)
    
            return tuple(new_args), keykwargs