views:

73

answers:

1

Hi,

I'm trying to use memcache to cache data retrevied from the datastore. Storing stings works fine. But can't one store an object? I get an error "TypeError: 'str' object is not callable" when trying to store with this:

pageData = StandardPage(page)    
memcache.add(memcacheid, pageData, 60)

I've read in the documentation that it requires "The value type can be any value supported by the Python pickle module for serializing values." But don't really understand what that is. Or how to convert pageData to it.

Any ideas?

..fredrik

EDIT:

I was a bit unclear. PageType is an class that amongst other thing get data from the datastore and manipulate it. The class looks like this:

class PageType():
    def __init__(self, page):
        self.pageData = PageData(data.Modules.gql('WHERE page = :page', page = page.key()).fetch(100))
        self.modules = []

    def renderEditPage(self):
        self.addModules()
        return self.modules



class StandardPage(PageTypes.PageType):
    templateName = 'Altan StandardPage'
    templateFile = 'templates/page.html'

    def __init__(self, page):
        self.pageData = PageTypes.PageData(data.Modules.gql('WHERE page = :page', page = page.key()).fetch(100))
        self.modules = []
        self.childModules = []

        for child in page.childPages:
            self.childModules.append(PageTypes.PageData(data.Modules.gql('WHERE page = :page', page = child.key()).fetch(100)))

    def addModules(self):
        self.modules.append(PageTypes.getStandardHeading(self, 'MainHeading'))
        self.modules.append(PageTypes.getStandardTextBox(self, 'FirstTextBox'))
        self.modules.append(PageTypes.getStandardTextBox(self, 'SecondTextBox'))
        self.modules.append(PageTypes.getStandardHeading(self, 'ListHeading'))
        self.modules.append(PageTypes.getStandardTextBox(self, 'ListTextBox'))
        self.modules.append(PageTypes.getDynamicModules(self))
A: 

You can use db.model_to_protobuf to turn your object into something that can be stored in memcache. Similarly, db.model_from_protobuf will get your object back.

http://code.google.com/appengine/docs/python/datastore/functions.html

Adam Crossland
Thanks. But I realize that I was a bit unclear. I will edit my question above.
fredrik
What is the exact error that is getting thrown? I'm thinking that it is `pageData = StandardPage(page)` and you have a scope error in your code. If not, what is the output of `print type(PageData)` after the first call?
msw