session

session

These are the available modules for reading and saving session data in your application.

ehcache session store

dependency

<dependency>
  <groupId>org.jooby</groupId>
  <artifactId>jooby-ehcache</artifactId>
  <version>1.6.6</version>
</dependency>

usage

This module provides an EhSessionStore. In order to use the EhSessionStore all you have to do is define a session cache:


ehcache.cache.session {
  # cache will expire after 30 minutes of inactivity   timeToIdle = 30m
}

And then register the EhSessionStore:

{
  session(EhSessionStore.class);
}

guava session store

dependency

<dependency>
 <groupId>org.jooby</groupId>
 <artifactId>jooby-guava-cache</artifactId>
 <version>1.6.6</version>
</dependency>

usage

This module comes with a Session.Store implementation. In order to use it you need to define a cache named session in your application.conf file:

guava.session = "maximumSize=10"

And set the GuavaSessionStore:

import org.jooby.guava.GuavaCache;
import org.jooby.guava.GuavaSessionStore;

{
  use(GuavaCache.newCache());

  session(GuavaSessionStore.class);
}

You can access to the session via name:

{
  get("/", req -> {
    Cache cache = require("session", Cache.class);
  });
}

hazelcast session store

dependency

<dependency>
  <groupId>org.jooby</groupId>
  <artifactId>jooby-hazelcast</artifactId>
  <version>1.6.6</version>
</dependency>

usage

{
  use(new Hcast());

  session(HcastSessionStore.class);

  get("/", req -> {
   req.session().set("name", "jooby");
  });
}

options

timeout

By default, a Hazelcast session will expire after 30 minutes. Changing the default timeout is as simple as:

# 8 hours 
session.timeout = 8h

# 15 seconds 
session.timeout = 15

# 120 minutes 
session.timeout = 120m

# no timeout 
session.timeout = -1

name

Default session’s name is sessions. It’s possible to change the default name by setting the property: hazelcast.sesssion.name.

cassandra session store

A Session.Store powered by Cassandra.

dependency

<dependency>
 <groupId>org.jooby</groupId>
 <artifactId>jooby-cassandra</artifactId>
 <version>1.6.6</version>
</dependency>

usage

{
  use(new Cassandra("cassandra://localhost/db"));

  session(CassandraSessionStore.class);

  get("/", req -> {
    Session session = req.session();
    session.put("foo", "bar");
    ..
  });

}

Session data is persisted in Cassandra using a session table.

options

timeout

By default, a session will expire after 30 minutes. Changing the default timeout is as simple as:

# 8 hours
session.timeout = 8h
# 15 seconds
session.timeout = 15
# 120 minutes
session.timeout = 120m

Expiration is done via Cassandra ttl option.

If no timeout is required, use -1.

couchbase session store

A Session.Store powered by Couchbase.

dependency

<dependency>
 <groupId>org.jooby</groupId>
 <artifactId>jooby-couchbase</artifactId>
 <version>1.6.6</version>
</dependency>

usage

{
  use(new Couchbase("couchbase://localhost/bucket"));

  session(CouchbaseSessionStore.class);

  get("/", req -> {
    Session session = req.session();
    session.put("foo", "bar");
    ..
  });
}

Session data is persisted in Couchbase and document looks like:

{ "session::{SESSION_ID}": { "foo": "bar" } } 

options

timeout

By default, a session will expire after 30 minutes. Changing the default timeout is as simple as:

# 8 hours 
session.timeout = 8h

# 15 seconds 
session.timeout = 15

# 120 minutes 
session.timeout = 120m

Expiration is done via Couchbase expiry/ttl option.

If no timeout is required, use -1.

custom bucket

The session document are persisted in the application/default bucket, if you need/want a different bucket then use {@link Couchbase#sessionBucket(String)}, like:

{
  use(
      new Couchbase("couchbase://localhost/myapp")
          .sessionBucket("session")
  );

}

redis session store

dependency

<dependency>
  <groupId>org.jooby</groupId>
  <artifactId>jooby-jedis</artifactId>
  <version>1.6.6</version>
</dependency>

usage

{
  use(new Redis());

  session(RedisSessionStore.class);

  get("/", req -> {
   req.session().set("name", "jooby");
  });
}

The name attribute and value will be stored in a Redis. Sessions are persisted as hashes.

options

timeout

By default, a Redis session will expire after 30 minutes. Changing the default timeout is as simple as:


# 8 hours session.timeout = 8h

# 15 seconds 
session.timeout = 15

# 120 minutes 
session.timeout = 120m

# no timeout 
session.timeout = -1

key prefix

Default redis key prefix is sessions. Sessions in [redis] will look like: sessions:ID

It’s possible to change the default key setting the jedis.sesssion.prefix properties

mongodb session store

dependency

<dependency>
  <groupId>org.jooby</groupId>
  <artifactId>jooby-mongodb</artifactId>
  <version>1.6.6</version>
</dependency>

usage

{
  use(new Mongodb());

  session(MongoSessionStore.class);

  get("/", req -> {
   req.session().set("name", "jooby");
  });
}

The name attribute and value will be stored in a MongoDB.

options

timeout

By default, a mongodb session will expire after 30 minutes. Changing the default timeout is as simple as:


# 8 hours 
session.timeout = 8h

# 15 seconds 
session.timeout = 15

# 120 minutes 
session.timeout = 120m

# no timeout 
session.timeout = -1

It uses MongoDB’s TTL collection feature (2.2+) to have mongod automatically remove expired sessions.

session collection

Default MongoDB collection is sessions.

It’s possible to change the default key setting the mongodb.sesssion.collection properties.

neo4j session store

A Session.Store powered by Neo4j.

dependency

<dependency>
 <groupId>org.jooby</groupId>
 <artifactId>jooby-neo4j</artifactId>
 <version>1.6.6</version>
</dependency>

usage

{
  session(Neo4jSessionStore.class);

  get("/", req -> {

   req.session().set("name", "jooby");
  });

}

The name attribute and value will be stored in a Neo4j database.

options

timeout

By default, a neo4j session will expire after 30 minutes. Changing the default timeout is as simple as:

# 8 hours
session.timeout = 8h
# 15 seconds
session.timeout = 15
# 120 minutes
session.timeout = 120m

It uses GraphAware’s Expire library to automatically remove expired sessions.

For embedded databases you need to configure the expire module, like:

com.graphaware.runtime.enabled = true
com.graphaware.module = [{
  class: com.graphaware.neo4j.expire.ExpirationModuleBootstrapper
  nodeExpirationProperty: _expire
}]

The Neo4jSessionStore uses the _expire attribute to evict sessions.

If you connect to a remote server make sure the expire module was installed. More information at .

If no timeout is required, use -1.

session label

It’s possible to provide the session label using the neo4j.session.label property.

spymemcached session store

dependency

<dependency>
  <groupId>org.jooby</groupId>
  <artifactId>jooby-spymemcached</artifactId>
  <version>1.6.6</version>
</dependency>

usage

{
  use(new SpyMemcached());

  session(SpySessionStore.class);

  get("/", req -> {
   req.session().set("name", "jooby");
  });
}

The name attribute and value will be stored in Memcached.

Session are persisted using the default Transcoder.

options

timeout

By default, a Memcached session will expire after 30 minutes. Changing the default timeout is as simple as:


# 8 hours 
session.timeout = 8h

# 15 seconds 
session.timeout = 15

# 120 minutes 
session.timeout = 120m

# no timeout 
session.timeout = -1

key prefix

Default Memcached key prefix is sessions:. Sessions in Memcached will looks like: sessions:ID

It’s possible to change the default key setting the memcached.sesssion.prefix property.