Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6015159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:47:45+00:00 2026-05-23T02:47:45+00:00

I’m trying to use spymemcached 2.6 with synchronization on update and I found the

  • 0

I’m trying to use spymemcached 2.6 with synchronization on update and I found the two following way to use it:

  • using CASMutation defining a CASMutator which is quite an invasive way to implement it, let see an example:

    public List<Item> addAnItem(final Item newItem) throws Exception {
    
        // This is how we modify a list when we find one in the cache.
        CASMutation<List<Item>> mutation = new CASMutation<List<Item>>() {
    
            // This is only invoked when a value actually exists.
            public List<Item> getNewValue(List<Item> current) {
                // Not strictly necessary if you specify the storage as
                // LinkedList (our initial value isn't), but I like to keep
                // things functional anyway, so I'm going to copy this list
                // first.
                LinkedList<Item> ll = new LinkedList<Item>(current);
    
                // If the list is already "full", pop one off the end.
                if(ll.size() > 10) {
                    ll.removeLast();
                }
                // Add mine first.
                ll.addFirst(newItem);
    
                return ll;
            }
        };
    
        // The initial value -- only used when there's no list stored under
        // the key.
        List<Item> initialValue=Collections.singletonList(newItem);
    
        // The mutator who'll do all the low-level stuff.
        CASMutator<List<Item>> mutator = new CASMutator<List<Item>>(client, transcoder);
    
        // This returns whatever value was successfully stored within the
        // cache -- either the initial list as above, or a mutated existing
        // one
        return mutator.cas("myKey", initialValue, 0, mutation);
    }
    

or by using cas method

cas(String key, long casId, Object value)

after have done:

gets(String key, Transcoder<T> tc) 

The second one is really more simple, and I understand why I would use CASMutation…
I would be really pleased to get some feedback about the use of this couchbase client.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:47:46+00:00Added an answer on May 23, 2026 at 2:47 am

    CASMutator/CASMutation captures the best practices and workflow so that the right thing is done for you.

    Your counter-example looks more simple since you aren’t saying what you’d actually do with those methods. The sample you posted above is showing a List being pulled out of memcached, a new item added to it, conditionally removing some items from it, and then putting it back. At least half of that text you posted you’d still need to write.

    If you don’t use CASMutator, you’ll end up reinvent it, and that’s not all that simple. This is what it does for you today:

    public T cas(final String key, final T initial, int initialExp,
            final CASMutation<T> m) throws Exception {
        T rv=initial;
    
        boolean done=false;
        for(int i=0; !done && i<max; i++) {
            CASValue<T> casval=client.gets(key, transcoder);
            T current=null;
            // If there were a CAS value, check to see if it's compatible.
            if(casval != null) {
                T tmp = casval.getValue();
                current=tmp;
            }
            // If we have anything mutate and CAS, else add.
            if(current != null) {
                // Declaring this impossible since the only way current can
                // be non-null is if casval was set.
                assert casval != null : "casval was null with a current value";
    
                rv=m.getNewValue(current);
                // There are three possibilities here:
                //  1) It worked and we're done.
                //  2) It collided and we need to reload and try again.
                //  3) It disappeared between our fetch and our cas.
                // We're ignoring #3 because it's *extremely* unlikely and the
                // behavior will be fine in this code -- we'll do another gets
                // and follow it up with either an add or another cas depending
                // on whether it exists the next time.
                if(client.cas(key, casval.getCas(), rv, transcoder)
                        == CASResponse.OK) {
                    done=true;
                }
            } else {
                // No value found, try an add.
                if(initial == null) {
                    done = true;
                    rv = null;
                } else if(client.add(key, initialExp, initial, transcoder).get()) {
                    done=true;
                    rv=initial;
                }
            }
        }
        if(!done) {
            throw new RuntimeException("Couldn't get a CAS in " + max
                + " attempts");
        }
    
        return rv;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.