I'm adding an API to the API server. The API server was built by somebody else based on Sinatra framework. Basically it looks like:
class ApiMain < Sinatra::Base
get "/api/xx" {...}
get "/api/yy" {...}
end
Now I'm adding a new API, which returns a data that is loaded from db. Calls to my new API are expected to be very frequently, but the data is not frequently updated, to reduce workload of the db, I'm thinking to cache data in memory, and only load from db once a minute. Suppose there are 1000 calls to my API every minute, this way decreases 999 times of db access.
When I read Sinatra doc, I see "For every incoming request, a new instance of your application class is created, and all handler blocks run in that scope."
I'm a pure newbie of Sinatra, I wonder if it's possible to cache data in global variable that can be shared with all request scopes? If yes, how to implement the global variable? A short sample will be greatly appreciated.
data.to_json
so it becomes a string. But I understand that this is definitely not the best solution.