如何显式设置 joblib 缓存中的 return 值?

How to set the return value in joblib's cache explicitly?

我想使用 joblib's Memory 为给定参数列表设置函数的缓存值。像这样:

from joblib import Memory

memory = Memory('cachedir')

@memory.cache
def f(x):
    # Placeholder for a heavy task
    print('Running f(%s)' % x)
    return 'Return and store this string %s' % x

# The following line isn't working, but I want something similar:
memory.set_cached_value(f, 1234, 'Return and store this string 1234')

我知道最简单的方法是使用给定的参数 (1234) 调用 f。但在我的情况下(有时)结果无需计算即可获得所以我想设置而不调用重 f.

是否有类似(有效)的:memory.set_cached_value(f, 1234, 'Return and store this string 1234')

解决方法是使用忽略的参数:

from joblib import Memory

memory = Memory('cachedir')

@memory.cache(ignore=['force_val'])
def f(x, force_val=None):
    if force_val is not None:
        return force_val
    # Placeholder for a heavy task
    print('Running f(%s)' % x)
    return 'Return and store this string %s' % x

# Set explicitly:
f(1234, 'Return and store this string 1234')