=  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
[source,xml,options="nowrap",subs=attributes+]
----
include::dependencies_maven/ispn_embedded.xml[]
----

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

----
include::code_examples/Quickstart.java[]
----

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
----
include::code_examples/QuickstartDefaultCache.java[]
----

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

[source,java,options=nowrap]
.DefaultCacheQuickstart.java
----
include::code_examples/QuickstartDefaultCacheThreadSafe.java[]
----

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

[source,java,options=nowrap]
.DefaultCacheQuickstart.java
----
include::code_examples/QuickstartDefaultCacheLifespan.java[]
----

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
----
include::code_examples/QuickstartCustomCache.java[]
----

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"
----

.infinispan.xml
[source,xml,options="nowrap",subs=attributes+]
----
include::config_examples/infinispan_custom_cache.xml[]
----

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

.XmlConfiguredCacheQuickstart.java
[source,java,options=nowrap]
----
include::code_examples/ConfigLoadCustomCache.java[]
----

To run using maven:

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