Skip to content

Commit 074e026

Browse files
committed
Bump to 11.30.1, add metrics
1 parent ac21c6b commit 074e026

5 files changed

Lines changed: 191 additions & 76 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<dependency>
2929
<groupId>io.ebean</groupId>
3030
<artifactId>ebean</artifactId>
31-
<version>11.28.1</version>
31+
<version>11.30.1</version>
3232
<scope>provided</scope>
3333
</dependency>
3434

src/main/java/io/ebean/redis/RedisCache.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import io.ebean.cache.ServerCache;
44
import io.ebean.cache.ServerCacheStatistics;
5+
import io.ebean.meta.MetricType;
6+
import io.ebean.metric.MetricFactory;
7+
import io.ebean.metric.TimedMetric;
58
import io.ebean.redis.encode.Encode;
6-
import io.ebeaninternal.metric.MetricFactory;
79
import redis.clients.jedis.Jedis;
810
import redis.clients.jedis.JedisPool;
911
import redis.clients.jedis.ScanParams;
@@ -22,18 +24,34 @@ class RedisCache implements ServerCache {
2224
private static final String CURSOR_0 = "0";
2325
private static final byte[] CURSOR_0_BYTES = SafeEncoder.encode(CURSOR_0);
2426

25-
// MetricFactory.get();
26-
2727
private final JedisPool jedisPool;
2828
private final String cacheKey;
2929
private final Encode keyEncode;
3030
private final Encode valueEncode;
3131

32+
private final TimedMetric metricGet;
33+
private final TimedMetric metricGetAll;
34+
private final TimedMetric metricPut;
35+
private final TimedMetric metricPutAll;
36+
private final TimedMetric metricRemove;
37+
private final TimedMetric metricRemoveAll;
38+
private final TimedMetric metricClear;
39+
3240
RedisCache(JedisPool jedisPool, String cacheKey, Encode keyEncode, Encode valueEncode) {
3341
this.jedisPool = jedisPool;
3442
this.cacheKey = cacheKey;
3543
this.keyEncode = keyEncode;
3644
this.valueEncode = valueEncode;
45+
46+
String prefix = "ebean.l2cache." + cacheKey;
47+
48+
metricGet = MetricFactory.get().createTimedMetric(MetricType.L2, prefix + ".get");
49+
metricGetAll = MetricFactory.get().createTimedMetric(MetricType.L2, prefix + ".getAll");
50+
metricPut = MetricFactory.get().createTimedMetric(MetricType.L2, prefix + ".put");
51+
metricPutAll = MetricFactory.get().createTimedMetric(MetricType.L2, prefix + ".putAll");
52+
metricRemove = MetricFactory.get().createTimedMetric(MetricType.L2, prefix + ".remove");
53+
metricRemoveAll = MetricFactory.get().createTimedMetric(MetricType.L2, prefix + ".removeAll");
54+
metricClear = MetricFactory.get().createTimedMetric(MetricType.L2, prefix + ".clear");
3755
}
3856

3957
private byte[] key(Object id) {
@@ -57,6 +75,7 @@ private Object valueDecode(byte[] data) {
5775
@Override
5876
public Map<Object, Object> getAll(Set<Object> keys) {
5977

78+
long start = System.nanoTime();
6079
Map<Object, Object> map = new LinkedHashMap<>();
6180

6281
List<Object> keyList = new ArrayList<>(keys);
@@ -65,28 +84,35 @@ public Map<Object, Object> getAll(Set<Object> keys) {
6584
for (int i = 0; i < keyList.size(); i++) {
6685
map.put(keyList.get(i), valueDecode(valsAsBytes.get(i)));
6786
}
87+
metricGetAll.add((System.nanoTime() - start) / 1000L, keyList.size());
6888
return map;
6989
}
7090
}
7191

7292

7393
@Override
7494
public Object get(Object id) {
95+
long start = System.nanoTime();
7596
try (Jedis resource = jedisPool.getResource()) {
76-
return valueDecode(resource.get(key(id)));
97+
Object val = valueDecode(resource.get(key(id)));
98+
metricGet.add((System.nanoTime() - start) / 1000L);
99+
return val;
77100
}
78101
}
79102

80103

81104
@Override
82105
public void put(Object id, Object value) {
106+
long start = System.nanoTime();
83107
try (Jedis resource = jedisPool.getResource()) {
84108
resource.set(key(id), value(value));
109+
metricPut.add((System.nanoTime() - start) / 1000L);
85110
}
86111
}
87112

88113
@Override
89114
public void putAll(Map<Object, Object> keyValues) {
115+
long start = System.nanoTime();
90116
try (Jedis resource = jedisPool.getResource()) {
91117
byte[][] raw = new byte[keyValues.size() * 2][];
92118
int pos = 0;
@@ -95,20 +121,25 @@ public void putAll(Map<Object, Object> keyValues) {
95121
raw[pos++] = value(entry.getValue());
96122
}
97123
resource.mset(raw);
124+
metricPutAll.add((System.nanoTime() - start) / 1000L, keyValues.size());
98125
}
99126
}
100127

101128
@Override
102129
public void remove(Object id) {
130+
long start = System.nanoTime();
103131
try (Jedis resource = jedisPool.getResource()) {
104132
resource.del(key(id));
133+
metricRemove.add((System.nanoTime() - start) / 1000L);
105134
}
106135
}
107136

108137
@Override
109138
public void removeAll(Set<Object> keys) {
139+
long start = System.nanoTime();
110140
try (Jedis resource = jedisPool.getResource()) {
111141
resource.del(keysAsBytes(keys));
142+
metricRemoveAll.add((System.nanoTime() - start) / 1000L, keys.size());
112143
}
113144
}
114145

@@ -123,7 +154,7 @@ private byte[][] keysAsBytes(Collection<Object> keys) {
123154

124155
@Override
125156
public void clear() {
126-
157+
long start = System.nanoTime();
127158
try (Jedis resource = jedisPool.getResource()) {
128159

129160
ScanParams params = new ScanParams();
@@ -144,8 +175,9 @@ public void clear() {
144175

145176
next = SafeEncoder.encode(nextCursor);
146177
} while (!next.equals("0"));
147-
}
148178

179+
metricClear.add((System.nanoTime() - start) / 1000L);
180+
}
149181
}
150182

151183
@Override

src/main/java/io/ebean/redis/RedisCacheFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ class RedisCacheFactory implements ServerCacheFactory {
5757
}
5858

5959
private JedisPool getJedisPool(ServerConfig serverConfig) {
60-
JedisPool jedisPool = (JedisPool) serverConfig.getServiceObject("jedisPool");
60+
JedisPool jedisPool = serverConfig.getServiceObject(JedisPool.class);
6161
if (jedisPool != null) {
6262
return jedisPool;
6363
}
64-
RedisConfig redisConfig = (RedisConfig) serverConfig.getServiceObject("redisConfig");
64+
RedisConfig redisConfig = serverConfig.getServiceObject(RedisConfig.class);
6565
if (redisConfig == null) {
6666
redisConfig = new RedisConfig();
6767
}
@@ -79,7 +79,8 @@ public ServerCache createCache(ServerCacheConfig config) {
7979

8080
private ServerCache createNormalCache(ServerCacheConfig config) {
8181

82-
// config.getCacheOptions().isNearCache();
82+
boolean nearCache = config.getCacheOptions().isNearCache();
83+
8384
String cacheKey = config.getCacheKey();
8485
EncodePrefixKey encodeKey = new EncodePrefixKey(cacheKey);
8586
switch (config.getType()) {

src/main/java/io/ebean/redis/RedisConfig.java

Lines changed: 115 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -10,73 +10,122 @@
1010
*/
1111
public class RedisConfig {
1212

13-
private int redisPort = 6379;
13+
private String server = "localhost";
1414

15-
private String redisServer = "localhost";
15+
private int port = 6379;
1616

17-
private int maxTotal = 200;
17+
private int maxTotal = 200;
1818

19-
private int maxIdle = 200;
20-
21-
private int minIdle = 1;
22-
23-
private long maxWaitMillis = -1L;
24-
25-
private boolean blockWhenExhausted = true;
26-
27-
/**
28-
* Return a new JedisPool based on the configuration.
29-
*/
30-
public JedisPool createPool() {
31-
32-
JedisPoolConfig poolConfig = new JedisPoolConfig();
33-
poolConfig.setMaxTotal(maxTotal);
34-
poolConfig.setMaxIdle(maxIdle);
35-
poolConfig.setMinIdle(minIdle);
36-
poolConfig.setMaxWaitMillis(maxWaitMillis);
37-
poolConfig.setBlockWhenExhausted(blockWhenExhausted);
38-
39-
return new JedisPool(poolConfig, redisServer, redisPort);
40-
}
41-
42-
43-
public void setRedisPort(int redisPort) {
44-
this.redisPort = redisPort;
45-
}
46-
47-
public String getRedisServer() {
48-
return redisServer;
49-
}
50-
51-
public void setRedisServer(String redisServer) {
52-
this.redisServer = redisServer;
53-
}
54-
55-
public int getMaxTotal() {
56-
return maxTotal;
57-
}
58-
59-
public void setMaxTotal(int maxTotal) {
60-
this.maxTotal = maxTotal;
61-
}
62-
63-
public int getMaxIdle() {
64-
return maxIdle;
65-
}
66-
67-
public void setMaxIdle(int maxIdle) {
68-
this.maxIdle = maxIdle;
69-
}
70-
71-
public int getMinIdle() {
72-
return minIdle;
73-
}
74-
75-
public void setMinIdle(int minIdle) {
76-
this.minIdle = minIdle;
77-
}
78-
79-
public void loadProperties(Properties properties) {
80-
81-
}
19+
private int maxIdle = 200;
20+
21+
private int minIdle = 1;
22+
23+
private long maxWaitMillis = -1L;
24+
25+
private boolean blockWhenExhausted = true;
26+
27+
/**
28+
* Return a new JedisPool based on the configuration.
29+
*/
30+
public JedisPool createPool() {
31+
32+
JedisPoolConfig poolConfig = new JedisPoolConfig();
33+
poolConfig.setMaxTotal(maxTotal);
34+
poolConfig.setMaxIdle(maxIdle);
35+
poolConfig.setMinIdle(minIdle);
36+
poolConfig.setMaxWaitMillis(maxWaitMillis);
37+
poolConfig.setBlockWhenExhausted(blockWhenExhausted);
38+
39+
return new JedisPool(poolConfig, server, port);
40+
}
41+
42+
public String getServer() {
43+
return server;
44+
}
45+
46+
public void setServer(String server) {
47+
this.server = server;
48+
}
49+
50+
public int getPort() {
51+
return port;
52+
}
53+
54+
public void setPort(int port) {
55+
this.port = port;
56+
}
57+
58+
public int getMaxTotal() {
59+
return maxTotal;
60+
}
61+
62+
public void setMaxTotal(int maxTotal) {
63+
this.maxTotal = maxTotal;
64+
}
65+
66+
public int getMaxIdle() {
67+
return maxIdle;
68+
}
69+
70+
public void setMaxIdle(int maxIdle) {
71+
this.maxIdle = maxIdle;
72+
}
73+
74+
public int getMinIdle() {
75+
return minIdle;
76+
}
77+
78+
public void setMinIdle(int minIdle) {
79+
this.minIdle = minIdle;
80+
}
81+
82+
public long getMaxWaitMillis() {
83+
return maxWaitMillis;
84+
}
85+
86+
public void setMaxWaitMillis(long maxWaitMillis) {
87+
this.maxWaitMillis = maxWaitMillis;
88+
}
89+
90+
public boolean isBlockWhenExhausted() {
91+
return blockWhenExhausted;
92+
}
93+
94+
public void setBlockWhenExhausted(boolean blockWhenExhausted) {
95+
this.blockWhenExhausted = blockWhenExhausted;
96+
}
97+
98+
public void loadProperties(Properties properties) {
99+
this.server = properties.getProperty("ebean.redis.server", server);
100+
this.port = getInt(properties, "ebean.redis.port", port);
101+
this.minIdle = getInt(properties, "ebean.redis.minIdle", minIdle);
102+
this.maxIdle = getInt(properties, "ebean.redis.maxIdle", maxIdle);
103+
this.maxTotal = getInt(properties, "ebean.redis.maxTotal", maxTotal);
104+
this.maxWaitMillis = getLong(properties, "ebean.redis.maxWaitMillis", maxWaitMillis);
105+
this.blockWhenExhausted = getBool(properties, "ebean.redis.blockWhenExhausted", blockWhenExhausted);
106+
}
107+
108+
private int getInt(Properties properties, String key, int defaulValue) {
109+
String val = properties.getProperty(key);
110+
if (val != null) {
111+
return Integer.parseInt(val.trim());
112+
}
113+
return defaulValue;
114+
}
115+
116+
private long getLong(Properties properties, String key, long defaultValue) {
117+
String val = properties.getProperty(key);
118+
if (val != null) {
119+
return Long.parseLong(val.trim());
120+
}
121+
return defaultValue;
122+
}
123+
124+
private boolean getBool(Properties properties, String key, boolean defaulValue) {
125+
String val = properties.getProperty(key);
126+
if (val != null) {
127+
return Boolean.parseBoolean(val.trim());
128+
}
129+
return defaulValue;
130+
}
82131
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.ebean.redis;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Properties;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
public class RedisConfigTest {
10+
11+
@Test
12+
public void loadProperties() {
13+
14+
Properties p = new Properties();
15+
p.setProperty("ebean.redis.server", "test-server");
16+
p.setProperty("ebean.redis.port", "99");
17+
p.setProperty("ebean.redis.maxIdle", "5");
18+
p.setProperty("ebean.redis.maxTotal", "6");
19+
p.setProperty("ebean.redis.minIdle", "7");
20+
p.setProperty("ebean.redis.maxWaitMillis", "8");
21+
22+
RedisConfig config = new RedisConfig();
23+
config.loadProperties(p);
24+
25+
assertThat(config.getServer()).isEqualTo("test-server");
26+
assertThat(config.getPort()).isEqualTo(99);
27+
assertThat(config.getMaxIdle()).isEqualTo(5);
28+
assertThat(config.getMaxTotal()).isEqualTo(6);
29+
assertThat(config.getMinIdle()).isEqualTo(7);
30+
assertThat(config.getMaxWaitMillis()).isEqualTo(8);
31+
32+
}
33+
}

0 commit comments

Comments
 (0)