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 8328509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:28:05+00:00 2026-06-09T01:28:05+00:00

I am using memcache with CakePHP. I am running a multithreaded process which updates

  • 0

I am using memcache with CakePHP.

I am running a multithreaded process which updates the memcache key.

So there is a 10% probability that two different processes end up updating the same memcache key, which throws an error.

  • 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-06-09T01:28:07+00:00Added an answer on June 9, 2026 at 1:28 am

    You can “lock” a key before setting it using code like the following.

    This code assumes you have the Instrumentation-for-php library:

    http://code.google.com/p/instrumentation-for-php

    Just comment out the instrumentation lines if you don’t want to use it

    function acquire_mutex_or_wait($key,$memcache = false) {
        Instrumentation::get_instance()->increment('memcache_mutex_requests', 1);
        if($memcache === false) {
                #bring in memcache from global scope
                global $memcache;
        }
        #prepend LOCK:: to the key to designate the new key as a lock for the given key
        $key = "LOCK::$key";
    
        #generate a random number for the lock (see below)
        $lockval = mt_rand(1,2^31);
    
        #add is SUPPOSED to be atomic, but it isn't really with multiple servers
        #to protect against two connections getting the mutex we initialize it
        #to a random value and check the value after setting.  If we didn't
        #really aquire the mutex, the value we get will be different from the
        #value we set
        $got_lock = $memcache->add($key, $lockval);
        if($got_lock) {
                $got_lock = $memcache->get($key);
                $got_lock = $got_lock === $lockval;
                if($got_lock) return true;
        }
    
        #the mutex was not acquired.  we must wait for it to be released
        $sleep_time = .01; #initial sleep time
        $sleep_time_increment = .05;
        $max_sleep_time=1; #maximum number of seconds to sleep between checking locks
        Instrumentation::get_instance()->timer();
        Instrumentation::get_instance()->increment('memcache_mutex_waits', 1);
        while(1) {
                usleep($sleep_time * 1000000);
                $sleep_time += $sleep_time_increment;
                if($sleep_time > $max_sleep_time) $sleep_time = $max_sleep_time;
                $exists = $memcache->get($key);
                if(!$exists) break; #mutex has been released
        }
        Instrumentation::get_instance()->increment('memcache_mutex_wait_time',   Instrumentation::get_instance()->timer());
        return false;
    }
    
    #NOTE - only the connection that acquired a mutex should release it.  there is no
    #protection to ensure this is the case
    function release_mutex($key, $memcache = false) {
        if($memcache === false) {
                #bring in memcache from global scope
                global $memcache;
        }
    
        $key = "LOCK::$key";
        return($memcache->delete($key));
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to start using memcache with PHP (on Ubuntu 9.10). There are lots
I am using Memcache in my ASP.NET application. I have 3 memcache servers that
Three Apache2 web servers running a PHP 5.2.3 web site. We're using Memcache to
I'm just wondering if there is a way to clear memcache using wildcards for
I am using memcache to store larger session data that is choking the MYSQL/PHP
In my current project, I am using Memcache to store key-value pairs, but since
I'm using memcache for caching (obviously) which is great. But I'm also using it
Having used PHP on Linux a lot, I'm used to using memcache so that
I have configured CakePHP 2.1 to use Memcache . But debugkit insists on using
I am using memcached (gem memcache-client ) in my application, but I want to

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.