Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

The dictionary .get() method allows you to specify a default value, such as dict.get('key', 'defaultval')

This can be useful if it's possible you might want to store None as a value in a dictionary.

However, the memcache.get() does not let you to do this. I've modified my @memoize decorator so it looks like this:

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. Is there a better way I can handle this besides converting None vals to strings and back?

like image 800
jamtoday Avatar asked May 21 '09 21:05

jamtoday


People also ask

How much data can memcache store?

The following limits apply to the use of the memcache service: The maximum size of a cached data value is 1 MB (10^6 bytes). A key cannot be larger than 250 bytes.

How does memcache store data?

How does Memcached work? Unlike databases that store data on disk or SSDs, Memcached keeps its data in memory. By eliminating the need to access disks, in-memory key-value stores such as Memcached avoid seek time delays and can access data in microseconds.

Where does memcache store data?

Memcached stores the data in the memory and when the data is needed the application checks for the data in the memcache via its daemon. Memcached has the ability to store SQL queries, that way the next time the query is ran, it can return the result from memory.


2 Answers

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). Alternatively, you could use the empty string "", or avoid storing None/null values altogether (absence of the key implies None).

Then check for instances of your 'None' class when you check the result of mc.get(key) (is None, == "", etc)

like image 160
Dana the Sane Avatar answered Nov 02 '22 08:11

Dana the Sane


You could do something like what Haskell and Scala does and store an Option dictionary. 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; 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.

like image 31
tomjen Avatar answered Nov 02 '22 08:11

tomjen