Avoid API calls for timelines by using cached statuses


#1

Hello.

I read somewhere on the API documentation that caching tweets is recommended(I’d do it anyways). I am currently in the following situation:
(1) The user is at this home timeline
(2) There are currently only the newest 20 tweets displayed
(3) He scrolls down, when he reaches the end of the timeline, new tweets are fetched from the Twitter server

At (3) I’d like to know whether I actually have to download anything at all - since I’m caching tweets anyway, they could already be on the hard disk so I’d just have to load them from there and display them.
Since I have 64bit integers I’m already using the most “efficient” version from [1], but I’d like to give the user a seamless expierence by just displaying already cached data.
Is that possible? If yes, how? If not, is anything in that direction planned?(E.g. a “next_status_id”/“prev_status_id” parameter for home_timeline that would give developers the possibility to check wheter the next/previous tweets is cached by their application).

I hope my qustion was clear enough, thanks in advance.


#2

You can already pass a since_id and max_id paramater in your API call; since_id will return tweets > than the ID you pass in (newer tweets), max_id will return tweets <= the ID you pass in (older tweets).

Say you have 50 tweets cached; when they load page 2, you can try and load 20 from your cache, and as you’ve got 50 cached you’ll be able to load 20 more, so you’re now showing 40. Then for page 3, you’ll try to load the next 20, but there’s only 10 cached tweets left, so then you would work out how many tweets short of 20 you are, in this case 10, then make an API request to load 10 tweets, and set max_id to be one less than the last tweet in your cache, and you’ll get the next 10 from the API to complete the 20 for page 3, which you can then cache.

Make sense? Hope that helps.


#3

Ah ok wait.
I could basically just display the newest 20 tweets. Then, when the user scrolls down, I simply use the ID of the oldest of those 20 tweets(let’s just assume they are totally ordered by their ID), say $lowest_id. Then I retrieve the tweets with the highest ID < $lowest_id, say $next_id, from my cache and still call the api with max_id=$lowest_id and since_id=$next_id. If that call returns 0 tweets, my cache is complete, else I just insert the retrieved tweets in between… right?
I think that makes sense, I’ll just gonna try that right away, if not I’ll probably be sad 20 minutes from now.
Anyway, thanks for pushing me into the right direction.