windows - 如何使用 Windows Azure 表存储时间戳或 Etag 字段

标签 windows node.js azure etag

我正在 Windows Azure 网站 (IISNode) 上开发一个 Node.js 应用程序,并已安装适用于 Node.js 的 Azure SDK 模块,我的问题是如何在表存储中使用 etag 或时间戳字段。

这是“我”做某事的问题吗,例如:

if (entities[0].Timestamp == newEntity.Timestamp)
    // commit this update because we are dealing with the latest copy of this entity
else
    // oops! one of the entities is newer than the other, better not save it

或者我是否需要监听 tableService.updateEntity 返回的错误(...返回的错误或其他内容?

感谢任何帮助或建议

最佳答案

正如 Neil 提到的,好的做法是让表服务处理并发相关的内容。

当我们使用客户端库从表服务中获取实体时,库会将与实体关联的 ETag(存在于实体服务的响应中)存储在 EntityDescriptior 实例中(EntityDescriptor 是由上下文中每个实体实例的库)

当您提交更新实体的请求时,sdk将准备HTTP请求,其中body作为实体序列化为XML,并将请求的ETag header 作为存储在实体对应的实体描述符中的ETag值。

表服务收到更新实体实例的请求时,会检查请求中的 ETag 与当前表存储中实体关联的 ETag 是否匹配(如果某些情况发生变化,则与服务中存储的实体关联的 ETag 是否匹配)其他更新发生在收到您的更新请求之前),如果不匹配,则通过将响应中的 Http 状态代码设置为 412 或 409,服务返回前置条件失败/冲突错误。

bool done = true;
while (!done)
{
    Stat statistics = _context.Execute<Stat>(new Uri("https://management.core.windows.net/<subscription-id>/services/storageservices/StatEntity?RowKey=..&PartitionKey=..").FirstOrDefault();

    try
    {
        // TODO: Update the entity, e.g. statistics.ReadCount++
        _context.UpdateObject(statistics);
        _context.SaveChanges();
        // success
        break;
    }
    catch (DataServiceRequestException exception)
    {
        var response = exception.Response.FirstOrDefault();
        if (response != null && (response.StatusCode == 412 || response.StatusCode == 409))
        {
            // Concurrency Exception - Entity Updated in-between
            // by another thread, Fetch the latest _stat entity and repeat operation
        }
        else
        {
            // log it and come out of while loop
            break;
        }
    }
    catch (Exception)
    {
        // log it and come out of while loop
        break;
    }
}

关于windows - 如何使用 Windows Azure 表存储时间戳或 Etag 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12399194/

相关文章:

windows - NASM + GoLink : "The following symbol was not defined in the object file or files"

javascript - 需要稍后在 Node 上执行的模块

javascript - 如何使用 Node.js 检查目录是否为空?

Azure:运行大量任务的最佳架构,每个任务需要 5 到 10 秒(从 Web api 触发)?

azure - 云服务支付

mysql - Rails - 安装 Gem 时出错

node.js - 我可以在 Windows 中使用命令行升级 Node.js 吗?

azure - Azure 上的 Quartz.net 未找到时区 ID 'Coordinated Universal Time'

Java 程序将编译但不会在命令提示符下运行

node.js - 如何从服务器端(使用 NodeJS sdk)将设备注册到 Azure 通知中心?