高并发下membase的数据能否加锁

发布于 2021-11-06 17:13:27 字数 92 浏览 685 评论 3

怎么锁定membase中的数据啊

在高并发情况下.....

急啊.....................................

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

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

发布评论

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

评论(3

策马西风 2021-11-10 17:09:45

Here is an example for .net application. You can use it for java in similar way:

A CAS (Check And Set) operation is the correct way to ensure that operations happen without data loss without requiring distributed locking protocols or transactions to be handled. The way it works is you get the value of something with its cas. This is a monotonically increasing number that can be thought of as the version number of the object in the database. You mutate the object (change it in some way) and use a Cas operation to write it back to the database. This will fail (a casResult whose Result property is set to false) if some other client had modified the value in the database in the meantime. Notice the use of the '??' operator. This says assign to the list variable result.Result if it is not null; otherwise create an empty list

/// <summary>
/// Adds a value to a list of type T associated with the given Key using a
/// CAS operation and retries.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="client"></param>
/// <param name="key"></param>
/// <param name="entry"></param>
private static void AddToListWithCas<T>(MembaseClient client, string key,
                                        T entry)
{
    CasResult<bool> casResult;
    do
    {
        var result = client.GetWithCas<List<T>>(key);
        var list = result.Result ?? new List<T>();
        var cas = result.Cas;

        // Avoid duplicates
        if (list.Contains(entry)) return;

        list.Add(entry);
        casResult = client.Cas(StoreMode.Set, key, list, cas);

    } while (casResult.Result == false);
}

长安忆 2021-11-10 03:31:43

具体用JAVA怎么操作啊

平生欢 2021-11-07 03:20:23

You can refer to this wiki article:

http://www.couchbase.org/wiki/display/membase/Locking

Locking 

Advisory locks can be useful to control access to scarce resources. For example, retrieving information from backend or remote systems might be slow and consume a lot of resources. Instead of letting any client access the backend system and potentially overwhelm the backend system with high concurrent client requests, you could create an advisory lock to allow only one client at a time access the backend.

Advisory locks in Membase or Memcached can be created by setting expiration times on a named data item and by using the 'add' and 'delete' commands to access that named item. The 'add' or 'delete' commands are atomic, so you can be know that only one client will become the advisory lock owner.

The first client that tries to ADD a named lock item (with an expiration timeout) will succeed. Other clients will see error responses to an ADD command on that named lock item, so they can know that some other client is owning the named lock item. When the current lock owner is finished owning the lock, it can send an explicit DELETE command on the named lock item to free the lock.

If the lock owning client crashes, the lock will automatically become available to the next client that polls for the lock (using 'add') after the expiration timeout.

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