org.babyfish.jimmer.spring.cache.RedisValueBinder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jimmer-spring-boot-starter Show documentation
Show all versions of jimmer-spring-boot-starter Show documentation
A revolutionary ORM framework for both java and kotlin
package org.babyfish.jimmer.spring.cache;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.babyfish.jimmer.meta.ImmutableProp;
import org.babyfish.jimmer.meta.ImmutableType;
import org.babyfish.jimmer.sql.cache.spi.AbstractRemoteValueBinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.SessionCallback;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class RedisValueBinder extends AbstractRemoteValueBinder {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisValueBinder.class);
private final RedisOperations operations;
public RedisValueBinder(
RedisOperations operations,
ObjectMapper objectMapper,
ImmutableType type,
Duration duration
) {
super(objectMapper,type, null, duration, 30);
this.operations = operations;
}
public RedisValueBinder(
RedisConnectionFactory connectionFactory,
ObjectMapper objectMapper,
ImmutableType type,
Duration duration
) {
super(objectMapper,type, null, duration, 30);
this.operations = RedisCaches.cacheRedisTemplate(connectionFactory);
}
public RedisValueBinder(
RedisOperations operations,
ObjectMapper objectMapper,
ImmutableProp prop,
Duration duration
) {
super(objectMapper,null, prop, duration, 30);
this.operations = operations;
}
public RedisValueBinder(
RedisConnectionFactory connectionFactory,
ObjectMapper objectMapper,
ImmutableProp prop,
Duration duration
) {
super(objectMapper,null, prop, duration, 30);
this.operations = RedisCaches.cacheRedisTemplate(connectionFactory);
}
@Override
protected List read(Collection keys) {
return operations.opsForValue().multiGet(keys);
}
@SuppressWarnings("unchecked")
@Override
protected void write(Map map) {
operations.executePipelined(
new SessionCallback() {
@Override
public Void execute(RedisOperations pops) throws DataAccessException {
RedisOperations pipelinedOps = (RedisOperations)pops;
pipelinedOps.opsForValue().multiSet(map);
for (String key : map.keySet()) {
pipelinedOps.expire(
key,
nextExpireMillis(),
TimeUnit.MILLISECONDS
);
}
return null;
}
}
);
}
@Override
protected void delete(Collection keys) {
LOGGER.info("Delete data from redis: {}", keys);
operations.delete(keys);
}
@Override
protected String reason() {
return "redis";
}
}