Rails Translation Caching

For best performance, translations must be cached in your application.

TML library offers a few caching options:

Rails Cache

If you want to use the standard Rails caching mechanism, you can customize the cache settings it in your config/environments/production.rb file, like the following:

Open your config/environments/production.rb configuration:

Rails Cache with Memcache
config.cache_store = :mem_cache_store, {  
  :namespace => 'my_cache' 
}
Rails Cache with Memcache and multiple servers
config.cache_store = :mem_cache_store, '123.456.78.9:1001', '123.456.78.9:1002'  
Rails Cache with Memcache and Dalli
config.cache_store = :mem_cache_store, Dalli::Client.new('localhost:11211', {  
  :namespace => 'my_cache'
})
Rails Cache with Redis
config.cache_store = :redis_store, {  
  :host => 'localhost',
  :port => 6379
}

Finally, tell TML SDK that you are using the standard Rails cache adapter by adding the following configuration to the config/initializers/tml.rb initialize file:

Tml.configure do |config|  
  config.cache = {
    :enabled    => true,
    :adapter    => 'rails'
  }
end  

In all of the above examples, translations will be stored in the same cache as the rest of your application. TML also provides a way to use a separate cache adapter for storing translations only. The next few sections show how to configure a separate cache for the TML library.

Memcache

To provide a separate memcache server to store your translations, independently from your Rails cache, add the following configuration to the tml initializer:

Tml.configure do |config|  
  config.cache = {
    :enabled    => true,
    :adapter    => 'memcache',
    :host       => 'localhost:11211',
    :namespace  => 'translations'
 }
end  

Redis

Similarly, you can use Redis to persist your translations in cache:

Tml.configure do |config|  
  config.cache = {
    :enabled    => true,
    :adapter    => 'redis',
    :host       => 'localhost',
    :port       => 6379,
    :password   => 'password',
    :namespace  => 'translations'
  }
end  

The above examples use shared caching model. All your Rails processes on all your servers share the same translation cache. This approach will save you memory space, as well as allow you to invalidate/redeploy your translations cache without having to redeploy your application.

To update the cache, execute the following line of code:

Tml.cache.upgrade_version  

Or you can run the rake command from any of your app instances:

$ bundle exec rake tml:cache:shared:upgrade

This will invalidate your current cache and rebuilt it with the latest translations from Translation Exchange CDN. As each page loads, it will pull the latest cache data from the CDN and put it into your local shared cache.

If you would like to warmup your cache manually, you can run the following command:

$ bundle exec rake tml:cache:shared:warmup

This will immediately update your local cache with all of the translations data from the most recent release from the CDN.

Files

An alternative approach to shared cache is the static file cache. This approach requires downloading and installing the release inside your application and re-releasing your app with new translations.

The translation cache will be loaded and stored in every process on every server, but it will be faster at serving translations and this approach does not require cache warmup.

To specify in-memory, file-based cache, provide the following configuration:

Tml.configure do |config|  
  config.cache = {
    :enabled    => true,
    :adapter    => 'file',
    :path       => 'config/tml',
    :version    => 'current'
  }
end  

The file based cache must be downloaded and installed before you deploy your application using the following command:

$ bundle exec rake tml:cache:local:download

You can also rollback to the previous file cache using:

$ bundle exec rake tml:cache:local:rollback

Or even rollup to the more recently installed version using:

$ bundle exec rake tml:cache:local::rollup

Custom

If you would like to use a completely custom cache adapter that stores data in an external shared storage, you can do this by creating your own class implementing the following methods:

class Tml::CacheAdapters::MyAdapter < Tml::Cache

  def initialize
    # initialize your adapter
    # use Tml.config.cache to access cache configuration
  end

  def read_only?
    # indicates if the cache is read only
    false 
  end

  def fetch(key, opts = {})
     # fetches the element
     # if block is given and it is a miss, stores the element in the cache  
     # use data = yield to get the data from the block   
  end

  def store(key, data, opts = {})
     # stores data in the cache
  end

  def delete(key, opts = {})
     # deletes data from the cache
  end

  def exist?(key, opts = {})
     # checks if data exists in the cache
  end

  def clear(opts = {})
     # clears the cache
  end   

end

Make sure that the class is loaded in your application and specify the adapter in the config:

Tml.configure do |config|  
  config.cache = {
    :enabled    => true,
    :adapter    => 'my_adapter',
    :setting1   => 'value1',
    :setting2   => 'value2'
  }
end