• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / redis-cache / 559

13 Apr 2025 12:38AM CUT coverage: 83.163%. Remained the same
559

push

github

web-flow
Merge pull request #301 from mybatis/renovate/junit5-monorepo

Update dependency org.junit.jupiter:junit-jupiter-engine to v5.12.2

51 of 80 branches covered (63.75%)

163 of 196 relevant lines covered (83.16%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/src/main/java/org/mybatis/caches/redis/RedisCache.java
1
/*
2
 *    Copyright 2015-2023 the original author or authors.
3
 *
4
 *    Licensed under the Apache License, Version 2.0 (the "License");
5
 *    you may not use this file except in compliance with the License.
6
 *    You may obtain a copy of the License at
7
 *
8
 *       https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *    Unless required by applicable law or agreed to in writing, software
11
 *    distributed under the License is distributed on an "AS IS" BASIS,
12
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *    See the License for the specific language governing permissions and
14
 *    limitations under the License.
15
 */
16
package org.mybatis.caches.redis;
17

18
import java.util.Map;
19
import java.util.concurrent.locks.ReadWriteLock;
20

21
import org.apache.ibatis.cache.Cache;
22

23
import redis.clients.jedis.Jedis;
24
import redis.clients.jedis.JedisPool;
25

26
/**
27
 * Cache adapter for Redis.
28
 *
29
 * @author Eduardo Macarron
30
 */
31
public final class RedisCache implements Cache {
32

33
  private final ReadWriteLock readWriteLock = new DummyReadWriteLock();
1✔
34

35
  private String id;
36

37
  private static JedisPool pool;
38

39
  private final RedisConfig redisConfig;
40

41
  private Integer timeout;
42

43
  public RedisCache(final String id) {
1✔
44
    if (id == null) {
1✔
45
      throw new IllegalArgumentException("Cache instances require an ID");
1✔
46
    }
47
    this.id = id;
1✔
48
    redisConfig = RedisConfigurationBuilder.getInstance().parseConfiguration();
1✔
49
    pool = new JedisPool(redisConfig, redisConfig.getHost(), redisConfig.getPort(), redisConfig.getConnectionTimeout(),
1✔
50
        redisConfig.getSoTimeout(), redisConfig.getPassword(), redisConfig.getDatabase(), redisConfig.getClientName(),
1✔
51
        redisConfig.isSsl(), redisConfig.getSslSocketFactory(), redisConfig.getSslParameters(),
1✔
52
        redisConfig.getHostnameVerifier());
1✔
53
  }
1✔
54

55
  // TODO Review this is UNUSED
56
  private Object execute(RedisCallback callback) {
57
    Jedis jedis = pool.getResource();
1✔
58
    try {
59
      return callback.doWithRedis(jedis);
1✔
60
    } finally {
61
      jedis.close();
1✔
62
    }
63
  }
64

65
  @Override
66
  public String getId() {
67
    return this.id;
1✔
68
  }
69

70
  @Override
71
  public int getSize() {
72
    return (Integer) execute(jedis -> {
×
73
      Map<byte[], byte[]> result = jedis.hgetAll(id.getBytes());
×
74
      return result.size();
×
75
    });
76
  }
77

78
  @Override
79
  public void putObject(final Object key, final Object value) {
80
    execute(jedis -> {
1✔
81
      final byte[] idBytes = id.getBytes();
1✔
82
      jedis.hset(idBytes, key.toString().getBytes(), redisConfig.getSerializer().serialize(value));
1✔
83
      if (timeout != null && jedis.ttl(idBytes) == -1) {
1✔
84
        jedis.expire(idBytes, timeout);
1✔
85
      }
86
      return null;
1✔
87
    });
88
  }
1✔
89

90
  @Override
91
  public Object getObject(final Object key) {
92
    return execute(
1✔
93
        jedis -> redisConfig.getSerializer().unserialize(jedis.hget(id.getBytes(), key.toString().getBytes())));
1✔
94
  }
95

96
  @Override
97
  public Object removeObject(final Object key) {
98
    return execute(jedis -> jedis.hdel(id, key.toString()));
1✔
99
  }
100

101
  @Override
102
  public void clear() {
103
    execute(jedis -> {
1✔
104
      jedis.del(id);
1✔
105
      return null;
1✔
106
    });
107
  }
1✔
108

109
  @Override
110
  public ReadWriteLock getReadWriteLock() {
111
    return readWriteLock;
×
112
  }
113

114
  @Override
115
  public String toString() {
116
    return "Redis {" + id + "}";
1✔
117
  }
118

119
  public void setTimeout(Integer timeout) {
120
    this.timeout = timeout;
1✔
121
  }
1✔
122

123
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc