Prototype a basic implementation of time-boxed, fixed duration expiration (e.g. 24 hours).
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.session.data.gemfire.support;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.session.Session;
|
||||
import org.springframework.session.SessionRepository;
|
||||
|
||||
/**
|
||||
* The FixedDurationExpirationSessionRepository class...
|
||||
*
|
||||
* @author John Blum
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class FixedDurationExpirationSessionRepository<S extends Session> implements SessionRepository<S> {
|
||||
|
||||
private final SessionRepository<S> delegate;
|
||||
|
||||
private final Duration expirationDuration;
|
||||
|
||||
public FixedDurationExpirationSessionRepository(SessionRepository<S> sessionRepository, Duration expirationDuration) {
|
||||
|
||||
this.delegate = Optional.ofNullable(sessionRepository)
|
||||
.orElseThrow(() -> new IllegalArgumentException("SessionRepository is required"));
|
||||
|
||||
this.expirationDuration = expirationDuration;
|
||||
}
|
||||
|
||||
protected SessionRepository<S> getDelegate() {
|
||||
return this.delegate;
|
||||
}
|
||||
|
||||
public Optional<Duration> getExpirationDuration() {
|
||||
return Optional.ofNullable(this.expirationDuration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public S createSession() {
|
||||
return getDelegate().createSession();
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public S findById(String id) {
|
||||
|
||||
return Optional.ofNullable(getDelegate().findById(id))
|
||||
.map(session ->
|
||||
getExpirationDuration()
|
||||
.map(expirationDuration -> handleExpired(session, expirationDuration))
|
||||
.orElse(session)
|
||||
).orElse(null);
|
||||
}
|
||||
|
||||
private S handleExpired(S session, Duration expirationDuration) {
|
||||
|
||||
if (isExpired(session, expirationDuration)) {
|
||||
deleteById(session.getId());
|
||||
session = null;
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
private boolean isExpired(S session, Duration expirationDuration) {
|
||||
|
||||
Instant sessionCreationTime = session.getCreationTime();
|
||||
Instant now = Instant.now();
|
||||
|
||||
return now.minusMillis(expirationDuration.toMillis()).isAfter(sessionCreationTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(S session) {
|
||||
getDelegate().save(session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(String id) {
|
||||
getDelegate().deleteById(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.session.data.gemfire.support;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.session.SessionRepository;
|
||||
|
||||
/**
|
||||
* The FixedDurationExpirationSessionRepositoryBeanPostProcessor class...
|
||||
*
|
||||
* @author John Blum
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class FixedDurationExpirationSessionRepositoryBeanPostProcessor implements BeanPostProcessor {
|
||||
|
||||
private final Duration expirationDuration;
|
||||
|
||||
public FixedDurationExpirationSessionRepositoryBeanPostProcessor(Duration expirationDuration) {
|
||||
this.expirationDuration = expirationDuration;
|
||||
}
|
||||
|
||||
@Nullable @Override @SuppressWarnings("unchecked")
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
|
||||
return (bean instanceof SessionRepository
|
||||
? new FixedDurationExpirationSessionRepository<>((SessionRepository) bean, this.expirationDuration)
|
||||
: bean);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user