If you want the cache to expire after a certain time, you probably want to use Memcached. Memcached lets you specify the expiration time of your cache. After you configure Rails to use Memcached as the default cache solution, you can do the following:
def following Rails.cache.fetch("following/#{id}", :expires_in => 1.hour) do result = twitter_request_authenticated('get_following') parse_body(result) end end
It will cache the following list in memory for 1 hour, and the cache will be unique to each user. You could manually expire this cache at any time with this command:
Rails.cache.delete("following/#{id}")
To get started with Ruby on Rails caching, check out this guide.
PS: I think your approach to modeling each user that matches current_user is great. If you want to complete the full set of actions for these users, it might be worth creating a separate model for TwitterUser, but I would not combine User and TwitterUser, because they are probably completely different. For example, a random TwitterUser might not have authorization on your system and probably won't be able to log in just because a user on your system is following it.
Pan thomakos
source share