==  Using Infinispan as an embedded cache in Java SE
Running Infinispan in embedded mode is very easy. First, we'll set up a project, and then we'll run Infinispan, 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 Infinispan project
The only thing you need to set up Infinispan 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:

[source,xml]
.pom.xml

----

<dependency>
	<groupId>org.infinispan</groupId>
	<artifactId>infinispan-embedded</artifactId>
	<version>${infinispan.version}</version>
</dependency>

----

to the `<dependencies>` section of the POM.
You'll need to substitute `${infinispan.version}` for the version of Infinispan you wish to use. 

.Which version of Infinispan should I use?
TIP: We recommend using the latest stable version of Infinispan. 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. 

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

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

[source,java]
.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:

 $ mvn compile exec:java -Dexec.mainClass="org.infinispan.quickstart.embeddedcache.Quickstart"

You should see Infinispan start up, and the version in use logged to the console.

Congratulations, you now have Infinispan running as a local cache!


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

[source,java]
.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);
----

Infinispan offers a thread-safe data-structure:

[source,java]
.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]
.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:

 $ mvn compile exec:java -Dexec.mainClass="org.infinispan.quickstart.embeddedcache.DefaultCacheQuickstart"

=== Use a custom cache
Each cache in Infinispan 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]
.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 Infinispan'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:

$ mvn compile exec:java -Dexec.mainClass="org.infinispan.quickstart.embeddedcache.CustomCacheQuickstart"


[source,xml]
.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]
.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:

$ mvn compile exec:java -Dexec.mainClass="org.infinispan.quickstart.embeddedcache.XmlConfiguredCacheQuickstart"
