简体   繁体   English

如果我想在memcache中存储None值怎么办?

[英]What if I want to store a None value in the memcache?

This is specifically related to the Google App Engine Memcache API, but I'm sure it also applies to other Memcache tools. 这与Google App Engine Memcache API特别相关,但我确信它也适用于其他Memcache工具。

The dictionary .get() method allows you to specify a default value, such as dict.get('key', 'defaultval') 字典.get()方法允许您指定默认值,例如dict.get('key','defaultval')

This can be useful if it's possible you might want to store None as a value in a dictionary. 如果您可能希望将None存储为字典中的值,则此选项非常有用。

However, the memcache.get() does not let you to do this. 但是,memcache.get()不允许您这样做。 I've modified my @memoize decorator so it looks like this: 我修改了我的@memoize装饰器,所以它看起来像这样:

def memoize(keyformat, time=1000000):

    """Decorator to memoize functions using memcache."""
    def decorator(fxn):
        def wrapper(*args, **kwargs):
            key = keyformat + str(args[1:]) + str(kwargs)
            from google.appengine.api import memcache
            data = memcache.get(key)
            if Debug(): return fxn(*args, **kwargs) 
            if data:
                if data is 'None': data =  None
                return data
            data = fxn(*args, **kwargs)
            if data is None: data = 'None' 
            memcache.set(key, data, time)
            return data
        return wrapper
    return decorator  

Now I'm sure there's a good argument that I shouldn't be storing None values in the first place, but let's put that aside for now. 现在我确定有一个很好的论据,我不应该首先存储None值,但是现在让我们把它放在一边。 Is there a better way I can handle this besides converting None vals to strings and back? 除了将无val转换为字符串并返回之外,还有更好的方法可以解决这个问题吗?

A possible way to do this is to create new class that defines None for this purpose, and assign instances of this to the cache (unfortunately you cannot extend None ). 一种可能的方法是创建为此目的定义None新类,并将其实例分配给缓存(遗憾的是,您不能扩展None )。 Alternatively, you could use the empty string "", or avoid storing None/null values altogether (absence of the key implies None). 或者,您可以使用空字符串“”,或者完全避免存储None / null值(缺少键意味着None)。

Then check for instances of your 'None' class when you check the result of mc.get(key) ( is None , == "" , etc) 当你检查mc.get(key)的结果时,检查你的'None'类的实例( is None== ""等)

You could do something like what Haskell and Scala does and store an Option dictionary. 您可以执行类似Haskell和Scala的操作并存储Option字典。 The dictionary contains two keys: one key to indicate that it is valid and one key that is used to hold the data. 该字典包含两个键:一个键表示它是有效的,一个键用于保存数据。 Something like this: 像这样的东西:

{valid: true, data: whatyouwanttostore}

Then if get return None, you know that the cache was missed; 然后如果get返回None,你知道缓存丢失了; if the result is a dictionary with None as the data, the you know that the data was in the cache but that it was false. 如果结果是包含None作为数据的字典,则您知道数据在缓存中但是它是错误的。

Not really. 并不是的。

You could store a None value as an empty string, but there isn't really a way to store special data in a memcache. 您可以将None值存储为空字符串,但实际上没有办法在memcache中存储特殊数据。

What's the difference between the cache key not existing and the cache value being None? 缓存密钥不存在与缓存值为None之间有什么区别? It's probably better to unify these two situations. 统一这两种情况可能更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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