Java Translation Caching

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

TML library offers a few caching options:

Memcache

To provide a separate memcache server to store your translations, independently, download Memcache adapter for java and put it on your test classpath. After this add the following configuration to the tml initializer:

Tml.getConfig().setCache(Utils.buildMap(  
    "enabled",  true,  
    "class",    "com.translationexchange.cache.Memcached",
    "host",     "localhost:11211",
    "namespace", "e9ecde2f25c1",
    "version_check_interval", 10
));

You can add the following dependency to your pom.xml:

<dependency>  
  <groupId>com.translationexchange</groupId>
  <artifactId>tml-memcached</artifactId>
  <version>0.2.3</version>
</dependency>  

Redis

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

Tml.getConfig().setCache(Utils.buildMap(  
    "enabled",  true,
    "class",    "com.translationexchange.cache.Redis",
    "host",     "localhost:6379"
));

You need to download Redis adapter and add it to your test classpath.

Or you can add the following dependency to your pom.xml:

<dependency>  
  <groupId>com.translationexchange</groupId>
  <artifactId>tml-redis</artifactId>
  <version>0.2.2</version>
</dependency>  

The above examples use shared caching model. All your Java 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.

EHCache

Also, yout can use EHCache to persist yout translations in cache:

Tml.getConfig().setCache(Utils.buildMap(  
  "enabled", true,
  "class",  "com.translationexchange.cache.EHCache"
));

You need to download EHCache adapter and add it to your test classpath.

Or you can add the following dependency to your pom.xml:

<dependency>  
  <groupId>com.translationexchange</groupId>
  <artifactId>tml-ehcache</artifactId>
  <version>0.2.2</version>
</dependency>  

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.getConfig().setCache(Utils.buildMap(  
    "enabled",  true,
    "class",    "com.translationexchange.cache.FileCache",
    "version",  "0123456789"
));

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:

public class CustomCache extends CacheAdapter implements Cache {

    public Object fetch(String key, Map<String, Object> options) {
        // TODO Auto-generated method stub
        return null;
    }

    public void store(String key, Object data, Map<String, Object> options) {
        // TODO Auto-generated method stub   
    }

    public void delete(String key, Map<String, Object> options) {
        // TODO Auto-generated method stub   
    }
}

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

Tml.getConfig().setCache(Utils.buildMap(  
    "enabled",    true,
    "class",      "CustomCache",
    "setting1",  "value1",
    "setting2",  "value2"
));