Search in sources :

Example 1 with RedisSession

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));
                }
            });
        }
    }
}
Also used : SessionDeletedEvent(io.micronaut.session.event.SessionDeletedEvent) RedisSession(io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession) SessionExpiredEvent(io.micronaut.session.event.SessionExpiredEvent)

Example 2 with RedisSession

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();
}
Also used : io.lettuce.core.api.async(io.lettuce.core.api.async) RedisPubSubAsyncCommandsImpl(io.lettuce.core.pubsub.RedisPubSubAsyncCommandsImpl) SessionCreatedEvent(io.micronaut.session.event.SessionCreatedEvent) LoggerFactory(org.slf4j.LoggerFactory) StatefulConnection(io.lettuce.core.api.StatefulConnection) PreDestroy(javax.annotation.PreDestroy) TaskExecutors(io.micronaut.scheduling.TaskExecutors) StatefulRedisConnection(io.lettuce.core.api.StatefulRedisConnection) Duration(java.time.Duration) SessionExpiredEvent(io.micronaut.session.event.SessionExpiredEvent) BeanLocator(io.micronaut.context.BeanLocator) TypeHint(io.micronaut.core.annotation.TypeHint) ObjectSerializer(io.micronaut.core.serialize.ObjectSerializer) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) Singleton(jakarta.inject.Singleton) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StatefulRedisClusterConnection(io.lettuce.core.cluster.api.StatefulRedisClusterConnection) StringUtils(io.micronaut.core.util.StringUtils) RedisPubSubAdapter(io.lettuce.core.pubsub.RedisPubSubAdapter) Range(io.lettuce.core.Range) Replaces(io.micronaut.context.annotation.Replaces) Named(jakarta.inject.Named) java.util(java.util) RedisPubSubReactiveCommandsImpl(io.lettuce.core.pubsub.RedisPubSubReactiveCommandsImpl) RedisSession(io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession) ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext) SessionDeletedEvent(io.micronaut.session.event.SessionDeletedEvent) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) RedisAdvancedClusterAsyncCommands(io.lettuce.core.cluster.api.async.RedisAdvancedClusterAsyncCommands) Primary(io.micronaut.context.annotation.Primary) Charset(java.nio.charset.Charset) io.micronaut.session(io.micronaut.session) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RedisFuture(io.lettuce.core.RedisFuture) Requires(io.micronaut.context.annotation.Requires) RedisPubSubCommands(io.lettuce.core.pubsub.api.sync.RedisPubSubCommands) MutableConvertibleValues(io.micronaut.core.convert.value.MutableConvertibleValues) ExecutorService(java.util.concurrent.ExecutorService) RedisConnectionUtil(io.micronaut.configuration.lettuce.RedisConnectionUtil) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) Logger(org.slf4j.Logger) RedisAdvancedClusterCommands(io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands) StatefulRedisPubSubConnection(io.lettuce.core.pubsub.StatefulRedisPubSubConnection) ApplicationEventPublisher(io.micronaut.context.event.ApplicationEventPublisher) TimeUnit(java.util.concurrent.TimeUnit) ChronoUnit(java.time.temporal.ChronoUnit) CollectionUtils(io.micronaut.core.util.CollectionUtils) io.lettuce.core.api.sync(io.lettuce.core.api.sync) CompletableFuture(java.util.concurrent.CompletableFuture) Duration(java.time.Duration)

Aggregations

RedisSession (io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession)2 SessionDeletedEvent (io.micronaut.session.event.SessionDeletedEvent)2 SessionExpiredEvent (io.micronaut.session.event.SessionExpiredEvent)2 Range (io.lettuce.core.Range)1 RedisFuture (io.lettuce.core.RedisFuture)1 StatefulConnection (io.lettuce.core.api.StatefulConnection)1 StatefulRedisConnection (io.lettuce.core.api.StatefulRedisConnection)1 io.lettuce.core.api.async (io.lettuce.core.api.async)1 io.lettuce.core.api.sync (io.lettuce.core.api.sync)1 StatefulRedisClusterConnection (io.lettuce.core.cluster.api.StatefulRedisClusterConnection)1 RedisAdvancedClusterAsyncCommands (io.lettuce.core.cluster.api.async.RedisAdvancedClusterAsyncCommands)1 RedisAdvancedClusterCommands (io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands)1 RedisPubSubAdapter (io.lettuce.core.pubsub.RedisPubSubAdapter)1 RedisPubSubAsyncCommandsImpl (io.lettuce.core.pubsub.RedisPubSubAsyncCommandsImpl)1 RedisPubSubReactiveCommandsImpl (io.lettuce.core.pubsub.RedisPubSubReactiveCommandsImpl)1 StatefulRedisPubSubConnection (io.lettuce.core.pubsub.StatefulRedisPubSubConnection)1 RedisPubSubCommands (io.lettuce.core.pubsub.api.sync.RedisPubSubCommands)1 RedisConnectionUtil (io.micronaut.configuration.lettuce.RedisConnectionUtil)1 BeanLocator (io.micronaut.context.BeanLocator)1 Primary (io.micronaut.context.annotation.Primary)1