---
---
= Examples
ifndef::sourcedir39[]
include::{includedir}/common.adoc[]
endif::sourcedir39[]

ifdef::notBuildingForSite[]
include::{includedir}/menu.adoc[]
endif::notBuildingForSite[]

== Peeper - a simple message board

The `demo` directory in the Ehcache 3 sources includes a sample applications with two (2)
implementations demonstrating Ehcache use.  Implemented as a simple
browser-based web service, the sample application Peeper displays any messages (_peeps_)
previously entered and accepts new peeps, recording the peeps in a database.  The peeps
database, shared among implementations of the Peeper application, is located at
+$HOME/ehcache-demo-peeper.mv.db+.  This file may be safely erased while the application
is not running.  While running, information about the operation of the Peeper application
(database access, cache access, etc.) is written to the console.

While the sample application may be run, the application is _very_ simplistic -- the code
implementing the sample is the interesting bit. Running the sample application requires the
use of https://gradle.org/[Gradle]. This sample may be accessed from GitHub by _cloning_
the Ehcache 3 git repository:

[source,bash]
----
# Create and/or change to a directory to hold the Ehcache git repository clone
git clone https://github.com/ehcache/ehcache3.git
----

=== Peeper without Caching -- +00-NoCache+

The first sample, located in +demos/00-NoCache+, is a base Peeper application that
does *not* use caching.  Each peep is stored in the database and all peeps are read
from the database to display the Peeper web page. To run this implementation:

[source,bash]
----
cd ehcache3/demos/00-NoCache
../../gradlew appStart
----

This builds the necessary components, starts a http://eclipse.org/jetty/[Jetty] web service,
and displays the URL of the web server on the console. The URL should be +http://localhost:8080/+.

While running, lines like the following are displayed to the console:
[source]
----
11:23:53.536 [800523121@qtp-1157162760-2] INFO  o.e.d.p.DataStore - Loading peeps from DB
11:24:03.226 [800523121@qtp-1157162760-2] INFO  o.e.d.p.DataStore - Adding peep into DB
11:24:03.234 [800523121@qtp-1157162760-2] INFO  o.e.d.p.DataStore - Loading peeps from DB
11:24:13.312 [800523121@qtp-1157162760-2] INFO  o.e.d.p.DataStore - Adding peep into DB
11:24:13.317 [800523121@qtp-1157162760-2] INFO  o.e.d.p.DataStore - Loading peeps from DB
11:24:41.238 [800523121@qtp-1157162760-2] INFO  o.e.d.p.DataStore - Loading peeps from DB
11:24:50.896 [800523121@qtp-1157162760-2] INFO  o.e.d.p.DataStore - Adding peep into DB
11:24:50.901 [800523121@qtp-1157162760-2] INFO  o.e.d.p.DataStore - Loading peeps from DB
11:24:56.295 [800523121@qtp-1157162760-2] INFO  o.e.d.p.DataStore - Adding peep into DB
11:24:56.298 [800523121@qtp-1157162760-2] INFO  o.e.d.p.DataStore - Loading peeps from DB
----
Note the absence of indications of interactions with a cache.

=== Peeper with Cache-aside Caching -- +01-CacheAside+

The second sample, located in +demos/01-CacheAside+, is a version of the Peeper application
that makes use of Ehcache.  As each peep is read from the database (for display in the web
page), it is written to an Ehcache instance.  If the Peeper web page is refreshed
(without adding a new peep) or a new Peeper client connects, the peeps are read from
the cache (instead of the database) to form the web page.  If a new peep is posted,
the cache is cleared.  To run this implementation:

[source,bash]
----
cd ehcache3/demos/01-CacheAside
../../gradlew appStart
----

This builds the necessary components, starts a http://eclipse.org/jetty/[Jetty] web service,
and displays the URL of the web server on the console. The URL, again, should be +http://localhost:8080/+.

While running, lines like the following are displayed to the console:
[source]
----
11:26:20.557 [139688380@qtp-965028604-0] INFO  o.e.d.p.DataStore - Loading peeps from DB
11:26:20.572 [139688380@qtp-965028604-0] INFO  o.e.d.p.DataStore - Filling cache with peeps
11:26:33.422 [139688380@qtp-965028604-0] INFO  o.e.d.p.DataStore - Adding peep into DB
11:26:33.428 [139688380@qtp-965028604-0] INFO  o.e.d.p.DataStore - Clearing peeps cache
11:26:33.431 [139688380@qtp-965028604-0] INFO  o.e.d.p.DataStore - Loading peeps from DB
11:26:33.432 [139688380@qtp-965028604-0] INFO  o.e.d.p.DataStore - Filling cache with peeps
11:26:50.025 [139688380@qtp-965028604-0] INFO  o.e.d.p.DataStore - Adding peep into DB
11:26:50.027 [139688380@qtp-965028604-0] INFO  o.e.d.p.DataStore - Clearing peeps cache
11:26:50.030 [139688380@qtp-965028604-0] INFO  o.e.d.p.DataStore - Loading peeps from DB
11:26:50.031 [139688380@qtp-965028604-0] INFO  o.e.d.p.DataStore - Filling cache with peeps
11:27:10.742 [139688380@qtp-965028604-0] INFO  o.e.d.p.DataStore - Getting peeps from cache
----
Note the presence of the +Filling cache with peeps+, +Clearing peeps cache+, and +Getting peeps from cache+ lines indicating cache interactions.

[[xml-with-107-extension]]
== XML with 107 extension

[source,xml,indent=0]
----
include::{sourcedir39}/107/src/test/resources/ehcache-example.xml[]
----
