springboot中自己封装jedisUtil ,操作redis
1、pom.xml引入
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.1.0</version> </dependency>
2、JedisProperties属性类
@Getter @Setter public class JedisProperties { private String address="redis://localhost:6379/0"; private int maxTotal =20; private int maxIdle=5 ; private int minIdle=5 ; private long maxWailMillis=10000L ; private boolean testOnBorrow=false; private boolean testOnReturn=false; }3、JedisConfig配置类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @EnableConfigurationProperties(JedisProperties.class) public class JedisConfig { @Autowired private JedisProperties jedisProperties; @Bean public JedisUtil jedisUtil(){ JedisUtil jedisUtil=new JedisUtil(); jedisUtil.setJedisProperties(jedisProperties); return jedisUtil; } }
4、JedisUtil 工具bean
import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPool; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import java.io.*; import java.util.LinkedList; import java.util.List; @Slf4j public class JedisUtil { @Getter @Setter //spring容器注入 private JedisProperties jedisProperties; //内部初始化 private ShardedJedisPool shardedJedisPool; @PostConstruct public void init(){ JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(jedisProperties.getMaxTotal()); config.setMaxIdle(jedisProperties.getMaxIdle()); config.setMinIdle(jedisProperties.getMinIdle()); config.setMaxWaitMillis(jedisProperties.getMaxWailMillis()); config.setTestOnBorrow(jedisProperties.isTestOnBorrow()); config.setTestOnReturn(jedisProperties.isTestOnReturn()); // JedisShardInfo List String address =jedisProperties.getAddress(); List<JedisShardInfo> jedisShardInfos = new LinkedList<JedisShardInfo>(); String[] addressArr = address.split(","); for (String addr: addressArr) { JedisShardInfo sharedInfo = new JedisShardInfo(addr); jedisShardInfos.add(sharedInfo); } shardedJedisPool = new ShardedJedisPool(config, jedisShardInfos); } @PreDestroy public void destroy(){ if(shardedJedisPool!=null){ shardedJedisPool.close(); } } private ShardedJedis getInstance(){ return shardedJedisPool.getResource(); } /** * 将对象-->byte[] (由于jedis中不支持直接存储object所以转换成byte[]存入) * * @param object * @return */ private static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { // 序列化 baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); byte[] bytes = baos.toByteArray(); return bytes; } catch (Exception e) { log.error(e.getMessage(), e); } finally { try { oos.close(); baos.close(); } catch (IOException e) { log.error(e.getMessage(), e); } } return null; } /** * 将byte[] -->Object * * @param bytes * @return */ private static Object unserialize(byte[] bytes) { ByteArrayInputStream bais = null; try { // 反序列化 bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception e) { log.error(e.getMessage(), e); } finally { try { bais.close(); } catch (IOException e) { log.error(e.getMessage(), e); } } return null; } /** * GET String * @param key * @return */ public String getStringValue(String key){ String value=null; ShardedJedis client =getInstance(); try { value= client.get(key); }catch (Exception e){ log.error(e.getMessage(),e); }finally { if(client!=null){ client.close(); } } return value; } /** * Get Object * * @param key * @return */ public Object getObjectValue(String key) { Object obj = null; ShardedJedis client = getInstance(); try { byte[] bytes = client.get(key.getBytes()); if (bytes != null && bytes.length > 0) { obj = unserialize(bytes); } } catch (Exception e) { log.error(e.getMessage(), e); } finally { if (client != null) { client.close(); } } return obj; } /** * Set String * * @param key * @param value * @param seconds 存活时间,单位/秒 * @return */ public String setStringValueEx(String key, String value, int seconds) { String result = null; ShardedJedis client = getInstance(); try { result = client.setex(key, seconds, value); } catch (Exception e) { log.error(e.getMessage(), e); } finally { if (client != null) { client.close(); } } return result; } /** * Set Object * * @param key * @param obj * @param seconds 存活时间,单位/秒 */ public String setObjectValueEx(String key, Object obj, int seconds) { String result = null; ShardedJedis client = getInstance(); try { result = client.setex(key.getBytes(), seconds, serialize(obj)); } catch (Exception e) { log.error(e.getMessage(), e); } finally { if (client != null) { client.close(); } } return result; } /** * Delete key * * @param key * @return Integer reply, specifically: * an integer greater than 0 if one or more keys were removed * 0 if none of the specified key existed */ public Long del(String key) { Long result = null; ShardedJedis client = getInstance(); try { result = client.del(key); } catch (Exception e) { log.error(e.getMessage(), e); } finally { if (client != null) { client.close(); } } return result; } /** * 判断 key是否存在 * exists valid * * @param key * @return Boolean reply, true if the key exists, otherwise false */ public boolean exists(String key) { Boolean result = null; ShardedJedis client = getInstance(); try { result = client.exists(key); } catch (Exception e) { log.error(e.getMessage(), e); } finally { if (client != null) { client.close(); } } return result; } /** * 刷新key的有效期 * expire reset * @param key * @param seconds 存活时间,单位/秒 * @return Integer reply, specifically: */ public long expireKey(String key, int seconds) { Long result = null; ShardedJedis client = getInstance(); try { result = client.expire(key, seconds); } catch (Exception e) { log.error(e.getMessage(), e); } finally { if (client != null) { client.close(); } } return result; } }
5、springboot测试类
@RunWith(SpringRunner.class) @SpringBootTest public class JedisSpringTest { @Autowired private JedisUtil jedisUtil; @Test public void test1(){ jedisUtil.setStringValueEx("key1","helloworld",3600); String value =jedisUtil.getStringValue("key1"); System.out.println(value); } @Test public void test2(){ User user=new User(); user.setId(1); user.setBirthday("1999-09-09"); user.setName("张三"); user.setSex(1); user.setRemark("备注"); jedisUtil.setObjectValueEx("zhangsan",user,3600); User zs=(User) jedisUtil.getObjectValue("zhangsan"); System.out.println(zs); } }
Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1