wp_cache_add refuses to overwrite; wp_cache_set does not

The two look interchangeable and differ on whether the key already exists: set() overwrites unconditionally, add() fails and returns false. That failure is the useful part.

// only one process wins
if ( wp_cache_add( 'rebuild_running', 1, 'locks', 30 ) ) {
    rebuild_the_index();
    wp_cache_delete( 'rebuild_running', 'locks' );
}

With a persistent backend that supports it, add() is atomic, which makes it a usable lightweight lock for exactly this — stopping two requests from starting the same expensive rebuild. Without a persistent object cache it is per-request and the lock is meaningless, so the pattern only works on a site that has one. Always give it an expiry, or a process that dies holding the lock holds it forever.