Page 1 of 1
PHP locking
Posted: Mon May 11, 2009 2:02 pm
by danc81
Hi,
I've just started working with PHP and I'm using memcache for storing frequently accessed objects. I sometimes need to update these and it's possible they can be updated from multiple threads so how could I go about locking the object while I update it? I would like to avoid a system-wide lock as this will create a bottleneck but maybe a named mutex or something would be good. Is this possible in PHP?
Re: PHP locking
Posted: Mon May 11, 2009 2:21 pm
by Christopher
Why not get them from a global Registry that does updating and locking internally.
Re: PHP locking
Posted: Tue May 12, 2009 1:27 am
by danc81
What do you mean by "global registry"? I'm using memcache but there is a time when the object is obtained from the cache and its properties are being updated before going back in the cache when I will need to lock it to prevent another thread overwriting the changes.
Re: PHP locking
Posted: Tue May 12, 2009 2:10 am
by Christopher
By Registry I mean have a object through which you access objects in memcache that need locking. That object can deal with locking for both writes and reads. It is a fine grained lock and the check need only be done on those objects that need locking.
Re: PHP locking
Posted: Tue May 12, 2009 2:15 am
by danc81
Ah, OK. Well that's how I do it already, there is a single object which accesses the objects in the memcache, my question is really, what can I use for the "fine grained" locking in PHP? I'm from a C++ background where I would use a mutex or critical section or even a read/write lock but I cannot find any of these concepts in PHP.
Re: PHP locking
Posted: Tue May 12, 2009 4:07 am
by Christopher
There is no language support for locking memcache. You will have to add the lock, replace the object, and delete the lock. But since you access objects through a class, then that class could poll on the lock. That's going to add an extra get for every access to that object unfortunately. I think I would just do replace without locking and see if there is really a problem.