锁定实体以在Google Cloud数据存储区中执行get

编程入门 行业动态 更新时间:2024-10-27 19:25:04
锁定实体以在Google Cloud数据存储区中执行get-update-set操作(Locking entities to perform get-update-set operations in Google Cloud datastore)

什么是锁定实体(例如用户(amount_earned))以在Google Cloud Datastore上执行一致的读取 - >更新 - >设置操作的推荐方法。

使用memcache,我们通常会执行memcache.add(lock_key),以确保只有进程/线程修改对象。

由于memcache的add是原子并且返回false,如果先前没有添加key,则很容易使用memcache模拟“锁定”。

如何在Google Cloud数据存储上模拟类似的锁定语义。

理想的情况下

with gcp.lock(lock_key)as key: user.get(user_id) # read user.update(amount) # update user.set() # save # Release the lock, so that other processes can now updated user (identified by user_id) object

What is the recommeneded way to lock an entity (e.g. User(amount_earned)) to perform consistent read --> update --> set operations on Google Cloud Datastore.

Using memcache, we usually do a memcache.add(lock_key), to make sure there is only process/thread modifying a object.

Since memcache's add is atomic and returns false, if key wasn't previously added, it is easy to simulate "locking" using memcache.

How can one simulate similar locking semantic on Google Cloud datastore.

Ideally

with gcp.lock(lock_key)as key: user.get(user_id) # read user.update(amount) # update user.set() # save # Release the lock, so that other processes can now updated user (identified by user_id) object

最满意答案

您也许可以使用交易和乐观锁定策略。

我们以更新用户实体为例。 为了使用乐观锁定,您必须使用特殊属性(例如实体版本)。 每次更新时,版本字段将加1。 更新实体的步骤:

加载实体 假设您有一个允许更新User实体的UI。 在编辑模式下在屏幕上显示实体。 最终用户评论/对实体进行更改 最终用户点击“保存” 现在开始一个交易 使用实体的密钥加载用户实体 比较新加载的实体的版本和先前加载到UI的实体 如果版本匹配,则没有其他人更新实体,因此请继续更新实体并提交事务。 确保在更新/提交时增加版本号。 如果版本不匹配,则其他人更改了实体。 不要更新实体,回滚事务。 采取适当的行动。 例如,您可以向用户显示消息,让他查看最新实体并进行进一步更改。

You could perhaps use Transactions and Optimistic Locking strategy.

Let's take the example of updating User entity. In order to use optimistic locking, you would have to use a special property (e.g. entity version). The version field will be incremented by one for each update. Steps to update an entity:

Load the entity Assume you have a UI that allows updating of User entity. Display the entity on the screen in Edit mode. End user reviews/makes changes to the entity End user clicks Save Now start a transaction Load the User entity using the entity's Key Compare the version of newly loaded entity and the entity previously loaded to the UI If the versions match, no one else updated the entity, so go ahead and update the entity and commit the transaction. Make sure to increment the version number while updating/committing. If the versions do not match, someone else changed the entity. Do not update the entity, rollback the transaction. Take appropriate action. For example, you can display a message to the user and have him review the latest entity and make any further changes.

更多推荐

本文发布于:2023-07-18 10:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1158552.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   区中   数据存储   Cloud   Google

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!