├── .gitignore ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── zzy │ │ └── util │ │ ├── hash │ │ ├── HashRedisConfig.java │ │ ├── HashRedisExecutor.java │ │ └── HashRedisUtil.java │ │ └── sharded │ │ ├── ShardedRedisExecutor.java │ │ ├── ShardedRedisUtil.java │ │ └── SharedRedisConfig.java └── resources │ ├── hashRedis.properties │ ├── logback.xml │ └── shardedRedis.properties └── test └── java └── com └── zzy └── util ├── HashRedisUtilTest.java └── ShardedRedisUtilTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.metadata 2 | */bin/* 3 | */bin/ 4 | */tmp/** 5 | *.tmp 6 | *.bak 7 | *.swp 8 | *~.nib 9 | *.classpath 10 | *.project 11 | *.settings/* 12 | *.settings/ 13 | */target/* 14 | */target/ 15 | target 16 | *.class 17 | *.iml 18 | .idea -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | zzy 5 | redisUtil 6 | 0.0.1-SNAPSHOT 7 | 8 | 9 | 1.7 10 | 2.8.0 11 | 1.7.12 12 | 4.12 13 | 1.1.3 14 | 2.6 15 | ${java.version} 16 | ${java.version} 17 | 18 | 19 | 20 | 21 | redis.clients 22 | jedis 23 | ${redis.version} 24 | 25 | 26 | commons-lang 27 | commons-lang 28 | ${commons-lang.version} 29 | 30 | 31 | junit 32 | junit 33 | ${junit.version} 34 | test 35 | 36 | 37 | 38 | ch.qos.logback 39 | logback-core 40 | ${logback.version} 41 | 42 | 43 | ch.qos.logback 44 | logback-classic 45 | ${logback.version} 46 | 47 | 48 | org.slf4j 49 | jcl-over-slf4j 50 | ${slf4j.version} 51 | 52 | 53 | org.slf4j 54 | log4j-over-slf4j 55 | ${slf4j.version} 56 | 57 | 58 | 59 | ${project.artifactId} 60 | 61 | 62 | org.apache.maven.plugins 63 | maven-compiler-plugin 64 | 65 | ${java.version} 66 | ${java.version} 67 | ${project.build.sourceEncoding} 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /src/main/java/com/zzy/util/hash/HashRedisConfig.java: -------------------------------------------------------------------------------- 1 | package com.zzy.util.hash; 2 | 3 | import java.util.ResourceBundle; 4 | 5 | /** 6 | * 读取redis配置 7 | * 8 | * @author zhengzhiyuan 9 | * @since May 20, 2016 10 | */ 11 | public class HashRedisConfig { 12 | private static final String DEFAULT_REDIS_PROPERTIES = "hashRedis"; 13 | private static ResourceBundle REDIS_CONFIG = ResourceBundle.getBundle(DEFAULT_REDIS_PROPERTIES); 14 | 15 | public static String getConfigProperty(String key) { 16 | return REDIS_CONFIG.getString(key); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/zzy/util/hash/HashRedisExecutor.java: -------------------------------------------------------------------------------- 1 | package com.zzy.util.hash; 2 | 3 | import redis.clients.jedis.Jedis; 4 | 5 | // redis具体逻辑接口 6 | public interface HashRedisExecutor { 7 | T execute(Jedis jedis); 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/zzy/util/hash/HashRedisUtil.java: -------------------------------------------------------------------------------- 1 | package com.zzy.util.hash; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | import org.apache.commons.lang.math.NumberUtils; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | 11 | import redis.clients.jedis.Jedis; 12 | import redis.clients.jedis.JedisPool; 13 | import redis.clients.jedis.JedisPoolConfig; 14 | 15 | /** 16 | * 对redis pool 进行hash分片 17 | * 18 | * @author zhengzhiyuan 19 | * @since May 20, 2016 20 | */ 21 | public class HashRedisUtil { 22 | private static final Logger LOGGER = LoggerFactory.getLogger(HashRedisUtil.class); 23 | 24 | private static final String DEFAULT_REDIS_SEPARATOR = ";"; 25 | 26 | private static final String HOST_PORT_SEPARATOR = ":"; 27 | 28 | private JedisPool[] jedisPools = new JedisPool[0]; 29 | 30 | private static final HashRedisUtil INSTANCE = new HashRedisUtil(); 31 | 32 | private HashRedisUtil() { 33 | initPool(); 34 | } 35 | 36 | private void initPool() { 37 | 38 | // 操作超时时间,默认2秒 39 | int timeout = NumberUtils.toInt(HashRedisConfig.getConfigProperty("redis.timeout"), 2000); 40 | // jedis池最大连接数总数,默认8 41 | int maxTotal = NumberUtils.toInt(HashRedisConfig.getConfigProperty("redis.jedisPoolConfig.maxTotal"), 8); 42 | // jedis池最大空闲连接数,默认8 43 | int maxIdle = NumberUtils.toInt(HashRedisConfig.getConfigProperty("redis.jedisPoolConfig.maxIdle"), 8); 44 | // jedis池最少空闲连接数 45 | int minIdle = NumberUtils.toInt(HashRedisConfig.getConfigProperty("redis.jedisPoolConfig.minIdle"), 0); 46 | // jedis池没有对象返回时,最大等待时间单位为毫秒 47 | long maxWaitMillis = NumberUtils.toLong(HashRedisConfig.getConfigProperty("redis.jedisPoolConfig.maxWaitTime"), -1); 48 | // 在borrow一个jedis实例时,是否提前进行validate操作 49 | boolean testOnBorrow = Boolean.parseBoolean(HashRedisConfig.getConfigProperty("redis.jedisPoolConfig.testOnBorrow")); 50 | 51 | // 设置jedis连接池配置 52 | JedisPoolConfig poolConfig = new JedisPoolConfig(); 53 | poolConfig.setMaxTotal(maxTotal); 54 | poolConfig.setMaxIdle(maxIdle); 55 | poolConfig.setMinIdle(minIdle); 56 | poolConfig.setMaxWaitMillis(maxWaitMillis); 57 | poolConfig.setTestOnBorrow(testOnBorrow); 58 | 59 | // 取得redis的url 60 | String redisUrls = HashRedisConfig.getConfigProperty("redis.jedisPoolConfig.urls"); 61 | if (redisUrls == null || redisUrls.trim().isEmpty()) { 62 | throw new IllegalStateException("the urls of redis is not configured"); 63 | } 64 | LOGGER.info("the urls of redis is {}", redisUrls); 65 | 66 | // 生成连接池 67 | List jedisPoolList = new ArrayList(); 68 | for (String redisUrl : redisUrls.split(DEFAULT_REDIS_SEPARATOR)) { 69 | String[] redisUrlInfo = redisUrl.split(HOST_PORT_SEPARATOR); 70 | jedisPoolList.add(new JedisPool(poolConfig, redisUrlInfo[0], Integer.parseInt(redisUrlInfo[1]), timeout)); 71 | } 72 | 73 | jedisPools = jedisPoolList.toArray(jedisPools); 74 | } 75 | 76 | public static HashRedisUtil getInstance() { 77 | return INSTANCE; 78 | } 79 | 80 | /** 81 | * 实现jedis连接的获取和释放,具体的redis业务逻辑由executor实现 82 | * 83 | * @param executor RedisExecutor接口的实现类 84 | * @return 85 | */ 86 | public T execute(String key, HashRedisExecutor executor) { 87 | Jedis jedis = jedisPools[(0x7FFFFFFF & key.hashCode()) % jedisPools.length].getResource(); 88 | T result = null; 89 | try { 90 | result = executor.execute(jedis); 91 | } finally { 92 | if (jedis != null) { 93 | jedis.close(); 94 | } 95 | } 96 | return result; 97 | } 98 | 99 | public String set(final String key, final String value) { 100 | return execute(key, new HashRedisExecutor() { 101 | @Override 102 | public String execute(Jedis jedis) { 103 | return jedis.set(key, value); 104 | } 105 | }); 106 | } 107 | 108 | public String set(final String key, final String value, final String nxxx, final String expx, final long time) { 109 | return execute(key, new HashRedisExecutor() { 110 | @Override 111 | public String execute(Jedis jedis) { 112 | return jedis.set(key, value, nxxx, expx, time); 113 | } 114 | }); 115 | } 116 | 117 | public String get(final String key) { 118 | return execute(key, new HashRedisExecutor() { 119 | @Override 120 | public String execute(Jedis jedis) { 121 | return jedis.get(key); 122 | } 123 | }); 124 | } 125 | 126 | public Boolean exists(final String key) { 127 | return execute(key, new HashRedisExecutor() { 128 | @Override 129 | public Boolean execute(Jedis jedis) { 130 | return jedis.exists(key); 131 | } 132 | }); 133 | } 134 | 135 | public Long setnx(final String key, final String value) { 136 | return execute(key, new HashRedisExecutor() { 137 | @Override 138 | public Long execute(Jedis jedis) { 139 | return jedis.setnx(key, value); 140 | } 141 | }); 142 | } 143 | 144 | public String setex(final String key, final int seconds, final String value) { 145 | return execute(key, new HashRedisExecutor() { 146 | @Override 147 | public String execute(Jedis jedis) { 148 | return jedis.setex(key, seconds, value); 149 | } 150 | }); 151 | } 152 | 153 | public Long expire(final String key, final int seconds) { 154 | return execute(key, new HashRedisExecutor() { 155 | @Override 156 | public Long execute(Jedis jedis) { 157 | return jedis.expire(key, seconds); 158 | } 159 | }); 160 | } 161 | 162 | public Long incr(final String key) { 163 | return execute(key, new HashRedisExecutor() { 164 | @Override 165 | public Long execute(Jedis jedis) { 166 | return jedis.incr(key); 167 | } 168 | }); 169 | } 170 | 171 | public Long decr(final String key) { 172 | return execute(key, new HashRedisExecutor() { 173 | @Override 174 | public Long execute(Jedis jedis) { 175 | return jedis.decr(key); 176 | } 177 | }); 178 | } 179 | 180 | public Long hset(final String key, final String field, final String value) { 181 | return execute(key, new HashRedisExecutor() { 182 | @Override 183 | public Long execute(Jedis jedis) { 184 | return jedis.hset(key, field, value); 185 | } 186 | }); 187 | } 188 | 189 | public String hget(final String key, final String field) { 190 | return execute(key, new HashRedisExecutor() { 191 | @Override 192 | public String execute(Jedis jedis) { 193 | return jedis.hget(key, field); 194 | } 195 | }); 196 | } 197 | 198 | public String hmset(final String key, final Map hash) { 199 | return execute(key, new HashRedisExecutor() { 200 | @Override 201 | public String execute(Jedis jedis) { 202 | return jedis.hmset(key, hash); 203 | } 204 | }); 205 | } 206 | 207 | public List hmget(final String key, final String... fields) { 208 | return execute(key, new HashRedisExecutor>() { 209 | @Override 210 | public List execute(Jedis jedis) { 211 | return jedis.hmget(key, fields); 212 | } 213 | }); 214 | } 215 | 216 | public Long del(final String key) { 217 | return execute(key, new HashRedisExecutor() { 218 | @Override 219 | public Long execute(Jedis jedis) { 220 | return jedis.del(key); 221 | } 222 | }); 223 | } 224 | 225 | public Map hgetAll(final String key) { 226 | return execute(key, new HashRedisExecutor>() { 227 | @Override 228 | public Map execute(Jedis jedis) { 229 | return jedis.hgetAll(key); 230 | } 231 | }); 232 | } 233 | 234 | public void destroy() { 235 | for (int i = 0; i < jedisPools.length; i++) { 236 | jedisPools[i].close(); 237 | } 238 | } 239 | } 240 | -------------------------------------------------------------------------------- /src/main/java/com/zzy/util/sharded/ShardedRedisExecutor.java: -------------------------------------------------------------------------------- 1 | package com.zzy.util.sharded; 2 | 3 | import redis.clients.jedis.ShardedJedis; 4 | 5 | // redis具体逻辑接口 6 | public interface ShardedRedisExecutor { 7 | T execute(ShardedJedis jedis); 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/zzy/util/sharded/ShardedRedisUtil.java: -------------------------------------------------------------------------------- 1 | package com.zzy.util.sharded; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | import org.apache.commons.lang.math.NumberUtils; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | 11 | import redis.clients.jedis.JedisPoolConfig; 12 | import redis.clients.jedis.JedisShardInfo; 13 | import redis.clients.jedis.ShardedJedis; 14 | import redis.clients.jedis.ShardedJedisPool; 15 | import redis.clients.util.Hashing; 16 | 17 | /** 18 | * 分片redis 19 | * 20 | * @author zhengzhiyuan 21 | * @since May 20, 2016 22 | */ 23 | public class ShardedRedisUtil { 24 | 25 | private static final Logger LOGGER = LoggerFactory.getLogger(ShardedRedisUtil.class); 26 | 27 | private static final String DEFAULT_REDIS_SEPARATOR = ";"; 28 | 29 | private static final String HOST_PORT_SEPARATOR = ":"; 30 | 31 | private ShardedJedisPool shardedJedisPool = null; 32 | 33 | private static final ShardedRedisUtil INSTANCE = new ShardedRedisUtil(); 34 | 35 | private ShardedRedisUtil() { 36 | initialShardedPool(); 37 | } 38 | 39 | private void initialShardedPool() { 40 | // 操作超时时间,默认2秒 41 | int timeout = NumberUtils.toInt(SharedRedisConfig.getConfigProperty("redis.timeout"), 2000); 42 | // jedis池最大连接数总数,默认8 43 | int maxTotal = NumberUtils.toInt(SharedRedisConfig.getConfigProperty("redis.jedisPoolConfig.maxTotal"), 8); 44 | // jedis池最大空闲连接数,默认8 45 | int maxIdle = NumberUtils.toInt(SharedRedisConfig.getConfigProperty("redis.jedisPoolConfig.maxIdle"), 8); 46 | // jedis池最少空闲连接数 47 | int minIdle = NumberUtils.toInt(SharedRedisConfig.getConfigProperty("redis.jedisPoolConfig.minIdle"), 0); 48 | // jedis池没有对象返回时,最大等待时间单位为毫秒 49 | long maxWaitMillis = NumberUtils.toLong(SharedRedisConfig.getConfigProperty("redis.jedisPoolConfig.maxWaitTime"), -1); 50 | // 在borrow一个jedis实例时,是否提前进行validate操作 51 | boolean testOnBorrow = Boolean.parseBoolean(SharedRedisConfig.getConfigProperty("redis.jedisPoolConfig.testOnBorrow")); 52 | 53 | // 设置jedis连接池配置 54 | JedisPoolConfig poolConfig = new JedisPoolConfig(); 55 | poolConfig.setMaxTotal(maxTotal); 56 | poolConfig.setMaxIdle(maxIdle); 57 | poolConfig.setMinIdle(minIdle); 58 | poolConfig.setMaxWaitMillis(maxWaitMillis); 59 | poolConfig.setTestOnBorrow(testOnBorrow); 60 | 61 | // 取得redis的url 62 | String redisUrls = SharedRedisConfig.getConfigProperty("redis.jedisPoolConfig.urls"); 63 | if (redisUrls == null || redisUrls.trim().isEmpty()) { 64 | throw new IllegalStateException("the urls of redis is not configured"); 65 | } 66 | LOGGER.info("the urls of redis is {}", redisUrls); 67 | 68 | // 生成连接池 69 | List shardedPoolList = new ArrayList(); 70 | for (String redisUrl : redisUrls.split(DEFAULT_REDIS_SEPARATOR)) { 71 | String[] redisUrlInfo = redisUrl.split(HOST_PORT_SEPARATOR); 72 | JedisShardInfo Jedisinfo = new JedisShardInfo(redisUrlInfo[0], Integer.parseInt(redisUrlInfo[1]), timeout); 73 | shardedPoolList.add(Jedisinfo); 74 | } 75 | 76 | // 构造池 77 | this.shardedJedisPool = new ShardedJedisPool(poolConfig, shardedPoolList, Hashing.MURMUR_HASH); 78 | } 79 | 80 | public static ShardedRedisUtil getInstance() { 81 | return INSTANCE; 82 | } 83 | 84 | /** 85 | * 实现jedis连接的获取和释放,具体的redis业务逻辑由executor实现 86 | * 87 | * @param executor RedisExecutor接口的实现类 88 | * @return 89 | */ 90 | public T execute(String key, ShardedRedisExecutor executor) { 91 | ShardedJedis jedis = shardedJedisPool.getResource(); 92 | T result = null; 93 | try { 94 | result = executor.execute(jedis); 95 | } finally { 96 | if (jedis != null) { 97 | jedis.close(); 98 | } 99 | } 100 | return result; 101 | } 102 | 103 | public String set(final String key, final String value) { 104 | return execute(key, new ShardedRedisExecutor() { 105 | @Override 106 | public String execute(ShardedJedis jedis) { 107 | return jedis.set(key, value); 108 | } 109 | }); 110 | } 111 | 112 | public String set(final String key, final String value, final String nxxx, final String expx, final long time) { 113 | return execute(key, new ShardedRedisExecutor() { 114 | @Override 115 | public String execute(ShardedJedis jedis) { 116 | return jedis.set(key, value, nxxx, expx, time); 117 | } 118 | }); 119 | } 120 | 121 | public String get(final String key) { 122 | return execute(key, new ShardedRedisExecutor() { 123 | @Override 124 | public String execute(ShardedJedis jedis) { 125 | return jedis.get(key); 126 | } 127 | }); 128 | } 129 | 130 | public Boolean exists(final String key) { 131 | return execute(key, new ShardedRedisExecutor() { 132 | @Override 133 | public Boolean execute(ShardedJedis jedis) { 134 | return jedis.exists(key); 135 | } 136 | }); 137 | } 138 | 139 | public Long setnx(final String key, final String value) { 140 | return execute(key, new ShardedRedisExecutor() { 141 | @Override 142 | public Long execute(ShardedJedis jedis) { 143 | return jedis.setnx(key, value); 144 | } 145 | }); 146 | } 147 | 148 | public String setex(final String key, final int seconds, final String value) { 149 | return execute(key, new ShardedRedisExecutor() { 150 | @Override 151 | public String execute(ShardedJedis jedis) { 152 | return jedis.setex(key, seconds, value); 153 | } 154 | }); 155 | } 156 | 157 | public Long expire(final String key, final int seconds) { 158 | return execute(key, new ShardedRedisExecutor() { 159 | @Override 160 | public Long execute(ShardedJedis jedis) { 161 | return jedis.expire(key, seconds); 162 | } 163 | }); 164 | } 165 | 166 | public Long incr(final String key) { 167 | return execute(key, new ShardedRedisExecutor() { 168 | @Override 169 | public Long execute(ShardedJedis jedis) { 170 | return jedis.incr(key); 171 | } 172 | }); 173 | } 174 | 175 | public Long decr(final String key) { 176 | return execute(key, new ShardedRedisExecutor() { 177 | @Override 178 | public Long execute(ShardedJedis jedis) { 179 | return jedis.decr(key); 180 | } 181 | }); 182 | } 183 | 184 | public Long hset(final String key, final String field, final String value) { 185 | return execute(key, new ShardedRedisExecutor() { 186 | @Override 187 | public Long execute(ShardedJedis jedis) { 188 | return jedis.hset(key, field, value); 189 | } 190 | }); 191 | } 192 | 193 | public String hget(final String key, final String field) { 194 | return execute(key, new ShardedRedisExecutor() { 195 | @Override 196 | public String execute(ShardedJedis jedis) { 197 | return jedis.hget(key, field); 198 | } 199 | }); 200 | } 201 | 202 | public String hmset(final String key, final Map hash) { 203 | return execute(key, new ShardedRedisExecutor() { 204 | @Override 205 | public String execute(ShardedJedis jedis) { 206 | return jedis.hmset(key, hash); 207 | } 208 | }); 209 | } 210 | 211 | public List hmget(final String key, final String... fields) { 212 | return execute(key, new ShardedRedisExecutor>() { 213 | @Override 214 | public List execute(ShardedJedis jedis) { 215 | return jedis.hmget(key, fields); 216 | } 217 | }); 218 | } 219 | 220 | public Long del(final String key) { 221 | return execute(key, new ShardedRedisExecutor() { 222 | @Override 223 | public Long execute(ShardedJedis jedis) { 224 | return jedis.del(key); 225 | } 226 | }); 227 | } 228 | 229 | public Map hgetAll(final String key) { 230 | return execute(key, new ShardedRedisExecutor>() { 231 | @Override 232 | public Map execute(ShardedJedis jedis) { 233 | return jedis.hgetAll(key); 234 | } 235 | }); 236 | } 237 | 238 | public void destroy() { 239 | this.shardedJedisPool.close(); 240 | } 241 | } 242 | -------------------------------------------------------------------------------- /src/main/java/com/zzy/util/sharded/SharedRedisConfig.java: -------------------------------------------------------------------------------- 1 | package com.zzy.util.sharded; 2 | 3 | import java.util.ResourceBundle; 4 | 5 | /** 6 | * 读取redis配置 7 | * 8 | * @author zhengzhiyuan 9 | * @since May 20, 2016 10 | */ 11 | public class SharedRedisConfig { 12 | private static final String DEFAULT_REDIS_PROPERTIES = "shardedRedis"; 13 | private static ResourceBundle REDIS_CONFIG = ResourceBundle.getBundle(DEFAULT_REDIS_PROPERTIES); 14 | 15 | public static String getConfigProperty(String key) { 16 | return REDIS_CONFIG.getString(key); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/resources/hashRedis.properties: -------------------------------------------------------------------------------- 1 | # 操作超时时间,默认2秒 2 | redis.timeout=3000 3 | 4 | # redis url接口以";"分割多个地址 5 | redis.jedisPoolConfig.urls=pbasadvw02.rmz.gomo.com:8878 6 | 7 | # jedis池最大连接数总数,默认8 8 | redis.jedisPoolConfig.maxTotal=8 9 | 10 | # jedis池最大空闲连接数,默认8 11 | redis.jedisPoolConfig.maxIdle=8 12 | 13 | #jedis池最少空闲连接数 14 | redis.jedisPoolConfig.minIdle=3 15 | 16 | # jedis池没有对象返回时,最大等待时间单位为毫秒 17 | redis.jedisPoolConfig.maxWaitTime=60000 18 | 19 | # 在borrow一个jedis实例时,是否提前进行validate操作 20 | redis.jedisPoolConfig.testOnBorrow=true 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | UTF-8 6 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/main/resources/shardedRedis.properties: -------------------------------------------------------------------------------- 1 | # 操作超时时间,默认2秒 2 | redis.timeout=3000 3 | 4 | # redis url接口以";"分割多个地址 5 | redis.jedisPoolConfig.urls=pbasadvw02.rmz.gomo.com:8878 6 | 7 | # jedis池最大连接数总数,默认8 8 | redis.jedisPoolConfig.maxTotal=8 9 | 10 | # jedis池最大空闲连接数,默认8 11 | redis.jedisPoolConfig.maxIdle=8 12 | 13 | #jedis池最少空闲连接数 14 | redis.jedisPoolConfig.minIdle=3 15 | 16 | # jedis池没有对象返回时,最大等待时间单位为毫秒 17 | redis.jedisPoolConfig.maxWaitTime=60000 18 | 19 | # 在borrow一个jedis实例时,是否提前进行validate操作 20 | redis.jedisPoolConfig.testOnBorrow=true 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/test/java/com/zzy/util/HashRedisUtilTest.java: -------------------------------------------------------------------------------- 1 | package com.zzy.util; 2 | 3 | import org.junit.Test; 4 | 5 | import com.zzy.util.hash.HashRedisUtil; 6 | 7 | public class HashRedisUtilTest { 8 | @Test 9 | public void test() { 10 | HashRedisUtil redisUtil = HashRedisUtil.getInstance(); 11 | System.out.println(redisUtil.set("keyTest","valueTest")); 12 | System.out.println(redisUtil.get("keyTest")); 13 | redisUtil.destroy(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/zzy/util/ShardedRedisUtilTest.java: -------------------------------------------------------------------------------- 1 | package com.zzy.util; 2 | 3 | import org.junit.Test; 4 | 5 | import com.zzy.util.sharded.ShardedRedisUtil; 6 | 7 | public class ShardedRedisUtilTest { 8 | @Test 9 | public void test() { 10 | ShardedRedisUtil redisUtil = ShardedRedisUtil.getInstance(); 11 | System.out.println(redisUtil.get("keyTest")); 12 | System.out.println(redisUtil.set("keyTest","valueTest2")); 13 | System.out.println(redisUtil.get("keyTest")); 14 | redisUtil.destroy(); 15 | } 16 | } 17 | --------------------------------------------------------------------------------