==  Using {brandname} as an embedded cache in Java SE
Running {brandname} in embedded mode is very easy. First, we'll set up a project, and then we'll run {brandname}, and start adding data.

.embedded-cache quickstart
TIP: All the code discussed in this tutorial is available in the link:https://github.com/infinispan/infinispan-quickstart/tree/master/embedded-cache[embedded-cache quickstart].

=== Creating a new {brandname} project
The only thing you need to set up {brandname} is add it's dependencies to your project.

==== Maven users
If you are using Maven (or another build system like Gradle or Ivy which can use Maven dependencies), then this is easy. Just add the following to the `<dependencies>` section of your `pom.xml`:

.pom.xml
include::topics/dependencies/ispn_embedded.adoc[]

ifndef::productized[]
.Which version of {brandname} should I use?
TIP: We recommend using the latest stable version of {brandname}. All releases are displayed on the link:http://www.infinispan.org/download[downloads page].

Alternatively, you can link:https://raw.github.com/infinispan/infinispan-quickstart/master/embedded-cache/pom.xml[use the POM] from the quickstart that accompanies this tutorial.
endif::productized[]

==== Ant users
If you are using Ant, or another build system which doesn't provide declarative dependency management, then the {brandname} distribution zip contains a lib/ directory. Add the contents of this to the build classpath.

=== Running {brandname} on a single node
In order to run {brandname}, we're going to create a `main()` method in the `Quickstart` class.
{brandname} comes configured to run out of the box; once you have set up your dependencies, all you need to do to start using {brandname} is to create a new cache manager and get a handle on the default cache.

[source,java,options=nowrap]
.Quickstart.java

----
public class Quickstart {

   public static void main(String args[]) throws Exception {
      Cache<Object, Object> c = new DefaultCacheManager().getCache();
   }

}
----

We now need a way to run the main method! To run the Quickstart main class:
If you are using Maven:

[source,bash,options=nowrap]
----
$ mvn compile exec:java -Dexec.mainClass="org.infinispan.quickstart.embeddedcache.Quickstart"
----

You should see {brandname} start up, and the version in use logged to the console.

Congratulations, you now have {brandname} running as a local cache!


=== Use the default cache
{brandname} exposes a Map-like, JSR-107-esque interface for accessing and mutating the data stored in the cache. For example:

[source,java,options=nowrap]
.DefaultCacheQuickstart.java
----
// Add a entry
cache.put("key", "value");
// Validate the entry is now in the cache
assertEqual(1, cache.size());
assertTrue(cache.containsKey("key"));
// Remove the entry from the cache
Object v = cache.remove("key");
// Validate the entry is no longer in the cache
assertEqual("value", v);
----

{brandname} offers a thread-safe data-structure:

[source,java,options=nowrap]
.DefaultCacheQuickstart.java
----
// Add an entry with the key "key"
cache.put("key", "value");
// And replace it if missing
cache.putIfAbsent("key", "newValue");
// Validate that the new value was not added
----

By default entries are immortal but you can override this on a per-key basis and provide lifespans.

[source,java,options=nowrap]
.DefaultCacheQuickstart.java
----
//By default entries are immortal but we can override this on a per-key basis and provide lifespans.
cache.put("key", "value", 5, SECONDS);
assertTrue(cache.containsKey("key"));
Thread.sleep(10000);
----

to run using maven:

[source,bash,options=nowrap]
----
$ mvn compile exec:java -Dexec.mainClass="org.infinispan.quickstart.embeddedcache.DefaultCacheQuickstart"
----

=== Use a custom cache
Each cache in {brandname} can offer a different set of features (for example transaction support, different replication modes or support for eviction), and you may want to use different caches for different classes of data in your application. To get a custom cache, you need to register it with the manager first:

[source,java,options=nowrap]
.CustomCacheQuickstart.java
----
public static void main(String args[]) throws Exception {
   EmbeddedCacheManager manager = new DefaultCacheManager();
   manager.defineConfiguration("custom-cache", new ConfigurationBuilder()
             .eviction().strategy(LIRS).maxEntries(10)
             .build());
   Cache<Object, Object> c = manager.getCache("custom-cache");
}
----

The example above uses {brandname}'s fluent configuration, which offers the ability to configure your cache programmatically. However, should you prefer to use XML, then you may. We can create an identical cache to the one created with a programmatic configuration:

To run using maven:

[source,bash,options=nowrap]
----
$ mvn compile exec:java -Dexec.mainClass="org.infinispan.quickstart.embeddedcache.CustomCacheQuickstart"
----

[source,xml,subs=attributes+]
.infinispan.xml
----
<infinispan
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="urn:infinispan:config:{infinispanversion} http://www.infinispan.org/schemas/infinispan-config-{infinispanversion}.xsd"
        xmlns="urn:infinispan:config:{infinispanversion}">

   <cache-container default-cache="default">
       <local-cache name="xml-configured-cache">
          <eviction strategy="LIRS" max-entries="10" />
       </local-cache>
   </cache-container>

</infinispan>
----

We then need to load the configuration file, and use the programmatically defined cache:

[source,java,options=nowrap]
.XmlConfiguredCacheQuickstart.java
----
public static void main(String args[]) throws Exception {
	Cache<Object, Object> c = new DefaultCacheManager("infinispan.xml").getCache("xml-configured-cache");
}
----

To run using maven:

[source,bash,options=nowrap]
----
$ mvn compile exec:java -Dexec.mainClass="org.infinispan.quickstart.embeddedcache.XmlConfiguredCacheQuickstart"
----
