PHP locking

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danc81
Forum Newbie
Posts: 16
Joined: Mon May 11, 2009 2:00 pm

PHP locking

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP locking

Post by Christopher »

Why not get them from a global Registry that does updating and locking internally.
(#10850)
danc81
Forum Newbie
Posts: 16
Joined: Mon May 11, 2009 2:00 pm

Re: PHP locking

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP locking

Post 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.
(#10850)
danc81
Forum Newbie
Posts: 16
Joined: Mon May 11, 2009 2:00 pm

Re: PHP locking

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP locking

Post 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.
(#10850)
Post Reply