io.ebean.redis.RedisCache Maven / Gradle / Ivy
package io.ebean.redis;
import io.ebean.cache.ServerCache;
import io.ebean.cache.ServerCacheConfig;
import io.ebean.cache.ServerCacheStatistics;
import io.ebean.meta.MetricType;
import io.ebean.meta.MetricVisitor;
import io.ebean.metric.CountMetric;
import io.ebean.metric.MetricFactory;
import io.ebean.metric.TimedMetric;
import io.ebean.redis.encode.Encode;
import io.ebean.redis.encode.EncodePrefixKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.util.SafeEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static io.ebean.meta.MetricType.L2;
class RedisCache implements ServerCache {
private static final Logger log = LoggerFactory.getLogger(RedisCache.class);
private static final String CURSOR_0 = "0";
private static final byte[] CURSOR_0_BYTES = SafeEncoder.encode(CURSOR_0);
private final JedisPool jedisPool;
private final String cacheKey;
private final Encode keyEncode;
private final Encode valueEncode;
private final TimedMetric metricGet;
private final TimedMetric metricGetAll;
private final TimedMetric metricPut;
private final TimedMetric metricPutAll;
private final TimedMetric metricRemove;
private final TimedMetric metricRemoveAll;
private final TimedMetric metricClear;
private final CountMetric hitCount;
private final CountMetric missCount;
RedisCache(JedisPool jedisPool, ServerCacheConfig config, Encode valueEncode) {
this.jedisPool = jedisPool;
this.cacheKey = config.getCacheKey();
this.keyEncode = new EncodePrefixKey(config.getCacheKey());
this.valueEncode = valueEncode;
String pre = "l2r.";
String shortName = config.getShortName();
MetricFactory factory = MetricFactory.get();
hitCount = factory.createCountMetric(L2, pre + shortName + ".hit");
missCount = factory.createCountMetric(L2, pre + shortName + ".miss");
metricGet = factory.createTimedMetric(MetricType.L2, pre + shortName + ".get");
metricGetAll = factory.createTimedMetric(MetricType.L2, pre + shortName + ".getMany");
metricPut = factory.createTimedMetric(MetricType.L2, pre + shortName + ".put");
metricPutAll = factory.createTimedMetric(MetricType.L2, pre + shortName + ".putMany");
metricRemove = factory.createTimedMetric(MetricType.L2, pre + shortName + ".remove");
metricRemoveAll = factory.createTimedMetric(MetricType.L2, pre + shortName + ".removeMany");
metricClear = factory.createTimedMetric(MetricType.L2, pre + shortName + ".clear");
}
@Override
public void visit(MetricVisitor visitor) {
hitCount.visit(visitor);
missCount.visit(visitor);
metricGet.visit(visitor);
metricGetAll.visit(visitor);
metricPut.visit(visitor);
metricPutAll.visit(visitor);
metricRemove.visit(visitor);
metricRemoveAll.visit(visitor);
metricClear.visit(visitor);
}
private byte[] key(Object id) {
return keyEncode.encode(id);
}
private byte[] value(Object data) {
if (data == null) {
return null;
}
return valueEncode.encode(data);
}
private Object valueDecode(byte[] data) {
try {
if (data == null) {
return null;
}
return valueEncode.decode(data);
} catch (Exception e) {
log.error("Error decoding data, treated as cache miss", e);
return null;
}
}
@Override
public Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy