|
| 1 | +/** |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.caffeine; |
| 7 | + |
| 8 | +import com.github.benmanes.caffeine.cache.Cache; |
| 9 | +import com.github.benmanes.caffeine.cache.Caffeine; |
| 10 | +import io.jooby.Context; |
| 11 | +import io.jooby.Session; |
| 12 | +import io.jooby.SessionStore; |
| 13 | +import io.jooby.SessionToken; |
| 14 | + |
| 15 | +import javax.annotation.Nonnull; |
| 16 | +import javax.annotation.Nullable; |
| 17 | +import java.time.Duration; |
| 18 | +import java.util.concurrent.ConcurrentHashMap; |
| 19 | + |
| 20 | +/** |
| 21 | + * Caffeine session store. |
| 22 | + * |
| 23 | + * Usage: |
| 24 | + * <pre>{@code |
| 25 | + * { |
| 26 | + * setSessionStore(new CaffeineSessionStore()); |
| 27 | + * } |
| 28 | + * }</pre> |
| 29 | + * |
| 30 | + * Default session timeout is: <code>30 minutes</code>. |
| 31 | + * |
| 32 | + * @author edgar |
| 33 | + * @since 2.8.5 |
| 34 | + */ |
| 35 | +public class CaffeineSessionStore implements SessionStore { |
| 36 | + |
| 37 | + private final Cache<String, Session> cache; |
| 38 | + |
| 39 | + private SessionToken token = SessionToken.cookieId(SessionToken.SID); |
| 40 | + |
| 41 | + /** |
| 42 | + * Creates a new session store using the given cache. |
| 43 | + * |
| 44 | + * @param cache Cache. |
| 45 | + */ |
| 46 | + public CaffeineSessionStore(@Nonnull Cache<String, Session> cache) { |
| 47 | + this.cache = cache; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Creates a new session store with given session timeout. |
| 52 | + * |
| 53 | + * @param timeout Session timeout. |
| 54 | + */ |
| 55 | + public CaffeineSessionStore(@Nonnull Duration timeout) { |
| 56 | + this.cache = Caffeine.newBuilder() |
| 57 | + .expireAfterAccess(timeout) |
| 58 | + .build(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Creates a new session store with timeout of <code>30 minutes</code>. |
| 63 | + */ |
| 64 | + public CaffeineSessionStore() { |
| 65 | + this(Duration.ofMinutes(DEFAULT_TIMEOUT)); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Session token. |
| 70 | + * |
| 71 | + * @return Session token. Uses a cookie by default: {@link SessionToken#SID}. |
| 72 | + */ |
| 73 | + public @Nonnull SessionToken getToken() { |
| 74 | + return token; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Set custom session token. |
| 79 | + * |
| 80 | + * @param token Session token. |
| 81 | + * @return This store. |
| 82 | + */ |
| 83 | + public @Nonnull CaffeineSessionStore setToken(@Nonnull SessionToken token) { |
| 84 | + this.token = token; |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + @Nonnull @Override public Session newSession(@Nonnull Context ctx) { |
| 89 | + String sessionId = token.newToken(); |
| 90 | + Session session = cache |
| 91 | + .get(sessionId, key -> Session.create(ctx, sessionId, new ConcurrentHashMap<>())); |
| 92 | + |
| 93 | + token.saveToken(ctx, sessionId); |
| 94 | + |
| 95 | + return session; |
| 96 | + } |
| 97 | + |
| 98 | + @Nullable @Override public Session findSession(@Nonnull Context ctx) { |
| 99 | + String sessionId = token.findToken(ctx); |
| 100 | + if (sessionId == null) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + Session session = cache.getIfPresent(sessionId); |
| 104 | + if (session != null) { |
| 105 | + token.saveToken(ctx, sessionId); |
| 106 | + } |
| 107 | + return session; |
| 108 | + } |
| 109 | + |
| 110 | + @Override public void deleteSession(@Nonnull Context ctx, @Nonnull Session session) { |
| 111 | + cache.invalidate(session.getId()); |
| 112 | + token.deleteToken(ctx, session.getId()); |
| 113 | + } |
| 114 | + |
| 115 | + @Override public void touchSession(@Nonnull Context ctx, @Nonnull Session session) { |
| 116 | + token.saveToken(ctx, session.getId()); |
| 117 | + } |
| 118 | + |
| 119 | + @Override public void saveSession(@Nonnull Context ctx, @Nonnull Session session) { |
| 120 | + // NOOP due we do everything on memory |
| 121 | + } |
| 122 | + |
| 123 | + @Override public void renewSessionId(@Nonnull Context ctx, @Nonnull Session session) { |
| 124 | + |
| 125 | + } |
| 126 | +} |
0 commit comments