Related Documentation : Integrations : Using Ehcache with JRuby and Rails : Using Ehcache from within Rails
Using Ehcache from within Rails
Configuration of Ehcache is still done with the ehcache.xml file, but for Rails applications you must place this file in the config directory of your Rails app. Also note that you must use JRuby to execute your Rails application, as these gems utilize Ruby's Java integration to call the Ehcache API. With this configuration out of the way, you can now use the Ehcache API directly from your Rails controllers and/or models. You could of course create a new Cache object everywhere you want to use it, but it is better to create a single instance and make it globally accessible by creating the Cache object in your Rails environment.rb file. For example, you could add the following lines to config/environment.rb:
require 'ehcache'
EHCACHE = Ehcache::CacheManager.new.cache
By doing so, you make the EHCACHE constant available to all Rails-managed objects in your application. Using the Ehcache API is now just like the above JRuby example. If you are using Rails 3 then you have a better option at your disposal: the built-in Rails 3 caching API. This API provides an abstraction layer for caching underneath which you can plug in any one of a number of caching providers. The most common provider to date has been the memcached provider, but now you can also use the Ehcache provider. Switching to the Ehcache provider requires only one line of code in your Rails environment file (e.g. development.rb or production.rb):
config.cache_store = :ehcache_store, {
:cache_name => 'rails_cache',
:ehcache_config => 'ehcache.xml'
}
This configuration will cause the Rails.cache API to use Ehcache as its cache store. The :cache_name and :ehcache_config are both optional parameters, the default values for which are shown in the above example. The value of the :ehcache_config parameter can be either an absolute path or a relative path, in which case it is interpreted relative to the Rails app’s config directory. A very simple example of the Rails caching API is as follows:
Rails.cache.write("answer", "42")
Rails.cache.read("answer") # => '42'
Using this API, your code can be agnostic about the underlying provider, or even switch providers based on the current environment (e.g., memcached in development mode, Ehcache in production). The write method also supports options in the form of a Hash passed as the final parameter.
See the "Supported Properties" table in Using the jruby-ehcache API directly for the options that are supported. These options are passed to the write method as Hash options using either camelCase or underscore notation, as in the following example:
Rails.cache.write('key', 'value', :time_to_idle => 60.seconds,
:timeToLive => 600.seconds) caches_action :index,
:expires_in => 60.seconds, :unless_exist => true
Turn on caching in your controllers
You can also configure Rails to use Ehcache for its automatic action caching and fragment caching, which is the most common method for caching at the controller level. To enable this, you must configure Rails to perform controller caching, and then set Ehcache as the provider in the same way as for the Rails cache API:
config.action_controller.perform_caching = true
config.action_controller.cache_store = :ehcache_store
Setting up a Rails Application with Ehcache
Here are the basic steps for configuring a Rails application to use Ehcache:
1. For this example, we will create a new Rails application with the custom template from JRuby.org. The following command creates a "rails-bigmemory" application:
jruby -S rails new rails-bigmemory -m http://jruby.org/rails3.rb
2. The example application will be a simple address book. Generate a scaffold for the address book application, which will create contacts including a first name, last name, and email address.
jruby -S rails generate scaffold Contact first_name: string last_name:
string email_address: string
3. Add support for caching with Ehcache. There are several ways to do this, but for this example, we will use the Action Controller caching mechanism. Open the ContactsController.rb. Add a call to the Action method to tell it to cache the results of our index and show pages.
caches_action :index, :show
To expire items from the cache as appropriate, add calls to expire the results of the caching calls.
Under create, add the following:
expire_action :action => 'index'
Under update, add the following:
expire_action :action => 'show', :id => params[:id]
expire_action :action => 'index'
Under destroy, add the following:
expire_action :action => 'index'
4. Now that the application is configured to support caching, specify Ehcache as its caching provider. Open the Gemfile and declare a dependency on the ehcache-jruby gem. Add the following line:
gem 'ehcache-jruby-rails3'
5. In the development.rb file, enable caching for the Rails Action Controller mechanism, which is disabled by default in development mode. (Note that caching must be configured for each environment in which you want to use it.) This file also needs a specification for using Ehcache as the cache store provider. Add the following two lines to the .rb file:
config.action_controller.perform_caching = true
config.cache_store = :ehcache_store
6. Run the Bundle Install command.
jruby -S bundle install
7. Run the Rake command to create the database and populate the initial schema.
jruby -S rake db:create db:migrate
Now you are ready to start the application with the following command:
jruby -S rails server
Once the application is started, populate the cache by adding, editing, and deleting contacts. To see the Contacts address book, enter the following in your browser:
http://localhost:3000/contacts
Copyright © 2010-2015 Software AG, Darmstadt, Germany.

Product Logo |   Feedback