me.geso.avans.session.spymemcached.SpyMemcachedSessionStore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of avans-session-spymemcached Show documentation
Show all versions of avans-session-spymemcached Show documentation
spymemcached as session storage
package me.geso.avans.session.spymemcached;
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import lombok.NonNull;
import me.geso.avans.session.WebSessionStore;
import net.spy.memcached.MemcachedClient;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
public class SpyMemcachedSessionStore implements WebSessionStore {
private final MemcachedClient memcachedClient;
private final int expirationTime;
private static final ObjectMapper defaultObjectMapper = new ObjectMapper(
new CBORFactory());
private final ObjectMapper objectMapper;
/**
* Create new intance.
*
* @param memcachedClient
* Spymemcached instance.
* @param objectMapper
* Jackson ObjectMapper instance. It's an
* serializer/deserializer.
* @param expirationTime
* Is a default expiration time for the session data.
*
* - If it's 0, the item never expires
* - If it's shorter than 30 days, the item will expires at
* {@code expirationTime} seconds.
* - If you want longer than 30 days, it's fine, just use an
* absolute time (time() + whatever).
*
*/
public SpyMemcachedSessionStore(
@NonNull MemcachedClient memcachedClient,
@NonNull ObjectMapper objectMapper,
int expirationTime) {
this.memcachedClient = memcachedClient;
this.expirationTime = expirationTime;
this.objectMapper = objectMapper;
}
/**
* Using CBOR serializer/deserializer.
*
* @param memcachedClient
* @param expirationTime
*/
public SpyMemcachedSessionStore(
MemcachedClient memcachedClient,
int expirationTime) {
this(memcachedClient, defaultObjectMapper, expirationTime);
}
@Override
public Optional