io.ebean.redis.RedisCache Maven / Gradle / Ivy
package io.ebean.redis;
import io.avaje.applog.AppLog;
import io.ebean.cache.ServerCache;
import io.ebean.cache.ServerCacheConfig;
import io.ebean.cache.ServerCacheOptions;
import io.ebean.cache.ServerCacheStatistics;
import io.ebean.meta.MetricVisitor;
import io.ebean.metric.CountMetric;
import io.ebean.metric.MetricFactory;
import io.ebean.metric.TimedMetric;
import io.ebean.metric.TimedMetricStats;
import io.ebean.redis.encode.Encode;
import io.ebean.redis.encode.EncodePrefixKey;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Transaction;
import redis.clients.jedis.params.ScanParams;
import redis.clients.jedis.params.SetParams;
import redis.clients.jedis.resps.ScanResult;
import redis.clients.jedis.util.SafeEncoder;
import java.util.*;
import static java.lang.System.Logger.Level.ERROR;
import static java.lang.System.Logger.Level.WARNING;
final class RedisCache implements ServerCache {
private static final System.Logger log = AppLog.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 EncodePrefixKey keyEncode;
private final Encode valueEncode;
private final SetParams expiration;
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;
this.expiration = expiration(config);
String namePrefix = "l2r." + config.getShortName();
MetricFactory factory = MetricFactory.get();
hitCount = factory.createCountMetric(namePrefix + ".hit");
missCount = factory.createCountMetric(namePrefix + ".miss");
metricGet = factory.createTimedMetric(namePrefix + ".get");
metricGetAll = factory.createTimedMetric(namePrefix + ".getMany");
metricPut = factory.createTimedMetric(namePrefix + ".put");
metricPutAll = factory.createTimedMetric(namePrefix + ".putMany");
metricRemove = factory.createTimedMetric(namePrefix + ".remove");
metricRemoveAll = factory.createTimedMetric(namePrefix + ".removeMany");
metricClear = factory.createTimedMetric(namePrefix + ".clear");
}
private SetParams expiration(ServerCacheConfig config) {
final ServerCacheOptions cacheOptions = config.getCacheOptions();
if (cacheOptions != null) {
final int maxSecsToLive = cacheOptions.getMaxSecsToLive();
if (maxSecsToLive > 0) {
return new SetParams().ex((long) maxSecsToLive);
}
}
return null;
}
@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.log(ERROR, "Error decoding data, treated as cache miss", e);
return null;
}
}
private void errorOnRead(Exception e) {
log.log(WARNING, "Error when reading redis cache", e);
}
private void errorOnWrite(Exception e) {
log.log(WARNING, "Error when writing redis cache", e);
}
@Override
public Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy