1   /*
2    * Copyright 2018 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package com.linecorp.centraldogma.server.internal.admin.auth;
17  
18  import static java.util.Objects.requireNonNull;
19  
20  import java.util.concurrent.CompletableFuture;
21  
22  import com.github.benmanes.caffeine.cache.Cache;
23  
24  import com.linecorp.centraldogma.server.auth.Session;
25  import com.linecorp.centraldogma.server.auth.SessionManager;
26  
27  /**
28   * Memory cache based {@link SessionManager} implementation.
29   */
30  public final class CachedSessionManager extends ForwardingSessionManager {
31  
32      private final Cache<String, Session> cache;
33  
34      /**
35       * Creates a new {@link SessionManager} instance which relies on the memory cache.
36       *
37       * @param delegate the {@link SessionManager} which manages the sessions using any kind of permanent
38       *                 storage
39       * @param cache the {@link Cache} instance which is configured by the caller
40       */
41      public CachedSessionManager(SessionManager delegate, Cache<String, Session> cache) {
42          super(delegate);
43          this.cache = requireNonNull(cache, "cache");
44      }
45  
46      @Override
47      public CompletableFuture<Boolean> exists(String sessionId) {
48          requireNonNull(sessionId, "sessionId");
49          if (cache.getIfPresent(sessionId) != null) {
50              return CompletableFuture.completedFuture(true);
51          }
52          return super.exists(sessionId);
53      }
54  
55      @Override
56      public CompletableFuture<Session> get(String sessionId) {
57          requireNonNull(sessionId, "sessionId");
58          final Session session = cache.getIfPresent(sessionId);
59          if (session != null) {
60              return CompletableFuture.completedFuture(session);
61          }
62          return super.get(sessionId).thenApply(found -> {
63              if (found != null) {
64                  cache.put(sessionId, found);
65              }
66              return found;
67          });
68      }
69  
70      @Override
71      public CompletableFuture<Void> create(Session session) {
72          return super.create(session).thenApply(unused -> {
73              cache.put(session.id(), session);
74              return null;
75          });
76      }
77  
78      @Override
79      public CompletableFuture<Void> update(Session session) {
80          return super.update(session).thenApply(unused -> {
81              cache.put(session.id(), session);
82              return null;
83          });
84      }
85  
86      @Override
87      public CompletableFuture<Void> delete(String sessionId) {
88          return super.delete(sessionId).thenApply(unused -> {
89              cache.invalidate(sessionId);
90              return null;
91          });
92      }
93  }