[id='programmatic-{context}']
= Configuring Caches Programmatically
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]
