public class SessionModule extends ConfigurableModule<SessionCookieConfig>
This module provides the general session API (see Session
), and a default SessionStore
implementation that stores session data in local memory.
It is expected that most applications will provide alternative bindings for the SessionStore
type, overriding the default.
This allows arbitrary stores to be used to persist session data.
The default, in memory, implementation stores the data in a Cache
<
AsciiString
, ByteBuf
>
.
This cache instance is provided by this module and defaults to storing a maximum of 1000 entries, discarding least recently used.
The memoryStore(java.util.function.Consumer<? super com.google.common.cache.CacheBuilder<io.netty.util.AsciiString, io.netty.buffer.ByteBuf>>)
methods are provided to conveniently construct alternative cache configurations, if necessary.
Objects must be serialized to be stored in the session.
The get/set methods SessionData
allow supplying a SessionSerializer
to be used for the specific value.
For variants of the get/set methods where a serializer is not provided, the implementation of SessionSerializer
bound with Guice will be used.
The default implementation provided by this module uses Java's in built serialization mechanism.
Users of this module may choose to override this binding with an alternative serialization strategy.
However, other Ratpack extensions may require session storage any rely on Java serialization.
For this reason, there is also always a JavaSessionSerializer
implementation available that is guaranteed to be able to serialize any Serializable
object (that conforms to the Serializable
contract.
Users of this module may also choose to override this binding with another implementation (e.g. one based on Kryo),
but this implementation must be able to serialize any object implementing Serializable
.
It is also often desirable to provide alternative implementations for SessionSerializer
and JavaSessionSerializer
.
The default binding for both types is an implementation that uses out-of-the-box Java serialization (which is neither fast nor efficient).
import ratpack.guice.Guice;
import ratpack.path.PathTokens;
import ratpack.session.Session;
import ratpack.session.SessionModule;
import ratpack.test.embed.EmbeddedApp;
import static junit.framework.Assert.assertEquals;
public class Example {
public static void main(String... args) throws Exception {
EmbeddedApp.of(a -> a
.registry(Guice.registry(b -> b
.module(SessionModule.class)
))
.handlers(c -> c
.get("set/:name/:value", ctx ->
ctx.get(Session.class).getData().then(sessionData -> {
PathTokens pathTokens = ctx.getPathTokens();
sessionData.set(pathTokens.get("name"), pathTokens.get("value"));
ctx.render("ok");
})
)
.get("get/:name", ctx -> {
ctx.get(Session.class).getData()
.map(d -> d.require(ctx.getPathTokens().get("name")))
.then(ctx::render);
})
)
).test(httpClient -> {
assertEquals("ok", httpClient.getText("set/foo/bar"));
assertEquals("bar", httpClient.getText("get/foo"));
assertEquals("ok", httpClient.getText("set/foo/baz"));
assertEquals("baz", httpClient.getText("get/foo"));
});
}
}
Modifier and Type | Field and Description |
---|---|
static Key<Cache<AsciiString,ByteBuf>> |
LOCAL_MEMORY_SESSION_CACHE_BINDING_KEY
The key of the binding for the
Cache implementation that backs the in memory session store. |
static String |
LOCAL_MEMORY_SESSION_CACHE_BINDING_NAME
The name of the binding for the
Cache implementation that backs the in memory session store. |
Constructor and Description |
---|
SessionModule() |
Modifier and Type | Method and Description |
---|---|
protected void |
configure() |
static void |
memoryStore(Binder binder,
Consumer<? super CacheBuilder<AsciiString,ByteBuf>> config)
A builder for an alternative cache for the default in memory store.
|
static Action<Binder> |
memoryStore(Consumer<? super CacheBuilder<AsciiString,ByteBuf>> config)
A builder for an alternative cache for the default in memory store.
|
configure, createConfig, defaultConfig, setConfig
addError, addError, addError, bind, bind, bind, bindConstant, binder, bindInterceptor, bindListener, bindListener, bindScope, configure, convertToTypes, currentStage, getMembersInjector, getMembersInjector, getProvider, getProvider, install, requestInjection, requestStaticInjection, requireBinding, requireBinding
public static final String LOCAL_MEMORY_SESSION_CACHE_BINDING_NAME
Cache
implementation that backs the in memory session store.memoryStore(Consumer)
,
Constant Field Valuespublic static final Key<Cache<AsciiString,ByteBuf>> LOCAL_MEMORY_SESSION_CACHE_BINDING_KEY
Cache
implementation that backs the in memory session store.memoryStore(Consumer)
public static Action<Binder> memoryStore(Consumer<? super CacheBuilder<AsciiString,ByteBuf>> config)
This method is intended to be used with the BindingsSpec.binder(Action)
method.
import ratpack.guice.Guice;
import ratpack.session.SessionModule;
public class Example {
public static void main(String... args) {
Guice.registry(b -> b
.binder(SessionModule.memoryStore(c -> c.maximumSize(100)))
);
}
}
config
- the cache configurationmemoryStore(Binder, Consumer)
public static void memoryStore(Binder binder, Consumer<? super CacheBuilder<AsciiString,ByteBuf>> config)
This method can be used from within a custom Module
.
import com.google.inject.AbstractModule;
import ratpack.session.SessionModule;
public class CustomSessionModule extends AbstractModule {
protected void configure() {
SessionModule.memoryStore(binder(), c -> c.maximumSize(100));
}
}
}
This method binds the built cache with the LOCAL_MEMORY_SESSION_CACHE_BINDING_KEY
key.
It also implicitly registers a RemovalListener
, that releases the byte buffers as they are discarded.
binder
- the guice binderconfig
- the cache configurationprotected void configure()
configure
in class AbstractModule