开发者

Using memcache to store obj's in google app engine

开发者 https://www.devze.com 2022-12-27 08:58 出处:网络
I\'m trying to use memcache to cache data retrevied开发者_如何学Python from the datastore. Storing stings works fine. But can\'t one store an object? I get an error \"TypeError: \'str\' object is not

I'm trying to use memcache to cache data retrevied开发者_如何学Python 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))


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.

Resource:

Datastore Functions

0

精彩评论

暂无评论...
验证码 换一张
取 消