[id='programmatic-{context}']
= Programmatic Configuration
Create new Configuration objects with the `ConfigurationBuilder` class and then
define cache configurations with the Cache Manager.

[NOTE]
====
The examples in this section use `EmbeddedCacheManager`, which is a Cache
Manager that runs in the same JVM as the client.

To configure caches remotely with HotRod clients, you use `RemoteCacheManager`.
Refer to the HotRod documentation for more information.
====

.Configure new cache instances

The following example configures a new cache instance:

[source,java]
----
EmbeddedCacheManager manager = new DefaultCacheManager("infinispan-prod.xml");
Cache defaultCache = manager.getCache();
Configuration c = new ConfigurationBuilder().clustering() <1>
  .cacheMode(CacheMode.REPL_SYNC) <2>
  .build();

String newCacheName = "replicatedCache";
manager.defineConfiguration(newCacheName, c); <3>
Cache<String, String> cache = manager.getCache(newCacheName);
----

<1> creates a new Configuration object.
<2> specifies distributed, synchronous cache mode.
<3> defines a new cache named "replicatedCache" with the Configuration object.

.Create new caches from existing configurations

The following examples create new cache configurations from existing ones:

[source,java,options="nowrap"]
----
EmbeddedCacheManager manager = new DefaultCacheManager("infinispan-prod.xml");
Configuration dcc = manager.getDefaultCacheConfiguration(); <1>
Configuration c = new ConfigurationBuilder().read(dcc) <2>
  .clustering()
  .cacheMode(CacheMode.DIST_SYNC) <3>
  .l1()
  .lifespan(60000L) <4>
  .build();
 
String newCacheName = "distributedWithL1";
manager.defineConfiguration(newCacheName, c); <5>
Cache<String, String> cache = manager.getCache(newCacheName);
----

<1> returns the default cache configuration from the Cache Manager. In this example, `infinispan-prod.xml` defines a replicated cache as the default.
<2> creates a new Configuration object that uses the default cache configuration as a base.
<3> specifies distributed, synchronous cache mode.
<4> adds an L1 lifespan configuration.
<5> defines a new cache named "distributedWithL1" with the Configuration object.

[source,java,options="nowrap"]
----
EmbeddedCacheManager manager = new DefaultCacheManager("infinispan-prod.xml");
Configuration rc = manager.getCacheConfiguration("replicatedCache"); <1>
Configuration c = new ConfigurationBuilder().read(rc)
  .clustering()
  .cacheMode(CacheMode.DIST_SYNC)
  .l1()
  .lifespan(60000L)
  .build();
 
String newCacheName = "distributedWithL1";
manager.defineConfiguration(newCacheName, c);
Cache<String, String> cache = manager.getCache(newCacheName);
----

<1> uses a cache configuration named "replicatedCache" as a base.

.Reference

* link:{javadocroot}/org/infinispan/manager/package-summary.html[CacheManager package summary]
* link:{javadocroot}/org/infinispan/configuration/cache/ConfigurationBuilder.html[org.infinispan.configuration.cache.ConfigurationBuilder]
* link:{javadocroot}/org/infinispan/manager/EmbeddedCacheManager.html[org.infinispan.manager.EmbeddedCacheManager]

* link:{hotrod_docs}[HotRod Java Client Guide]
* link:{javadocroot}/org/infinispan/client/hotrod/configuration/ConfigurationBuilder.html[org.infinispan.client.hotrod.configuration.ConfigurationBuilder]
* link:{javadocroot}org/infinispan/client/hotrod/RemoteCacheManager.html[org.infinispan.client.hotrod.RemoteCacheManager]
