<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">
<br><div><div>On Sep 7, 2007, at 15:11 , Chris Goffinet wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; 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; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0; "><div>There is a possibility that a site could have no traffic for hours, and a user visits it each hour, so the list could have a user multiple times (spread by hour) since were only limiting it by the hour and we do want it to be unique. So what I am wondering is, how could we potentially get around this issue by avoiding locks? We thought about just storing an array for a site of last 10 users, then using array push/pop methods on it and use a lock method. But wouldn't a lock potentially delay the script executing until the lock is available (slowing down the execution time) ?</div></span></blockquote><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>It's worse in the case where there's no blocking lock.<br class="webkit-block-placeholder"></div><div><br class="webkit-block-placeholder"></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>There've been talks about having ADTs on the server-side to help deal with some of these issues.  What you really want here is a fixed-size set of sorts.  That'd be neat.<br class="webkit-block-placeholder"></div><div><br class="webkit-block-placeholder"></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>Here's a way you might be able to emulate it, but I'm not sure how memcached would feel about this kind of thrashing:<br class="webkit-block-placeholder"></div><div><br class="webkit-block-placeholder"></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>Create some arbitrary prefix:   last_visitor_<br class="webkit-block-placeholder"></div><div><br class="webkit-block-placeholder"></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>Create a counter:  last_visitor_counter<br class="webkit-block-placeholder"></div><div><br class="webkit-block-placeholder"></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>On a given request you'd define as a ``visit,'' you can increment the counter and set ``last_visitor_[n]'' to the user's id.  Your last visitors would be the unique IDs from the the last few found here.</div><div><br class="webkit-block-placeholder"></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>That's a sloppy,  lock-free way.  If you don't mind an occasional lock, you can first do a get, grab the latest n values, see if you're in them.  If you're in them, you're done.  If you're not, you can then acquire a lock, grab the values again, rewrite them however you want to make sure you're in the set, and then release the lock.<br class="webkit-block-placeholder"></div><div><br class="webkit-block-placeholder"></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>A failure to grab the lock is expensive (because it's non-blocking and you have to poll), but you can make it less frequent by storing more data.<br class="webkit-block-placeholder"></div><div><br class="webkit-block-placeholder"></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>A nice alternative would be a way to CAS a value.  The ``replace'' semantics could *almost* provide such a thing, but there'd need to be more information returned from a ``get'' and more that could be supplied to a ``cas'' to make this work.  You wouldn't want to transfer two different values, but it'd be awfully handy to provide something to summarize the value reliably.<br></div><br><div> <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>-- </div><div>Dustin Sallings</div><br class="Apple-interchange-newline"></span> </div><br></body></html>