Example usage for org.springframework.data.redis.core BoundSetOperations expire

List of usage examples for org.springframework.data.redis.core BoundSetOperations expire

Introduction

In this page you can find the example usage for org.springframework.data.redis.core BoundSetOperations expire.

Prototype

@Nullable
Boolean expire(long timeout, TimeUnit unit);

Source Link

Document

Sets the key time-to-live/expiration.

Usage

From source file:org.springframework.session.data.redis.RedisSessionExpirationPolicy.java

public void onExpirationUpdated(Long originalExpirationTimeInMilli, ExpiringSession session) {
    String keyToExpire = "expires:" + session.getId();
    long toExpire = roundUpToNextMinute(expiresInMillis(session));

    if (originalExpirationTimeInMilli != null) {
        long originalRoundedUp = roundUpToNextMinute(originalExpirationTimeInMilli);
        if (toExpire != originalRoundedUp) {
            String expireKey = getExpirationKey(originalRoundedUp);
            this.redis.boundSetOps(expireKey).remove(keyToExpire);
        }/*w  ww. j av a 2  s  .c  o  m*/
    }

    long sessionExpireInSeconds = session.getMaxInactiveIntervalInSeconds();
    String sessionKey = getSessionKey(keyToExpire);

    if (sessionExpireInSeconds < 0) {
        this.redis.boundValueOps(sessionKey).append("");
        this.redis.boundValueOps(sessionKey).persist();
        this.redis.boundHashOps(getSessionKey(session.getId())).persist();
        return;
    }

    String expireKey = getExpirationKey(toExpire);
    BoundSetOperations<Object, Object> expireOperations = this.redis.boundSetOps(expireKey);
    expireOperations.add(keyToExpire);

    long fiveMinutesAfterExpires = sessionExpireInSeconds + TimeUnit.MINUTES.toSeconds(5);

    expireOperations.expire(fiveMinutesAfterExpires, TimeUnit.SECONDS);
    if (sessionExpireInSeconds == 0) {
        this.redis.delete(sessionKey);
    } else {
        this.redis.boundValueOps(sessionKey).append("");
        this.redis.boundValueOps(sessionKey).expire(sessionExpireInSeconds, TimeUnit.SECONDS);
    }
    this.redis.boundHashOps(getSessionKey(session.getId())).expire(fiveMinutesAfterExpires, TimeUnit.SECONDS);
}