1

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.

5
  • Maybe redis will do the work? Nov 24, 2015 at 10:06
  • Yeah, eventually should introduce redis or memcached. But for now, we don't want to add more component.
    – TieDad
    Nov 24, 2015 at 10:09
  • And what about environment variables?) Nov 24, 2015 at 10:12
  • I guess env var doesn't work, because the data is a hash table.
    – TieDad
    Nov 24, 2015 at 10:14
  • But you may use data.to_json so it becomes a string. But I understand that this is definitely not the best solution. Nov 24, 2015 at 10:17

1 Answer 1

2

One of the choices is to use set method, which will store data in a class variable.

require 'sinatra/base'

class App < Sinatra::Base
  set :cache, {}

  get "/?:word?" do
    word = params[:word]
    App.cache[word] ||= 0
    App.cache[word] += 1
    count = App.cache[word]
    "`#{word}` was called #{count} times"
  end
end

You should keep in mind that this approach has got many gotchas. You should care of synchronization this data with its original source. You should care not to change it accidentally as far as Ruby Hash could be mutated. You will have a copy of this data for each process, etc.

5
  • A few style notes: You're mixing classic and modular styles (sinatrarb.com/…), and this example won't work as-is without a rackup script. set should be called from within a configure block. The cache should be accessed with settings.cache instead of App.cache. Also, if this is going to run under a multithreaded web server like Puma, I would recommend using a thread-safe Concurrent::Hash. Here's a working example: gist.github.com/mwpastore/dc8f39cdadac4a6e32ad
    – mwp
    Nov 25, 2015 at 22:46
  • @mwp, thank you for feedback. I am not mixing styles, It is actually modular style :). I didn't put rackup file as far as it is not the point (and I don't like run! if app_file == $0). configure and settings are just a sugar for yield self and self, but on my taste this cache is not something about configuration at all. So I prefer to use idiomatic Sinatra setter set but not in configure block because I feel it semantically more correct.
    – fl00r
    Nov 25, 2015 at 22:57
  • sorry for being pedantic here, but I still have some concerns. For modular style you should require 'sinatra/base' and wrap your app in a class. For classic style you should require 'sinatra' and keep your app inline. The app doesn't respond to any requests as you have it written now, unless you run App from a separate rackup script.
    – mwp
    Nov 25, 2015 at 23:01
  • @mwp, It is my old habit to require whole sinatra namespace instead of sinatra/base. I think it came from ancients times when there was no sinatra/base at all :). (It still works fine with sinatra because it requires sinatra/base internally)
    – fl00r
    Nov 25, 2015 at 23:04
  • @mwp github.com/sinatra/sinatra/blob/master/lib/sinatra/main.rb#L1, but yes, I should be more explicit. Thank you.
    – fl00r
    Nov 25, 2015 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.