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

com.redis.spring.batch.writer.operation.Expire Maven / Gradle / Ivy

The newest version!
package com.redis.spring.batch.writer.operation;

import java.time.Duration;
import java.util.function.Function;

import org.springframework.batch.item.Chunk;

import io.lettuce.core.RedisFuture;
import io.lettuce.core.api.async.BaseRedisAsyncCommands;
import io.lettuce.core.api.async.RedisKeyAsyncCommands;

public class Expire extends AbstractKeyWriteOperation {

	private Function ttlFunction = t -> null;

	public Expire(Function keyFunction) {
		super(keyFunction);
	}

	public void setTtl(Duration duration) {
		this.ttlFunction = t -> duration;
	}

	public void setTtlFunction(Function function) {
		this.ttlFunction = function;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	protected void execute(BaseRedisAsyncCommands commands, T item, K key, Chunk> outputs) {
		Duration duration = ttlFunction.apply(item);
		if (duration != null && !duration.isNegative() && !duration.isZero()) {
			outputs.add((RedisFuture) ((RedisKeyAsyncCommands) commands).pexpire(key, duration));
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy