Oh, I'm so bad.  I forgot to mention that I cheat a bit in the example I gave.  It doesn't demonstrate the multi_get.  Which you should use in a situation like this.  I dont use it this time because my GetArticle() function is able to cheat a bit because the peice of code that runs before this had already pulled down all the article information for what it needed to do, so a lot of it is already in a local hash array.  So it doesn't need to ask memcache for maybe 90% of the articles that will generate XML in this example.
<br><br>Sorry for any confusion there... especially since I started off talking about my solution for caching paged data, and then I gave you some example code for something completely different.&nbsp; Oh well.&nbsp; You get what you pay for, eh?
<br><br><div><span class="gmail_quote">On 9/21/07, <b class="gmail_sendername">Clint Webb</b> &lt;<a href="mailto:webb.clint@gmail.com">webb.clint@gmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Well, not really arrays in memory.&nbsp; Lists in memcache.&nbsp;&nbsp; It works well because 99% of requests will always be within the 100.&nbsp; That is why it exists as apposed to just having a 500,1000,1500, etc.<br><br>But lets say a user has browsed up to the 800th entry.&nbsp; When getting the key from memcache, it only gets the one (this one is called &quot;Articles:top:limit=1000&quot;).&nbsp; Which contains all the ID&#39;s of the current articles in the order that they would be displayed.&nbsp; I skip the first 800 entries in that list, and display the next 15.&nbsp; The actual contents of the article itself is in another cache entry (&quot;Article:aid=800&quot; for example).&nbsp; Depending on how I was accessing this information, I would produce either xml or html and would generate a key called &quot;xml:Article-list:800-814&quot; or &quot;html:Article-list:800-814&quot;).&nbsp; 
<br><br>This might not really be the most optimal solution but it worked well enough so far that I havent gone back to look at doing any optimisations.&nbsp; <br><br>One optimization I did think of doing..&nbsp; The list of ID&#39;s in the &quot;Articles:top:limit=1000&quot; is just a comma delimited string of ID&#39;s.&nbsp; I was planning on either storing the ID&#39;s in a fixed width spacing so that I could determine where in the string the 800th entry will be and just skip to that bit.&nbsp; Or I was thinking of storing a serialized array instead.&nbsp; But this piece of code has been quick enough so far that I haven&#39;t bothered.
<br><br>As a sample for ya&#39;s... in perl, if you have a comma delimited string, its fairly easy to drop that into an array.&nbsp; In fact, heres a peice of code that accesses part of it.&nbsp; The GetTopArticles(), GetArticle(), GetUsernameByUserID() and GetArticleScore() functions try and get the data from the cache, if not, it gets it from the database.&nbsp;&nbsp; This example doesn&#39;t illustrate the paging because this is for the public API that doesnt do paging.
<br><br>&nbsp;&nbsp;&nbsp; &amp;ConnectCache;<br>&nbsp;&nbsp;&nbsp; my ($xml) = $cache-&gt;get(&quot;xml:Top:$limit&quot;);<br>&nbsp;&nbsp;&nbsp; unless ($xml) {<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SiteLog(&quot;api:Articles - XML Top:$limit not found in cache. Adding.&quot;);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $xml = &quot;&lt;root&gt;\n&quot;;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $xml .= &quot;\t&lt;result&gt;1&lt;/result&gt;\n&quot;;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my ($list) = GetTopArticles($limit);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my (@top) = split(/,/, $list);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach $id (@top) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my ($article) = GetArticle($id);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unless ($article) { SiteLog(&quot;api.cgi: Wasnt able to get article details for aid=$id&quot;); }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else {<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my ($d_userID) = 0 + $article-&gt;{&#39;userID&#39;};<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my ($d_username) = GetUsernameByUserID($d_userID);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my ($v_score) = GetArticleScore($id);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $xml .= &quot;\t&lt;article&gt;\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $xml .= &quot;\t\t&lt;id&gt;$id&lt;/id&gt;\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $xml .= &quot;\t\t&lt;uid&gt;$d_userID&lt;/uid&gt;\n&quot;;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $xml .= &quot;\t\t&lt;username&gt;&quot;.xml_quote($d_username).&quot;&lt;/username&gt;\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $xml .= &quot;\t\t&lt;title&gt;&quot;.xml_quote($article-&gt;{&#39;title&#39;}).&quot;&lt;/title&gt;\n&quot;;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $xml .= &quot;\t\t&lt;content&gt;&quot;.xml_quote($article-&gt;{&#39;content&#39;}).&quot;&lt;/content&gt;\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $xml .= &quot;\t\t&lt;url&gt;&quot;.xml_quote($article-&gt;{&#39;url&#39;}).&quot;&lt;/url&gt;\n&quot;;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $xml .= &quot;\t\t&lt;score&gt;$v_score&lt;/score&gt;\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $xml .= &quot;\t&lt;/article&gt;\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $xml .= &quot;&lt;/root&gt;\n&quot;;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $cache-&gt;add(&quot;xml:Top:$limit&quot;, &quot;$xml&quot;, 600);<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &amp;cgiXML;<br>&nbsp;&nbsp;&nbsp; print &quot;$xml&quot;;<br><br><br><br><br><div><span class="q"><span class="gmail_quote">On 9/21/07, <b class="gmail_sendername">

K J</b> &lt;<a href="mailto:sanbat@gmail.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">sanbat@gmail.com</a>&gt; wrote:</span></span><div><span class="e" id="q_1152737a4235750c_2"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<span>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">Only you could answer that definitively, but I would guess that it would be better to get the lot.&nbsp; Depends how often your data changes.&nbsp;&nbsp; 
<br><br>On my site, people see the first 15 entries, but I put the first 100 in one cache key, and the first 500 in a second cache key if needed.&nbsp; I get the first 15 out of the hundred, and if they want more, I iterate though it until I need more than 100.&nbsp; On the rare occassion that anyone gets past the 500 mark I just go straight to the database, and then add back to the cache.&nbsp; 
<br><br>I&#39;ve split it up into 100 and 500 because most people would only ever look at less than the first 100 entries.&nbsp; if they do manage to look past the first 100, then I have the first 500 cached in another key.&nbsp; Keep in mind, this is not first 100 next 500 to make a total of 600 articles.&nbsp; The first 100 are also duplicated in the 500 list.&nbsp; The 500 entry list is generated only the first time it is needed, and the exact same routine also creates the 1000 entry key if that is ever needed, and so on.&nbsp; There is no built in limit, it could end up being a key for a 20000 entry list fall all I know. 
<br><br>Every situation is different.&nbsp; I suggest you build some test cases and test it under various situations and see what works for you.&nbsp; There are some parts of my site that dont use memcache at all and simply go to the database directly every time, but I did it that way because for that particular problem a cached solution would be clunky, and memcache just didnt fit well.&nbsp; But apart from those special cases, I cache almost everything.&nbsp; I cache the little bits of data (such as key for each IP address that hits the site, I increment a counter each time they hit, and give it an expiry), all the small elements of data, all the bigger elements made up of the smaller elements, all the rendered XML and some of the rendered HTML.&nbsp; My database is mostly idle :) 
</blockquote>
<div>&nbsp;</div></span>
<div>I&#39;m wondering about the 100, then the 500.&nbsp; Are you creating a new array at certain intervals? For instance suppose a user keeps paging through the results and end up at result 800.&nbsp; Would you then have&nbsp;3 arrays like this?
</div>
<div>- 100 array</div>
<div>- 500 array</div>
<div>- 1000 array</div>
<div><br>&nbsp;</div>
</blockquote></span></div></div><br><br clear="all"><div><span class="e" id="q_1152737a4235750c_4"><br>-- <br>&quot;Be excellent to each other&quot;
</span></div></blockquote></div><br><br clear="all"><br>-- <br>&quot;Be excellent to each other&quot;