谷歌应用引擎 + Memcache如何从缓存中获取所有数据

发布于 2024-10-18 01:48:10 字数 376 浏览 2 评论 0原文

在我的系统中,我有一些寿命很短的数据,这意味着这些数据实际上仍然不会存在很长时间,但应该保留在数据存储中。 该数据还可以针对每个用户频繁地改变,例如每分钟改变一次。 潜在的用户数量可能足够大,我想通过使用 memcache 来加速这些数据的放入/获取过程,并延迟保留到大表。

只需通过键放置/获取对象就没有问题。但对于某些用例,我需要从仍然存在的缓存中检索所有数据,但 api 允许我仅通过键获取数据。因此,我需要有一些密钥持有者知道内存缓存内数据的所有密钥...但是任何对象都可能被驱逐,我需要从密钥的全局注册表中删除此密钥(但这样的侦听器在 GAE 中不起作用)。为了将所有这些对象存储在列表中,地图不适合我的解决方案,因为每个对象都应该有自己的驱逐时间...

有人可以推荐我应该以哪种方式移动吗?

Inside my system I have data with a short lenght of life, it means that the data is still actuall not for a long time but shold be persisted in data store.
Also this data may be changed frequently for each user, for instance each minute.
Potentially amount of users maybe large enough and I want to speed up the put/get process of this data by usage of memcache and delayed persist to the bigtable.

No problems just to put/get objects by keys. But for some use cases I need to retrieve all data from cache that still alive but api allows me to get data only by keys. Hence I need to have some key holder that knows all keys of the data inside memcache... But any object may be evicted and I need to remove this key from global registry of keys (but such listener doesn't work in GAE). To store all this objects in the list a map is not accaptable for my solution because each object should has it's own time to evict...

Could somebody recommend me in which way I should move?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦晓ヶ微光ヅ倾城 2024-10-25 01:48:10

听起来您真正想要做的是为您将保留的数据建立某种队列。对此,Memcache 不是一个好的选择,因为正如您所说,它不可靠(也不应该可靠)。也许使用任务队列会更好?

It sounds like what you really are attempting to do is have some sort of queue for data that you will be persisting. Memcache is not a good choice for this since as you've said, it is not reliable (nor is it meant to be). Perhaps you would be better off using Task Queues?

夏日浅笑〃 2024-10-25 01:48:10

Memcache 并不是为详尽的访问而设计的,如果您需要它,您可能会以错误的方式使用它。 Memcache 是一个分片哈希表,因此实际上并不是为了枚举而设计的。

从您的描述中并不清楚您想要做什么,但听起来至少您需要重组数据,以便您在想要将其写入数据存储时知道预期的键。

Memcache isn't designed for exhaustive access, and if you need it, you're probably using it the wrong way. Memcache is a sharded hashtable, and as such really isn't designed to be enumerated.

It's not clear from your description exactly what you're trying to do, but it sounds like at the least you need to restructure your data so you're aware of the expected keys at the time you want to write it to the datastore.

最美的太阳 2024-10-25 01:48:10

由于我遇到了同样的问题,我可以通过构建一个装饰器函数并在其周围包装驱逐函数来解决这个问题,以便实体的密钥自动从内存缓存上的密钥目录/占位符中删除,即当您调用驱逐时。

像这样的事情:

def decorate_evict_decorator(key_prefix):

    def evict_decorator(evict):         
        def wrapper(self,entity_name_or_id):#use self if the function is bound to a class.
            mem=memcache.Client()
            placeholder=mem.get("placeholder")#could use gets with cas
            #{"placeholder":{key_prefix+"|"+entity_name:key_or_id}}
            evict(self,entity_name_or_id)
            del placeholder[key_prefix+"|"+entity_name]
            mem.set("placeholder",placeholder)
        return wrapper
    return evict_decorator


class car(db.Model):
    car_model=db.StringProperty(required=True)
    company=db.StringProperty(required=True)
    color=db.StringProperty(required=True)
    engine=db.StringProperty()

    @classmethod
    @decorate_evict_decorator("car")
    evict(car_model):
        #delete process

class engine(db.Model):
    model=db.StringProperty(required=True)
    cylinders=db.IntegerProperty(required=True)
    litres=db.FloatProperty(required=True)
    manufacturer=db.StringProperty(required=True)

    @classmethod
    @decorate_evict_decorator("engine")
    evict(engine_model):
        #delete process

您可以根据您的数据结构和流程对此进行改进。以及有关装饰器的更多信息。

您可能需要添加cron 使您的数据存储区定期与内存缓存同步。

Since I am encountering the very same problem, which I might solve by building a decorator function and wrap the evicting function around it so that key to the entity is automatically deleted from key directory/placeholder on memcache, i.e. when you call for eviction.

Something like this:

def decorate_evict_decorator(key_prefix):

    def evict_decorator(evict):         
        def wrapper(self,entity_name_or_id):#use self if the function is bound to a class.
            mem=memcache.Client()
            placeholder=mem.get("placeholder")#could use gets with cas
            #{"placeholder":{key_prefix+"|"+entity_name:key_or_id}}
            evict(self,entity_name_or_id)
            del placeholder[key_prefix+"|"+entity_name]
            mem.set("placeholder",placeholder)
        return wrapper
    return evict_decorator


class car(db.Model):
    car_model=db.StringProperty(required=True)
    company=db.StringProperty(required=True)
    color=db.StringProperty(required=True)
    engine=db.StringProperty()

    @classmethod
    @decorate_evict_decorator("car")
    evict(car_model):
        #delete process

class engine(db.Model):
    model=db.StringProperty(required=True)
    cylinders=db.IntegerProperty(required=True)
    litres=db.FloatProperty(required=True)
    manufacturer=db.StringProperty(required=True)

    @classmethod
    @decorate_evict_decorator("engine")
    evict(engine_model):
        #delete process

You could improve on this according to your data structure and flow. And for more on decorators.

You might want to add a cron to keep your datastore in sync the memcache at a regular interval.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文