memcached
can be used for a caching static data which reduces database lookup and typically does memcached.get(id)
and memcached.set(id)
.
However is it fine to use this for locking mechanisms? Does memcache.set
and memcached.get
always give the data if it is present or will it just return None if the request is taking too much time?
I want to avoid concurrent access to a particular resource identified by a id
and I use this logic:
def access(id):
if memcache.get(id):
return access
else:
memcache.set(id)
return true
If any user tries to access that resource, if memcache.get(id) = username
returns a value we decline the access else we do memcache.set(id) = username
to stop subsequent access and allow access for the current user.
Is it fine to using memcached
like this? Will set
and get
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: Guaranteed memcached lock) so far is of the former category and might not work for locking thought it might work 99% of the time.
Can anyone clarify and if there are alternative locking mechanisms?