在沙发上更新 [英] Update in couchbase

查看:49
本文介绍了在沙发上更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些使用Couchbase进行更新的帮助.我的页面上有一个任务.如果用户单击喜欢",则应该在我的长沙发"存储桶中更新喜欢"计数.我已经尝试了自己的更新处理程序代码,但是有一些时间延迟.我也在下面也包含了我的更新代码.

I need some help in update using couchbase. I have a task in my page. If the user clicks the like then the likes count should be updated in my couchbase bucket. I have tried my own update handler code but that has some time latency. I have included my update code too below.

这是我喜欢执行任务的代码...

This is my code for liking a task...

public ResponseVO LikeTask(LikeVO likeVO)
        {
            ResponseVO response = new ResponseVO();

            try
            {
                if (!isLiked(likeVO.TaskID, likeVO.UserID))
                {

                    UpdateTaskDB likeUpdate = new UpdateTaskDB();

                    UpdateTaskVO updatetaskvo = new UpdateTaskVO();
                    updatetaskvo.FieldName = "Likes";
                    LikeVO tempvo = new LikeVO();
                    tempvo.LikedOn = DateTime.Now.ToString();
                    tempvo.UserID = likeVO.UserID;
                    tempvo.UserName = likeVO.UserName;
                    tempvo.TaskID = likeVO.TaskID;
                    updatetaskvo.ObjectValue = tempvo;
                    updatetaskvo.TaskID = likeVO.TaskID;
                    likeUpdate.UpdateDocument(updatetaskvo);
              }
                response.StatusMessage = "Liked Successfully";
            }
            catch (Exception ex)
            {
                response.StatusCode = "0";
                response.StatusMessage = ex.Message;
            }

            return response;
        }

我自己的更新处理程序代码:

My own update handler code:

 public class UpdateTaskDB
    {
        CouchbaseClient oCouchbase;
        public UpdateTaskDB()
        {
            oCouchbase = new CouchbaseClient("vwspace", "");
        }
        public TaskVO GetTaskByID(string task_id)
        {

            TaskVO results = null;
            try
            {
                String str1;

                str1 = (String)oCouchbase.Get(task_id);

                results = JsonConvert.DeserializeObject<TaskVO>(str1);
            }
            catch (Exception ex)
            {

            }
            return results;
        }

        public void UpdateDocument(UpdateTaskVO inputParams)
        {
            try
            {
                var client = new CouchbaseClient("vwspace", "");
                TaskVO taskDoc = GetTaskByID(inputParams.TaskID);

                switch (inputParams.FieldName)
                {
                    case "Likes":
                        List<LikeVO> docLikes = taskDoc.likes;
                        docLikes.Add((LikeVO)inputParams.ObjectValue);
                        taskDoc.likes = docLikes;
                        break;
                    case "UnLike":
                        LikeVO unlikevo = (LikeVO)inputParams.ObjectValue;
                        for (int count = 0; count < taskDoc.likes.Count; count++)
                        {
                            if (taskDoc.likes[count].UserID.Equals(unlikevo.UserID))
                            {
                                unlikevo = taskDoc.likes[count];
                                break;
                            }
                        }
                        taskDoc.likes.Remove(unlikevo);
                        break;

                    default:
                        break;
                }

                String json = JsonConvert.SerializeObject(taskDoc);
                client.Store(StoreMode.Set, inputParams.TaskID, json);

            }
            catch (Exception ex)
            {
                Console.Write("Exception :" + ex.Message);
            }
        }
    }

还有其他方法可以在Couchbase中处理此更新吗?请帮助我.

Is ther any other way to handle this update in couchbase? Kindly help me out..

推荐答案

您看到的延迟很可能是由于您为每次点击创建了两个CouchbaseClient实例.创建CouchbaseClient的实例是一项昂贵的操作,因为会进行自举和配置设置.

The latency you're seeing is likely due to the fact that you're creating two instances of the CouchbaseClient for each click. Creating an instance of a CouchbaseClient is an expensive operation, because of the bootstrapping and configuration setup that takes place.

您可以采取两种不同的方法来最小化创建CouchbaseClient实例的频率.一种方法是创建一个静态客户端,供您的数据访问类重用.Web应用程序的另一种方法是将实例与HttpApplication实例相关联.有关Web方法的示例,请参见下面我在GitHub上的(不完整)示例项目.

There are a couple of different approaches you can take to minimize how frequently you create CouchbaseClient instances. One would be to create a static client that is reused by your data access classes. Another approach for web apps is to associate instances with HttpApplication instances. For an example of the Web approach, see my (incomplete) sample project on GitHub below.

https://github.com/jzablocki/couchbase-beer.net/blob/master/src/CouchbaseBeersWeb/Models/WebRepositoryBase%271.cs

此外,我建议在更新文档的计数时使用CAS操作.您要确保喜欢"投票不会导致整个文档从陈旧的读取中被更新.

Also, I would suggest using CAS operations when updating a document's like count. You want to make sure that a "like" vote doesn't cause the entire document to be update from a stale read.

例如:

public TaskVO GetTaskByID(string task_id)
{
    var getResult = oCouchbase.ExecuteGet<string>(task_id);   
    var results = JsonConvert.DeserializeObject<TaskVO>(str1.Value);
    results.Cas = getResult.Cas; //Here I'm suggesting adding a Cas property to your TaskVO
    return results;
}

然后更新:

public void UpdateDocument(UpdateTaskVO inputParams)
{
    try
    {
        TaskVO taskDoc = GetTaskByID(inputParams.TaskID);

        switch (inputParams.FieldName)
        {
            ...
        }        

        String json = JsonConvert.SerializeObject(taskDoc);
        client.ExecuteStore(StoreMode.Set, inputParams.TaskID, json, taskDoc.Cas); 
        //this will fail if the document has been updated by another user.  You could use a retry strategy    
    }
    catch (Exception ex)
    {
        Console.Write("Exception :" + ex.Message);
    }
}

这篇关于在沙发上更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆