First off, your locking will not work because if a lock item disappears from the cache you have no way of knowing if that's because someone released it, or if it's because the item was evicted or expired. Memcached is a cache, not a datastore. You can never be certain that you will get back an item that you have cached.
Second, if I'm understanding you correctly, what you want to do is this: 1) Get an item from the cache 2) Manipulate the item (this may take some time) 3) Put the item back in the cache ...while not allowing anyone else to change the item until you've put it back. For the above, use CAS. It is designed for exactly the above use-case. If your memcached client doesn't support CAS, get another client that does. Implementing a lock with memcached will not work. /Henrik On Fri, Jan 29, 2010 at 14:39, nEosAg <[email protected]> wrote: > Hi, > > > @Brian: it was typing mistake ..i agree to whatever you are saying. > > @Henrik: recently i knew about CAS from Adam Lee, still not > implemented as i am using pecl/memcache client library which doesn't > have that method. > > i'm storing packet not an item, by packet i mean for a > given key i will store Array of items. > > As far as locking is concerned its Logical, my code will add a key > with name lock_<key name>, here <key name> is memcache key. > > If another request came for same memcache key then, code will again > try to add key with that name which is already added so will get > failed which gives us illusion of item/packet being locked. > > After finishing my operation i delete that lock_<key_name> item which > i called "releasing of lock", so that another waiting request would be > able to add it and so on. > > > Thanks for the replies. > > > Regards, > Sagar > > > > On Jan 29, 5:40 pm, Henrik Schröder <[email protected]> wrote: > > I don't understand what you mean by integrity and *why* you are locking. > > > > Are you storing multiple related items, and trying to ensure that they > all > > get set together? This is probably a bad idea. How would your application > > handle single items being evicted from a group of related items? > > > > Are you storing single items, but need to know if someone else has > modified > > it? Then use CAS. > > > > Are you just storing single items? No need to lock, memcached is atomic > in > > itself. > > > > Or are you doing something completely different? > > > > And how are you doing the locking? Distributed? Non-distributed? Through > > memcached or some other mechanism? > > > > /Henrik > > > > On Fri, Jan 29, 2010 at 10:57, nEosAg <[email protected]> wrote: > > > Hi Henrik, > > > Thanks for the reply. > > > > > I put the lock around the gets and sets becoz of my requirement. Each > > > set in cache is manipulated on existing data and then set so to keep > > > integrity i must have to put lock so that no other process could > > > acquire the lock and destroy the integrity. > > > > > I do have persistent storage as DB but our First point of delivery to > > > page is cache and not DB, if cache fails then it will be handled by > > > code to fetch from DB. > > > > > But as far as our Cache server is Up, I have to make sure that data > > > integrity is not Lost!! > > > > > and becoz to acquire Lock i have implemented Strict locking i.e. Dont > > > proceed until lock is acquired, so in that case, i need that status. I > > > m giving pseudo -code for lock then it will be more clear, > > > > > pseudo_lock = "lock_".<key> > > > IS_LOCK = false > > > while ( 1 ) { /loop indefinitely > > > if ( get_pseudo_lock_success ) { > > > IS_LOCK = true > > > break; > > > } > > > usleep // wait for microsecond > > > } > > > > > if ( IS_LOCK) //Then only proceed > > > else //do DB Opearation > > > > > Now in this scenario, if dont know the status of the server my loop > > > will be forever !! > > > > > Waiting for a favourable reply. > > > > > Regards, > > > Sagar > > > > > On Jan 29, 2:16 pm, Henrik Schröder <[email protected]> wrote: > > > > Why do you need to put a lock around your memcached code? All > memcached > > > > operations are atomic in themselves. > > > > > > Why do you need to know the status of a server before you do a > memcached > > > > operation? Doesn't your client library return whether a set operation > > > > succeeded or failed? > > > > > > They way it's supposed to work is that you should never really care > about > > > > individual servers. You do your sets and gets, each item goes to some > > > > server, and when you get an item you may or may not get it back. > Maybe it > > > > expired. Maybe the server it went to was down. Either case, it's a > cache, > > > > and your application should be able to handle cache misses. > > > > > > /Henrik > > > > > > On Fri, Jan 29, 2010 at 06:03, nEosAg <[email protected]> > wrote: > > > > > Hi, > > > > > > > I am using PHP Memcache client library and NOT Memcached library. > > > > > > > "How can i know if Memcache server is Running except > getServerStatus() > > > > > method of PHP client Memcache Library??" > > > > > > > Why i am saying not using getServerStatus() call is because that > > > > > method works only with individual server but at runtime Code will > > > > > never knew which server has been chosen by Hash policy, so we are > > > > > having pool of servers all the time. > > > > > > > I need to check this is because as i need to do Locking to achieve > > > > > atomicity and integrity, in that case i have wait for infinite > loop > > > > > till i get lock, so in that case i must know that status of server > or > > > > > my code will go in indefinite loop if memcache server goes down. > > > > > > > Please guide me. Waiting for a favourable reply. > > > > > > > Regards, > > > > > Sagar >
