<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div contenteditable="false"><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><div>Yes, sorry meant for the code below to use incr for the counter part I was aware of that :)</div><div><br class="webkit-block-placeholder"></div><div>Anyway to get around waiting for locks?&nbsp;</div><br class="Apple-interchange-newline"></span> </div><br><div><div>On Sep 7, 2007, at 10:10 AM, Timo Ewalds wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"> <div bgcolor="#ffffff" text="#000000"> I don't quite follow what you're doing there, but you can always use a different key as a lock to get rid of the race conditions. Just use an add operation on the lock key instead of set. If the add worked, you have the lock, if not, someone else does, and you can act appropriately (skip, wait, whatever). The other thing you may care to know, is there are incr and decr commands which are good for counters.<br> <br> Timo<br> <br> Chris Goffinet wrote: <blockquote cite="mid:B9FD8A29-BB6F-4835-BBC7-46E2632601CA@yahoo-inc.com" type="cite">I was wondering if anyone had any suggestions of trying to implement a binary search tree using memcache? I know it wouldn't be a good&nbsp;candidate&nbsp;if we had multiple memcache servers, but I was thinking it might be doable if we partition the tree on 1 memcache node.&nbsp;<br>  <br>  <div>Our biggest concern at the moment is race conditions of the tree updating. I thought a binary tree might solve the problem were having and that is the following scenario:</div>  <div><br class="webkit-block-placeholder">  </div>  <div>1) We have hundreds of memcache objects being created (imagine rows in a database of users visiting a site)</div>  <div>2) We need to have a running list of last 10 users for the hour, without duplication of a users record. So we implemented the idea of a counter:</div>  <div><br class="webkit-block-placeholder">  </div>  <div>&nbsp;&nbsp;if(!memcache-&gt;get("userid:$hourofday")</div>  <div>&nbsp;&nbsp;{</div>  <div>&nbsp;&nbsp; &nbsp;$counter = memcache-&gt;set("counter",1);</div>  <div>&nbsp;&nbsp; &nbsp;memcache-&gt;set("counter:$counter", payload);</div>  <div>&nbsp;&nbsp; &nbsp;memcache-&gt;set("userid:$hourofday",1);</div>  <div>&nbsp;&nbsp;}<br>  <div contenteditable="false"><br class="webkit-block-placeholder">  </div>  <div contenteditable="false"> <span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px;">This would create only 1 record per hour of day, increment the counter, so we could build the key list in our application to pull last 10 items for the hour. The issue we run into is what happens when the following scenario happens:</span></div>  <div contenteditable="false"><br class="webkit-block-placeholder">  </div>  <div contenteditable="false"><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px;">&nbsp;&nbsp;Hour 1 - &gt; User A - Counter 1</span></div>  <div contenteditable="false"><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px;">&nbsp;&nbsp;Hour 2 - &gt; User A - Counter 2</span></div>  <div contenteditable="false">&nbsp;&nbsp;Hour 3 - &gt; User A&nbsp;- Counter 3</div>  <div contenteditable="false">  <div contenteditable="false">&nbsp;&nbsp;Hour 4 - &gt; User A&nbsp;- Counter 4</div>  <div contenteditable="false">&nbsp;&nbsp;Hour 5 - &gt; User A&nbsp;- Counter 5</div>  <div contenteditable="false"><br class="webkit-block-placeholder">  </div>  </div>  <div contenteditable="false"><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px;">In this case we would only just want to show User A one time especially since its a list of user activity. We can't store a hash and just push/pop because of race conditions (our first thought). If anyone has any ideas, I'd really appreciate it!</span></div>  <div contenteditable="false"><br>  </div>  <div contenteditable="false"><br class="webkit-block-placeholder">  </div>  <div contenteditable="false"><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px;">  <div>-Chris</div>  <br class="Apple-interchange-newline">  </span> </div>  <br>  </div> </blockquote> </div> </blockquote></div><br></body></html>