Caching strategy when using API's that have a limit on number of calls allowed?
I am making an app using twitter API that needs to use the twitter REST API to find the followers of a user frequently . But twitter has a restriction of 350 API requests in an hour . My App in its current state is sure to exceed tha开发者_如何学Ct. Please tell me what kind of caching strategy should I employ to reduce the number of API calls I make and thereby improve speed of my app and the follow twitter policies without any problem.
Abstract your access to Twitter API and do something along these lines.
If last call to Twitter at least 12 seconds ago
Make new call to Twitter and store returned info
Set Timestamp
else
Return last stored data
endif
This means that only one part of your program needs to know about the restriction and all other parts can treat the data as having come fresh from Twitter.
In the light of your comment, the above pseudo-code becomes
If last call to Twitter at least 12 seconds ago
Make new call to Twitter and save follower list in DB
Set Timestamp
endif
Return follower list from DB
I would be inclined to have this sort of structure in one table, at least at first.
twitter_id
.
.
whatever else you want to store about the person
.
.
followers VARCHAR space-separated list of follower IDs
Obviously, this would be a simplistic approach, but on the basis of 'the simplest thing that works' it would be fine.
精彩评论