1.項目pom.xml中添加Jedis依賴
1 <dependency> 2 <groupId>redis.clients</groupId> 3 <artifactId>jedis</artifactId> 4 </dependency>
2.項目application.properties中添加Redis配置信息
1 # Redis 相關配置 2 redis.host=127.0.0.1 3 redis.port=6379 4 redis.password=112233 5 redis.timeout=300 6 redis.poolMaxTotal=10 7 redis.poolMaxIdle=10 8 redis.poolMaxWait=3
3.com.xxx.redis包:
①RedisConfig類:讀取配置文件中的Redis配置信息
1 @Component 2 @ConfigurationProperties(prefix="redis") 3 public class RedisConfig { 4 5 private String host; 6 private int port; 7 //秒 8 private int timeout; 9 private String password; 10 private int poolMaxTotal; 11 private int poolMaxIdle; 12 //秒 13 private int poolMaxWait; 14 15 public String getHost() { 16 return host; 17 } 18 19 public void setHost(String host) { 20 this.host = host; 21 } 22 23 public int getPort() { 24 return port; 25 } 26 27 public void setPort(int port) { 28 this.port = port; 29 } 30 31 public int getTimeout() { 32 return timeout; 33 } 34 35 public void setTimeout(int timeout) { 36 this.timeout = timeout; 37 } 38 39 public String getPassword() { 40 return password; 41 } 42 43 public void setPassword(String password) { 44 this.password = password; 45 } 46 47 public int getPoolMaxTotal() { 48 return poolMaxTotal; 49 } 50 51 public void setPoolMaxTotal(int poolMaxTotal) { 52 this.poolMaxTotal = poolMaxTotal; 53 } 54 55 public int getPoolMaxIdle() { 56 return poolMaxIdle; 57 } 58 59 public void setPoolMaxIdle(int poolMaxIdle) { 60 this.poolMaxIdle = poolMaxIdle; 61 } 62 63 public int getPoolMaxWait() { 64 return poolMaxWait; 65 } 66 67 public void setPoolMaxWait(int poolMaxWait) { 68 this.poolMaxWait = poolMaxWait; 69 } 70 }
②RedisPoolFactory類:通過JedisPool的構造方法及配置參數,獲取JedisPool
1 @Service 2 public class RedisPoolFactory { 3 4 @Autowired 5 private RedisConfig redisConfig; 6 7 /** 8 * 獲得的JedisPool注入Spring的容器中 9 * @return 10 */ 11 @Bean 12 public JedisPool jedisPoolFactory() { 13 14 JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); 15 jedisPoolConfig.setMaxTotal(redisConfig.getPoolMaxTotal()); 16 jedisPoolConfig.setMaxIdle(redisConfig.getPoolMaxIdle()); 17 jedisPoolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait() * 1000); 18 19 JedisPool jedisPool = new JedisPool(jedisPoolConfig, redisConfig.getHost(), 20 redisConfig.getPort(), redisConfig.getTimeout(), redisConfig.getPassword(), 0); 21 22 return jedisPool; 23 }
③KeyPrefix類:定義各個對象存儲在Redis中的key的前綴
1 public interface KeyPrefix { 2 3 public int expireSeconds(); 4 5 public String getPrefix(); 6 7 8 }
1 public abstract class BasePrefix implements KeyPrefix{ 2 3 private int expireSeconds; 4 5 private String prefix; 6 7 //0代表永不過期 8 public BasePrefix(String prefix) { 9 this(0,prefix); 10 } 11 12 public BasePrefix(int expireSeconds, String prefix) { 13 this.expireSeconds = expireSeconds; 14 this.prefix = prefix; 15 } 16 17 @Override 18 public int expireSeconds(){ 19 return expireSeconds; 20 } 21 22 23 @Override 24 public String getPrefix(){ 25 String className = getClass().getSimpleName(); 26 return className+":" + prefix; 27 } 28 29 }
1 public class UserKey extends BasePrefix{ 2 3 /** 4 * 父類構造方法 5 * @param prefix 6 */ 7 private UserKey(String prefix) { 8 super(prefix); 9 } 10 11 private UserKey(int expireSeconds, String prefix) { 12 super(expireSeconds, prefix); 13 } 14 15 16 public static UserKey getById = new UserKey(30, "id"); 17 public static UserKey getByName = new UserKey(30, "name"); 18 }
④RedisService類:通過JedisPool獲取Jedis;通過Jedis客戶端處理緩存數據
1 @Service 2 public class RedisService { 3 4 @Autowired 5 private RedisPoolFactory redisPoolFactory; 6 7 /** 8 * 獲取對象 9 * @param key 10 * @param clazz 11 * @param <T> 12 * @return 13 */ 14 public <T> T getRedis(KeyPrefix prefix,String key, Class<T> clazz) { 15 16 JedisPool jedisPool = redisPoolFactory.jedisPoolFactory(); 17 Jedis jedis = null; 18 try{ 19 jedis = jedisPool.getResource(); 20 21 String realKey = prefix.getPrefix() + key; 22 String value = jedis.get(realKey); 23 T realValue = stringToBean(value,clazz); 24 return realValue; 25 }finally{ 26 returnToPool(jedis); 27 } 28 29 } 30 31 public <T> boolean setRedis(KeyPrefix prefix,String key, T value) { 32 33 JedisPool jedisPool = redisPoolFactory.jedisPoolFactory(); 34 Jedis jedis = null; 35 try{ 36 jedis = jedisPool.getResource(); 37 38 String valueStr = beanToString(value); 39 40 String realKey = prefix.getPrefix() + key; 41 42 if(valueStr == null || valueStr.length() <= 0) { 43 return false; 44 } 45 jedis.set(realKey, valueStr); 46 47 return true; 48 }finally{ 49 returnToPool(jedis); 50 } 51 52 } 53 54 /** 55 * 刪除 56 * */ 57 public boolean delete(KeyPrefix prefix, String key) { 58 JedisPool jedisPool = redisPoolFactory.jedisPoolFactory(); 59 Jedis jedis = null; 60 try { 61 jedis = jedisPool.getResource(); 62 //生成真正的key 63 String realKey = prefix.getPrefix() + key; 64 long ret = jedis.del(realKey); 65 return ret > 0; 66 }finally { 67 returnToPool(jedis); 68 } 69 } 70 71 /** 72 * 增加值 73 * */ 74 public <T> Long incr(KeyPrefix prefix, String key) { 75 JedisPool jedisPool = redisPoolFactory.jedisPoolFactory(); 76 Jedis jedis = null; 77 try { 78 jedis = jedisPool.getResource(); 79 //生成真正的key 80 String realKey = prefix.getPrefix() + key; 81 return jedis.incr(realKey); 82 }finally { 83 returnToPool(jedis); 84 } 85 } 86 87 88 private void returnToPool(Jedis jedis){ 89 if(null != jedis){ 90 jedis.close(); 91 } 92 } 93 94 95 public static <T> String beanToString(T value) { 96 if(value == null) { 97 return null; 98 } 99 Class<?> clazz = value.getClass(); 100 if(clazz == int.class || clazz == Integer.class) { 101 return ""+value; 102 }else if(clazz == String.class) { 103 return (String)value; 104 }else if(clazz == long.class || clazz == Long.class) { 105 return ""+value; 106 }else { 107 return JSON.toJSONString(value); 108 } 109 } 110 111 @SuppressWarnings("unchecked") 112 public static <T> T stringToBean(String str, Class<T> clazz) { 113 if(str == null || str.length() <= 0 || clazz == null) { 114 return null; 115 } 116 if(clazz == int.class || clazz == Integer.class) { 117 return (T)Integer.valueOf(str); 118 }else if(clazz == String.class) { 119 return (T)str; 120 }else if(clazz == long.class || clazz == Long.class) { 121 return (T)Long.valueOf(str); 122 }else { 123 return JSON.toJavaObject(JSON.parseObject(str), clazz); 124 } 125 } 126 }
4. 控制層:連接Redis服務器,測試
1 /**
* 獲取用戶
3 * 測試Redis服務 4 * @return 5 */ 6 @RequestMapping("/testRedisGet") 7 @ResponseBody 8 public Result<User> testRedis(@RequestParam("id") int id){ 9 10 User user = redisService.getRedis(UserKey.getById,id + "", User.class); 11 12 return Result.success(user); 13 } 14 15 //寫入Redis 16 @RequestMapping("/testRedisSet") 17 @ResponseBody 18 public Result<User> testRedisSet(@RequestParam("id") int id, 19 @RequestParam("name") String name){ 20 21 User user = new User(); 22 user.setId(id); 23 user.setName(name); 24 redisService.setRedis(UserKey.getById,id + "", user); 25 26 return Result.success(user); 27 }