Skip to content

Commit

Permalink
publish LocalSessionDestroyedEvent on session removal from local cache
Browse files Browse the repository at this point in the history
  • Loading branch information
gierl committed Sep 28, 2021
1 parent c844cdf commit f5b58a9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.springframework.session.sticky;

import static java.util.Comparator.comparing;

import java.lang.ref.WeakReference;
import java.time.Duration;
import java.time.Instant;
Expand All @@ -27,7 +25,10 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.lang.Nullable;
import org.springframework.session.Session;
import org.springframework.session.events.SessionDestroyedEvent;
import org.springframework.session.sticky.StickySessionRepository.CacheEntry;
import org.springframework.util.Assert;

Expand All @@ -46,10 +47,17 @@ public class StickySessionCache {

protected Duration cleanupAfter = Duration.ofMinutes(DEFAULT_CLEANUP_AFTER_MINUTES);

private ApplicationEventPublisher eventPublisher = event -> {
};

public StickySessionCache(int cacheConcurrency) {
this.sessions = new ConcurrentHashMap<>(16, 0.75F, cacheConcurrency);
}

public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
}

/**
* Cached session entries that have not been accessed for {@linkplain #cleanupAfter the configured period} will
* be removed from the cache (but not the remote store) when {@link #cleanupOutdatedCacheEntries()} is called.
Expand All @@ -76,9 +84,11 @@ public void put(CacheEntry entry) {

@Nullable
public void remove(String id) {
sessions.remove(id);
final CacheEntry cacheEntry = sessions.remove(id);
// We don't remove from cleanup cache here, because that would require a separate mapping of session ids.
// The weak reference will be cleared, and the cleanup entry will simply be skipped when it's due.

eventPublisher.publishEvent(new LocalSessionDestroyedEvent(this, cacheEntry.createView()));
}

/**
Expand Down Expand Up @@ -142,12 +152,17 @@ void cleanup() {
if (session.getLastAccessedTime().isBefore(maxLastAccessed)) {
if (logger.isDebugEnabled())
logger.debug("Cached session " + session.getId() + " is scheduled for cleanup, removing from cache.");
sessions.remove(session.getId());
StickySessionCache.this.remove(session.getId());
} else {
schedule(session);
}
}
}
}

public static class LocalSessionDestroyedEvent extends SessionDestroyedEvent {
private LocalSessionDestroyedEvent(Object source, Session session) {
super(source, session);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.springframework.session.sticky;

import static java.util.Comparator.comparing;

import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
Expand Down Expand Up @@ -339,7 +337,7 @@ private synchronized void saveDelegate() {
delegateAwaitsSave = false;
}

private synchronized StickySession createView() {
synchronized StickySession createView() {
if (logger.isTraceEnabled())
logger.trace("Creating new session view for " + getId());
return new StickySession(this, new MapSession(cached));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

import java.time.Duration;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -140,6 +138,7 @@ public DelegateSaveStrategy stickySessionDelegateSaveStrategy() {
public StickySessionCache stickySessionCache() {
StickySessionCache cache = new StickySessionCache(this.sessionConcurrency);
cache.setCleanupAfter(this.cleanupAfter);
cache.setApplicationEventPublisher(this.eventPublisher);
return cache;
}

Expand Down

0 comments on commit f5b58a9

Please sign in to comment.