org.babyfish.jimmer.spring.cache.RedisHashBinder 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.AbstractRemoteHashBinder;
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.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class RedisHashBinder extends AbstractRemoteHashBinder {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisHashBinder.class);
private final RedisOperations operations;
public RedisHashBinder(
RedisOperations operations,
ObjectMapper objectMapper,
ImmutableType type,
Duration duration
) {
super(objectMapper, type, null, duration, 30);
this.operations = operations;
}
public RedisHashBinder(
RedisConnectionFactory connectionFactory,
ObjectMapper objectMapper,
ImmutableType type,
Duration duration
) {
super(objectMapper, type, null, duration, 30);
this.operations = RedisCaches.cacheRedisTemplate(connectionFactory);
}
public RedisHashBinder(
RedisOperations operations,
ObjectMapper objectMapper,
ImmutableProp prop,
Duration duration
) {
super(objectMapper, null, prop, duration, 30);
this.operations = operations;
}
public RedisHashBinder(
RedisConnectionFactory connectionFactory,
ObjectMapper objectMapper,
ImmutableProp prop,
Duration duration
) {
super(objectMapper, null, prop, duration, 30);
this.operations = RedisCaches.cacheRedisTemplate(connectionFactory);
}
@SuppressWarnings("unchecked")
@Override
protected List read(Collection keys, String hashKey) {
return (List)(List>)operations.executePipelined(
new SessionCallback() {
@Override
public Void execute(RedisOperations pops) throws DataAccessException {
RedisOperations pipelinedOps = (RedisOperations)pops;
for (String key : keys) {
pipelinedOps.opsForHash().get(key, hashKey);
}
return null;
}
}
);
}
@SuppressWarnings("unchecked")
@Override
protected void write(Map map, String hashKey) {
operations.executePipelined(
new SessionCallback() {
@Override
public Void execute(RedisOperations pops) throws DataAccessException {
RedisOperations pipelinedOps = (RedisOperations)pops;
for (Map.Entry e : map.entrySet()) {
pipelinedOps.opsForHash().put(e.getKey(), hashKey, e.getValue());
pipelinedOps.expire(
e.getKey(),
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";
}
}