java操作redis
一、添加依赖
<!--redis--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.2</version> </dependency>
二、配置yml文件
# redis相关配置
jedis :
pool :
# 线上库
host : ip
port : 6379
config :
maxTotal: 100
maxIdle: 10
maxWaitMillis : 100000
三、添加配置文件
package com.***.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; @Configuration public class RedisConfiguration { @Bean(name = "jedis.pool") @Autowired public JedisPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config, @Value("${jedis.pool.host}") String host, @Value("${jedis.pool.port}") int port) { return new JedisPool(config, host, port); } @Bean(name = "jedis.pool.config") public JedisPoolConfig jedisPoolConfig(@Value("${jedis.pool.config.maxTotal}") int maxTotal, @Value("${jedis.pool.config.maxIdle}") int maxIdle, @Value("${jedis.pool.config.maxWaitMillis}") int maxWaitMillis) { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(maxTotal); config.setMaxIdle(maxIdle); config.setMaxWaitMillis(maxWaitMillis); return config; } }
四、创建redis工具类
package com.***.utils; import com.google.common.collect.Lists; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import java.util.Iterator; import java.util.List; import java.util.Set; @Component public class RedisClientUtil { @Autowired private JedisPool jedisPool; /** * 获取Jedis实例 * * @return */ public Jedis getJedis() { return jedisPool.getResource(); } /** * 存放数据到redis * * @param key * @param value * @return */ public boolean set(String key, String value) { Jedis jedis = null; try { jedis = getJedis(); jedis.set(key, value); return true; } catch (Exception e) { e.printStackTrace(); } finally { //返还到连接池 returnResource(jedis); } return false; } /** * 存放数据到redis,并设置缓存时间 * * @param key * @param value * @param time 过期时间(秒) * @return */ public boolean set(String key, String value, Integer time) { Jedis jedis = null; try { jedis = getJedis(); jedis.set(key, value); if (time > 0) { jedis.expire(key, time); } return true; } catch (Exception e) { e.printStackTrace(); } finally { //返还到连接池 returnResource(jedis); } return false; } /** * 判断某个key是否存在 * * @param key * @return */ public boolean exist(String key) { Jedis jedis = null; try { jedis = getJedis(); return jedis.exists(key); } catch (Exception e) { e.printStackTrace(); } finally { // 返还到连接池 returnResource(jedis); } return false; } /** * 以key删除某个数据 * * @param key * @return */ public Long del(String key) { Jedis jedis = null; try { jedis = getJedis(); return jedis.del(key); } catch (Exception e) { e.printStackTrace(); return null; } finally { // 返还到连接池 returnResource(jedis); } } /** * 通过key获取value * * @param key * @return */ public String get(String key) { String value = null; Jedis jedis = null; try { jedis = getJedis(); value = jedis.get(key); } catch (Exception e) { e.printStackTrace(); } finally { // 返还到连接池 returnResource(jedis); } return value; } /** * 将jedis返还到连接池 * * @param jedis */ public void returnResource(Jedis jedis) { if (jedis != null) { jedisPool.returnResource(jedis); } } /** * 在名称为key的list尾添加一个值为value的元素 * * @param key * @param value * @return */ public <T> boolean rpush(String key, T value) { Jedis jedis = null; try { jedis = getJedis(); jedis.rpush(key.getBytes(), SerializeUtil.serialize(value)); return true; } catch (Exception e) { e.printStackTrace(); } finally { //返还到连接池 returnResource(jedis); } return false; } /** * 在名称为key的list头添加一个值为value的元素 * * @param key * @param value * @return */ public boolean lpush(byte[] key, byte[] value) { Jedis jedis = null; try { jedis = getJedis(); jedis.lpush(key, value); return true; } catch (Exception e) { e.printStackTrace(); } finally { //返还到连接池 returnResource(jedis); } return false; } /** * 返回名称为key的list中start至end之间的元素(下标从0开始,下同) * * @param key * @param start * @param end * @return */ public <T> List<T> lrange(byte[] key, Integer start, Integer end) { Jedis jedis = null; List<T> t = Lists.newArrayList(); try { jedis = getJedis(); List<byte[]> list = jedis.lrange(key, start, end); list.forEach(bytes -> { Object o = SerializeUtil.unserialize(bytes); if (o!=null){ t.add((T)o); } }); } catch (Exception e) { e.printStackTrace(); } finally { //返还到连接池 returnResource(jedis); } return t; } public Long llen(String key) { Jedis jedis = null; try { jedis = getJedis(); return jedis.llen(key); } catch (Exception e) { e.printStackTrace(); } finally { //返还到连接池 returnResource(jedis); } return 0l; } public boolean setObject(String key, Object value) { Jedis jedis = null; try { jedis = getJedis(); byte[] bytes = SerializeUtil.serialize(value); jedis.set(key.getBytes(), bytes); return true; } catch (Exception e) { e.printStackTrace(); } finally { //返还到连接池 returnResource(jedis); } return false; } /** * 存放数据 * * @param key 存储的key * @param value 需要存储的数据 * @param express key失效时间 * @return */ public <T> boolean setObject(String key, T value, int express) { Jedis jedis = null; try { jedis = getJedis(); byte[] bytes = SerializeUtil.serialize(value); jedis.set(key.getBytes(), bytes); jedis.expire(key, express); return true; } catch (Exception e) { e.printStackTrace(); } finally { //返还到连接池 returnResource(jedis); } return false; } /** * 删除key集合 */ public <T> boolean delKeys(List<String> keys) { Jedis jedis = null; try { jedis = getJedis(); for (String key : keys) { jedis.del(key.getBytes()); } return true; } catch (Exception e) { e.printStackTrace(); } finally { //返还到连接池 returnResource(jedis); } return false; } /** * 获取数据 * * @param key 存储的key * @return */ public <T> T getObject(String key) { Object value = null; Jedis jedis = null; try { jedis = getJedis(); byte[] bytes = jedis.get(key.getBytes()); value = SerializeUtil.unserialize(bytes); } catch (Exception e) { e.printStackTrace(); } finally { // 返还到连接池 returnResource(jedis); } if (value != null) { return (T) value; } return null; } /** * 将key的时间置为0,即清除缓存 * * @param key 将key的时间置为0,即清除缓存 */ public void expire(String key) { Jedis jedis = null; try { jedis = getJedis(); jedis.expire(key, 0); } catch (Exception e) { e.printStackTrace(); } finally { // 返还到连接池 returnResource(jedis); } } /** * 删除以某字符串为前缀的key集合 */ public <T> boolean delKeysMatch(String keyMatch) { Jedis jedis = null; try { jedis = getJedis(); Set<String> keys = jedis.keys(keyMatch + "*"); Iterator<String> it = keys.iterator(); while (it.hasNext()) { String keyStr = it.next(); jedis.del(keyStr); } return true; } catch (Exception e) { e.printStackTrace(); } finally { //返还到连接池 returnResource(jedis); } return false; } }
需要的序列化工具类
package com.***.utils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerializeUtil { //序列化 public 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) { } return null; } //反序列化 public static Object unserialize(byte[] bytes) { ByteArrayInputStream bais = null; try { bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception e) { } return null; } }