All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.ext.web.sstore.impl.LocalSessionStoreImpl Maven / Gradle / Ivy

There is a newer version: 4.5.10
Show newest version
/*
 * Copyright 2014 Red Hat, Inc.
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Apache License v2.0 which accompanies this distribution.
 *
 *  The Eclipse Public License is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  The Apache License v2.0 is available at
 *  http://www.opensource.org/licenses/apache2.0.php
 *
 *  You may elect to redistribute this code under either of these licenses.
 */

package io.vertx.ext.web.sstore.impl;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.shareddata.LocalMap;
import io.vertx.ext.web.Session;
import io.vertx.ext.web.sstore.LocalSessionStore;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Tim Fox
 */
public class LocalSessionStoreImpl implements LocalSessionStore, Handler {

  protected final Vertx vertx;
  protected final LocalMap localMap;
  private final long reaperInterval;
  private long timerID = -1;
  private boolean closed;

  public LocalSessionStoreImpl(Vertx vertx, String sessionMapName, long reaperInterval) {
    this.vertx = vertx;
    this.reaperInterval = reaperInterval;
    localMap = vertx.sharedData().getLocalMap(sessionMapName);
    setTimer();
  }

  @Override
  public Session createSession(long timeout) {
    return new SessionImpl(timeout);
  }

  @Override
  public long retryTimeout() {
    return 0;
  }

  @Override
  public void get(String id, Handler> resultHandler) {
    resultHandler.handle(Future.succeededFuture(localMap.get(id)));
  }

  @Override
  public void delete(String id, Handler> resultHandler) {
    resultHandler.handle(Future.succeededFuture(localMap.remove(id) != null));
  }

  @Override
  public void put(Session session, Handler> resultHandler) {
    localMap.put(session.id(), session);
    resultHandler.handle(Future.succeededFuture(true));
  }

  @Override
  public void clear(Handler> resultHandler) {
    localMap.clear();
    resultHandler.handle(Future.succeededFuture(true));
  }

  @Override
  public void size(Handler> resultHandler) {
    resultHandler.handle(Future.succeededFuture(localMap.size()));
  }

  @Override
  public synchronized void close() {
    localMap.close();
    if (timerID != -1) {
      vertx.cancelTimer(timerID);
    }
    closed = true;
  }

  @Override
  public synchronized void handle(Long tid) {
    long now = System.currentTimeMillis();
    Set toRemove = new HashSet<>();
    for (Session session: localMap.values()) {
      if (now - session.lastAccessed() > session.timeout()) {
        toRemove.add(session.id());
      }
    }
    for (String id: toRemove) {
      localMap.remove(id);
    }
    if (!closed) {
      setTimer();
    }
  }

  private void setTimer() {
    if (reaperInterval != 0) {
      timerID = vertx.setTimer(reaperInterval, this);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy