Thanks for your feedback Dormando.
The idea with 4) is that I'd cache an array of key value pairs, where
the key is the item's id and the value is the first few characters of
the item's name. So if the item's name is "Baseball Game", the value
might be "bas", "baseba", or even "baseball gam". (I guess I'd have to
test for optimal length to balance not having too many items with the
same value while keeping the cache value as small as possible.)
In that case, I could do a rough sort in memory based on the truncated
values. Now suppose my user interface lets users page through values
20 at a time. For page one, I'd need to make sure the values for the
20th and 21st item in the array aren't equal. If they are, I'd need
to include item 21's id (and 22, 23, etc. if they also have the same
value) in my multi-get for the detail data (which I'd lookup by id)
and use the detailed data for final sorting before deciding which 20
items should be displayed.
Similar code would be used when inserting items in the array... if the
first few letters of the item don't match something already in the
array, I can add the item id and value without any extra work.
Otherwise, I'd have to multi-get any items with the same first few
letters to decide where to add the item in the array. Again, I could
balance how often the extra step is necessary by varying the length of
the values in the cache.
Obviously the code is more complex here, but as long as there aren't
too many items with the same cached values, it might (or might not) be
more efficient than deleting and re-caching the entire array from SQL
every time an item is added or removed.
Anyone else tackled this before?