use of io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession in project micronaut-redis by micronaut-projects.
the class RedisSessionStore method message.
@Override
public void message(String pattern, String channel, String message) {
if (message.startsWith(expiryPrefix)) {
boolean expired = pattern.endsWith(":expired");
if (pattern.endsWith(":del") || expired) {
String id = message.substring(expiryPrefix.length());
redisSortedSetAsyncCommands.zrem(activeSessionsSet, id.getBytes(charset)).whenComplete((aVoid, throwable) -> {
if (throwable != null) {
if (LOG.isErrorEnabled()) {
LOG.error("Error removing session [" + id + "] from active sessions: " + throwable.getMessage(), throwable);
}
}
});
findSessionInternal(id, true).whenComplete((optional, throwable) -> {
if (throwable == null && optional.isPresent()) {
RedisSession session = optional.get();
eventPublisher.publishEvent(expired ? new SessionExpiredEvent(session) : new SessionDeletedEvent(session));
}
});
}
}
}
use of io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession in project micronaut-redis by micronaut-projects.
the class RedisSessionStore method saveSessionDelta.
private CompletableFuture<RedisSession> saveSessionDelta(RedisSession session, Map<byte[], byte[]> changes) {
Duration maxInactiveInterval = session.getMaxInactiveInterval();
long expirySeconds = maxInactiveInterval.getSeconds();
byte[] sessionKey = getSessionKey(session.getId());
byte[] sessionIdBytes = session.getId().getBytes(charset);
if (expirySeconds == 0) {
// delete the expired session
RedisFuture<Long> deleteOp = redisKeyAsyncCommands.del(getExpiryKey(session));
return deleteOp.thenCompose(ignore -> redisHashAsyncCommands.hmset(sessionKey, changes)).thenApply(ignore -> session).toCompletableFuture();
}
return redisHashAsyncCommands.hmset(sessionKey, changes).thenCompose(ignore -> {
try {
if (session.isNew()) {
session.clearModifications();
baseRedisAsyncCommands.publish(sessionCreatedTopic, sessionIdBytes).whenComplete((aLong, throwable12) -> {
if (throwable12 != null) {
if (LOG.isErrorEnabled()) {
LOG.error("Error publishing session creation event: " + throwable12.getMessage(), throwable12);
}
}
});
} else {
session.clearModifications();
}
} catch (Throwable e) {
if (LOG.isErrorEnabled()) {
LOG.error("Error publishing session creation event: " + e.getMessage(), e);
}
}
long fiveMinutesAfterExpires = expirySeconds + TimeUnit.MINUTES.toSeconds(EXPIRATION_SECONDS);
byte[] expiryKey = getExpiryKey(session);
double expireTimeScore = Long.valueOf(Instant.now().plus(expirySeconds, ChronoUnit.SECONDS).toEpochMilli()).doubleValue();
CompletableFuture<Boolean> expireOp = redisKeyAsyncCommands.expire(sessionKey, fiveMinutesAfterExpires).toCompletableFuture();
CompletableFuture<String> saveExpiryOp = redisStringAsyncCommands.setex(expiryKey, expirySeconds, String.valueOf(expirySeconds).getBytes()).toCompletableFuture();
CompletableFuture<Long> saveActiveSessionOp = redisSortedSetAsyncCommands.zadd(activeSessionsSet, expireTimeScore, sessionIdBytes).toCompletableFuture();
return CompletableFuture.allOf(expireOp, saveExpiryOp, saveActiveSessionOp).thenApply(ignore2 -> session);
}).toCompletableFuture();
}
Aggregations