版本 3

    Infinispan的编程式配置主要是通过CacheManager API来完成的。 尽管我们可以通过编程方式来设置Infinispan的各个方面,但是更常用的方式是在以XML配置文件的形式作为起点,然后在运行时,如果需要的话再通过编程方式对配置进行调整以最大程度的满足我们的需要。

    EmbeddedCacheManager manager = new DefaultCacheManager("my-config-file.xml");
    Cache defaultCache = manager.getCache();

     

    假设我们想要编程方式的定义一个同步复制的缓存。我们先创建一个Configuration对象的新实例然后将缓存模式设置为同步复制。最后,我们将会通过Manager对其进行注册或者定义。如果指定的以”repl“命名的缓存不存在的话,将会按照默认缓存的配置进行克隆然后通过我们新创建的配置的克隆对其进行重写。

     

    Configuration c = new Configuration();
    c.setCacheMode(CacheMode.REPL_SYNC);
         
    String newCacheName = "repl";
    manager.defineConfiguration(newCacheName, c);
    Cache<String, String> cache = manager.getCache(newCacheName);

     

    开始的时候,我们也可以使用默认缓存配置 (或者其他缓存配置) 来创建新缓存。例如, 假设 my-config-file.xml 中默认情况下是复制缓存,然而我们希望创建一个并且具有特定的L1生命周期但是其他方面与my-config-file.xml.默认缓存配置保持一致的分布式缓存。因此,我们可以在开始时获取一个默认Configuration 对象的实例,然后设置实例对应的缓存模式和L1生命周期.。最后,我们将通过Manager进行注册或者定义 。

     

    EmbeddedCacheManager manager = new DefaultCacheManager("my-config-file.xml");
    Configuration c = manager.getDefaultConfiguration().clone();
    c.setCacheMode(CacheMode.DIST_SYNC);
    c.setL1Lifespan(60000L);
         
    String newCacheName = "distributedWithL1";
    manager.defineConfiguration(newCacheName, c);
    Cache<String, String> cache = manager.getCache(newCacheName);

     

    更多信息请参照 CacheManagerConfigurationGlobalConfiguration

    Configuration