Springboot配置Redis插件解决方案
前提条件:Springboot + gradle-3.5
1.配置Redis所需jar包
//redis工具 compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-redis', version: '2.1.3.RELEASE' compile group: 'org.springframework.session', name: 'spring-session-data-redis', version: '2.1.4.RELEASE'
2.配置Properties相关参数
#设置redis配置 spring.redis.host=127.0.0.1 spring.redis.port=6379
3.新增Redis插件工具类
package com.taoxw.plugins.redis; import java.util.Set; import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Service; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; @Service public class RedisUtil { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private RedisTemplate<String, String> redisTemplate; /** * Redis数据获取 * @param redisKey * @return */ public String getString(String redisKey){ ValueOperations<String, String> ops = redisTemplate.opsForValue(); String redisValue = ops.get(redisKey); return redisValue; } /** * Redis数据获取 * @param redisKey * @return */ public JSONObject getJSONObject(String redisKey){ ValueOperations<String, String> ops = redisTemplate.opsForValue(); String redisValue = ops.get(redisKey); return JSONObject.parseObject(redisValue); } /** * Redis数据获取 * @param redisKey * @return */ public JSONArray getJSONArray(String redisKey){ ValueOperations<String, String> ops = redisTemplate.opsForValue(); String redisValue = ops.get(redisKey); return JSONArray.parseArray(redisValue); } /** * Redis数据缓存 * @param redisKey * @param redisValue */ public void setString(String redisKey, String redisValue){ ValueOperations<String, String> ops = redisTemplate.opsForValue(); ops.set(redisKey, redisValue); logger.info("【Redis数据缓存】字符串格式存入{}={}", redisKey, redisValue); } /** * Redis数据缓存 * @param redisKey * @param redisValue * @param timeout * @param unit */ public void setString(String redisKey, String redisValue, long timeout, TimeUnit unit){ ValueOperations<String, String> ops = redisTemplate.opsForValue(); ops.set(redisKey, redisValue, timeout, unit); logger.info("【Redis数据缓存】字符串格式存入{}={}", redisKey, redisValue); } /** * Redis数据缓存 * @param redisKey * @param redisJSONObject */ public void setJSONObject(String redisKey, JSONObject redisJSONObject){ ValueOperations<String, String> ops = redisTemplate.opsForValue(); ops.set(redisKey, redisJSONObject.toJSONString()); logger.info("【Redis数据缓存】JSON字符串格式存入{}={}", redisKey, redisJSONObject.toJSONString()); } /** * Redis数据缓存 * @param redisKey * @param redisJSONObject * @param timeout * @param unit */ public void setJSONObject(String redisKey, JSONObject redisJSONObject, long timeout, TimeUnit unit){ ValueOperations<String, String> ops = redisTemplate.opsForValue(); ops.set(redisKey, redisJSONObject.toJSONString(), timeout, unit); logger.info("【Redis数据缓存】JSON字符串格式存入{}={}", redisKey, redisJSONObject.toJSONString()); } /** * Redis数据缓存 * @param redisKey * @param redisJSONArray */ public void setJSONArray(String redisKey, JSONArray redisJSONArray){ ValueOperations<String, String> ops = redisTemplate.opsForValue(); ops.set(redisKey, redisJSONArray.toJSONString()); logger.info("【Redis数据缓存】JSON数组格式存入{}={}", redisKey, redisJSONArray.toJSONString()); } /** * Redis数据缓存 * @param redisKey * @param redisJSONArray * @param timeout * @param unit */ public void setJSONArray(String redisKey, JSONArray redisJSONArray, long timeout, TimeUnit unit){ ValueOperations<String, String> ops = redisTemplate.opsForValue(); ops.set(redisKey, redisJSONArray.toJSONString(), timeout, unit); logger.info("【Redis数据缓存】JSON数组格式存入{}={}", redisKey, redisJSONArray.toJSONString()); } /** * Redis数据删除 * @param redisKey */ public void delete(String redisKey){ redisTemplate.delete(redisKey); logger.info("【Redis数据删除】删除Key={}", redisKey); } /** * Redis数据删除 * @param patternKey *taoxw* || taoxw* || *taoxw */ public void deleteByPattern(String patternKey){ Set<String> keys = redisTemplate.keys(patternKey); redisTemplate.delete(keys); logger.info("【Redis数据删除】删除Key匹配规则={}", patternKey); } }
4.单元测试类
package com.taoxw.plugins.redis; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import com.taoxw._StarterTest; public class RedisUtilTest extends _StarterTest{ @Autowired private RedisUtil redisUtil; @Test public void test_setString() { redisUtil.setString("redis_test", "test"); } }