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

mybatis / redis-cache / #330

pending completion
#330

Pull #187

github

web-flow
Merge b3aa7e284 into 359e3da7b
Pull Request #187: Update dependency org.mybatis:mybatis-parent to v37

163 of 199 relevant lines covered (81.91%)

0.82 hits per line

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

89.19
/src/main/java/org/mybatis/caches/redis/RedisCache.java
1
/*
2
 *    Copyright 2015-2022 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(new RedisCallback() {
×
73
      @Override
74
      public Object doWithRedis(Jedis jedis) {
75
        Map<byte[], byte[]> result = jedis.hgetAll(id.getBytes());
×
76
        return result.size();
×
77
      }
78
    });
79
  }
80

81
  @Override
82
  public void putObject(final Object key, final Object value) {
83
    execute(new RedisCallback() {
1✔
84
      @Override
85
      public Object doWithRedis(Jedis jedis) {
86
        final byte[] idBytes = id.getBytes();
1✔
87
        jedis.hset(idBytes, key.toString().getBytes(), redisConfig.getSerializer().serialize(value));
1✔
88
        if (timeout != null && jedis.ttl(idBytes) == -1) {
1✔
89
          jedis.expire(idBytes, timeout);
1✔
90
        }
91
        return null;
1✔
92
      }
93
    });
94
  }
1✔
95

96
  @Override
97
  public Object getObject(final Object key) {
98
    return execute(new RedisCallback() {
1✔
99
      @Override
100
      public Object doWithRedis(Jedis jedis) {
101
        return redisConfig.getSerializer().unserialize(jedis.hget(id.getBytes(), key.toString().getBytes()));
1✔
102
      }
103
    });
104
  }
105

106
  @Override
107
  public Object removeObject(final Object key) {
108
    return execute(new RedisCallback() {
1✔
109
      @Override
110
      public Object doWithRedis(Jedis jedis) {
111
        return jedis.hdel(id, key.toString());
1✔
112
      }
113
    });
114
  }
115

116
  @Override
117
  public void clear() {
118
    execute(new RedisCallback() {
1✔
119
      @Override
120
      public Object doWithRedis(Jedis jedis) {
121
        jedis.del(id);
1✔
122
        return null;
1✔
123
      }
124
    });
125

126
  }
1✔
127

128
  @Override
129
  public ReadWriteLock getReadWriteLock() {
130
    return readWriteLock;
×
131
  }
132

133
  @Override
134
  public String toString() {
135
    return "Redis {" + id + "}";
1✔
136
  }
137

138
  public void setTimeout(Integer timeout) {
139
    this.timeout = timeout;
1✔
140
  }
1✔
141

142
}
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