Can memcached be used for locking?
<p><code>memcached</code> can be used for a caching static data which reduces database lookup and typically does <code>memcached.get(id)</code> and <code>memcached.set(id)</code>.</p>

<p>However is it fine to use this for locking mechanisms?  Does <code>memcache.set</code> and <code>memcached.get</code> always give the data if it is present or will it just return None if the request is taking too much time?</p>

<p>I want to avoid concurrent access to a particular resource identified by a <code>id</code> and I use this logic:</p>

<pre><code>def access(id):
    if memcache.get(id):
        return access
    else:
        memcache.set(id)
        return true
</code></pre>

<p>If any user tries to access that resource, if <code>memcache.get(id) = username</code> returns a value we decline the access else we do <code>memcache.set(id) = username</code> to stop subsequent access and allow access for the current user.</p>

<p>Is it fine to using <code>memcached</code> like this? Will <code>set</code> and <code>get</code> actually give the data if its available regardless of the time it takes or does it give best possible result in the least possible time from whatever I have found (for example: <a href="https://stackoverflow.com/a/6538938/452102">Guaranteed memcached lock</a>) so far is of the former category and might not work for locking thought it might work 99% of the time.</p>

<p>Can anyone clarify and if there are alternative locking mechanisms?</p>