Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Plone 根据函数';s返回值_Plone_Memoization - Fatal编程技术网

Plone 根据函数';s返回值

Plone 根据函数';s返回值,plone,memoization,Plone,Memoization,我试图缓存一个函数的返回值,但前提是它不是空的 在下面的示例中,缓存someFunction的结果是有意义的,以防它从某个url获取了一小时的数据 如果无法获取数据,则将结果缓存一小时(或更长时间)没有意义,但可能需要5分钟(因此some-domain.com的服务器有时间恢复) 在cachekey中调用方法(self,lang)没有多大意义。在这种情况下,您不应该泛化“返回为无”,因为decorator缓存的结果只能依赖于输入值 相反,您应该在函数内部构建缓存机制,而不是依赖于装饰器 然后这就

我试图缓存一个函数的返回值,但前提是它不是空的

在下面的示例中,缓存someFunction的结果是有意义的,以防它从某个url获取了一小时的数据

如果无法获取数据,则将结果缓存一小时(或更长时间)没有意义,但可能需要5分钟(因此some-domain.com的服务器有时间恢复)


在cachekey中调用
方法(self,lang)
没有多大意义。

在这种情况下,您不应该泛化“返回为无”,因为decorator缓存的结果只能依赖于输入值

相反,您应该在函数内部构建缓存机制,而不是依赖于装饰器

然后这就变成了一个通用的非特定于Plone的Python问题,即如何缓存值

下面是一个如何使用RAMCache构建手动缓存的示例:


由于此代码太长,无法发表评论,我将在这里发布,希望它能帮助其他人:

#initialize cache
from zope.app.cache import ram
my_cache = ram.RAMCache()
my_cache.update(maxAge=3600, maxEntries=20)
_marker = object()


def _cachekey(lang):
    return (lang, time.time() // (60 * 60))


def someFunction(self, lang='en'):

    cached_result = my_cache.query(_cacheKey(lang), _marker)

    if cached_result is _marker:
        #not found, download, compute and add to cache
        data = urllib2.urlopen('http://some-url.com/data.txt', timeout=10).read()
        except socket.timeout:
            data = None
        except urllib2.URLError:
            data = None

        if data is not None:
            #cache computed value for 1 hr
            computed = expensive_compute(data)
            my_cache.set(data, (lang, time.time() // (60 * 60) )
        else:
            # allow download server to recover 5 minutes instead of trying to download on every page load
            computed = None
            my_cache.set(None, (lang, time.time() // (60 * 5) )

        return computed


    return cached_result

非常类似问题的替代解决方案:
#initialize cache
from zope.app.cache import ram
my_cache = ram.RAMCache()
my_cache.update(maxAge=3600, maxEntries=20)
_marker = object()


def _cachekey(lang):
    return (lang, time.time() // (60 * 60))


def someFunction(self, lang='en'):

    cached_result = my_cache.query(_cacheKey(lang), _marker)

    if cached_result is _marker:
        #not found, download, compute and add to cache
        data = urllib2.urlopen('http://some-url.com/data.txt', timeout=10).read()
        except socket.timeout:
            data = None
        except urllib2.URLError:
            data = None

        if data is not None:
            #cache computed value for 1 hr
            computed = expensive_compute(data)
            my_cache.set(data, (lang, time.time() // (60 * 60) )
        else:
            # allow download server to recover 5 minutes instead of trying to download on every page load
            computed = None
            my_cache.set(None, (lang, time.time() // (60 * 5) )

        return computed


    return cached_result