赞
踩
1:添加maven依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-redis</artifactId>
- <version>1.4.7.RELEASE</version>
- </dependency>
2:在application.yml中配置redis的连接参数
- spring:
- redis:
- database: 0
- hostName: 127.0.0.1
- port: 6379
- timeout: 0
- password:
- pool:
- maxTotal: 200
- maxWaitMillis: 10000
- maxIdle: 50
- minIdle: 10
- testOnBorrow: true
3:创建redis的配置类RedisConfig.java
-
- @Configuration
- @EnableCaching
- public class RedisConfig extends CachingConfigurerSupport {
-
- @Value("${spring.redis.database}")
- private int database;
-
- @Value("${spring.redis.hostName}")
- private String hostName;
-
- @Value("${spring.redis.port}")
- private int port;
-
- @Value("${spring.redis.password}")
- private String password;
-
- @Value("${spring.redis.timeout}")
- private int timeout;
-
- @Value("${spring.redis.pool.maxIdle}")
- private int maxIdle;
-
- @Value("${spring.redis.pool.minIdle}")
- private int minIdle;
-
-
- @Value("${spring.redis.pool.maxTotal}")
- private int maxTotal;
-
- @Value("${spring.redis.pool.maxWaitMillis}")
- private long maxWaitMillis;
-
- @Value("${spring.redis.pool.testOnBorrow}")
- private boolean testOnBorrow;
-
- public JedisPoolConfig poolCofig(int maxIdle, int maxTotal,
- long maxWaitMillis, boolean testOnBorrow) {
- JedisPoolConfig poolCofig = new JedisPoolConfig();
- poolCofig.setMaxIdle(maxIdle);
- poolCofig.setMaxIdle(minIdle);
- poolCofig.setMaxTotal(maxTotal);
- poolCofig.setMaxWaitMillis(maxWaitMillis);
- poolCofig.setTestOnBorrow(testOnBorrow);
- return poolCofig;
- }
-
- @Bean
- public JedisConnectionFactory redisConnectionFactory() {
- JedisConnectionFactory factory = new JedisConnectionFactory();
- factory.setDatabase(database);
- factory.setHostName(hostName);
- factory.setPort(port);
- factory.setPassword(password);
- factory.setTimeout(timeout);
-
- factory.setPoolConfig(poolCofig(maxIdle, maxTotal, maxWaitMillis, testOnBorrow));
- // 初始化连接pool
- factory.afterPropertiesSet();
-
- return factory;
- }
-
- @Bean
- @Override
- public KeyGenerator keyGenerator() {
- return (target, method, objects) -> {
- StringBuilder sb = new StringBuilder();
- sb.append(target.getClass().getName());
- sb.append("::" + method.getName() + ":");
- for (Object obj : objects) {
- sb.append(obj.toString());
- }
- return sb.toString();
- };
- }
-
- // @Bean
- // public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
- // return new RedisCacheManager(redisTemplate);
- // }
-
- @Bean
- public RedisTemplate<String, String> redisTemplate(
- RedisConnectionFactory factory) {
- StringRedisTemplate template = new StringRedisTemplate(factory);
- Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
- ObjectMapper om = new ObjectMapper();
- //om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
- om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
- jackson2JsonRedisSerializer.setObjectMapper(om);
- template.setValueSerializer(jackson2JsonRedisSerializer);
- template.afterPropertiesSet();
- return template;
- }
-
- @Bean
- public JedisPool getJedisPool(){
- JedisPoolConfig config = new JedisPoolConfig();
- config.setMaxTotal(maxTotal);
- config.setMaxIdle(maxIdle);
- config.setMinIdle(minIdle);
- config.setTestOnBorrow(testOnBorrow);
- config.setTestOnReturn(false);
- config.setBlockWhenExhausted(true);
- config.setMaxWaitMillis(maxWaitMillis);
- JedisPool pool = new JedisPool(config,hostName,port,timeout,password);
- return pool;
- }
- }
4:创建redis操作服务类RedisService.java
- @Component
- public class RedisService {
-
- @Autowired
- private RedisTemplate redisTemplate;
-
- @Autowired
- private JedisPool jedisPool;
-
-
- public Jedis getResource() {
- return jedisPool.getResource();
- }
-
- public void returnResource(Jedis jedis) {
- if (jedis != null) {
- jedisPool.returnResourceObject(jedis);
- }
- }
-
- //=============================common============================
-
- /**
- * 指定缓存失效时间
- *
- * @param key 键
- * @param time 时间(秒)
- * @return
- */
- public boolean expire(String key, long time) {
- try {
- if (time > 0) {
- redisTemplate.expire(key, time, TimeUnit.SECONDS);
- }
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
-
- /**
- * 根据key 获取过期时间
- *
- * @param key 键 不能为null
- * @return 时间(秒) 返回0代表为永久有效
- */
- public long getExpire(String key) {
- return redisTemplate.getExpire(key, TimeUnit.SECONDS);
- }
-
- /**
- * 判断key是否存在
- *
- * @param key 键
- * @return true 存在 false不存在
- */
- public boolean hasKey(String key) {
- try {
- return redisTemplate.hasKey(key);
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
-
- /**
- * 删除缓存
- *
- * @param key 可以传一个值 或多个
- */
- @SuppressWarnings("unchecked")
- public void del(String... key) {
- if (key != null && key.length > 0) {
- if (key.length == 1) {
- redisTemplate.delete(key[0]);
- } else {
- redisTemplate.delete(CollectionUtils.arrayToList(key));
- }
- }
- }
-
- //============================String=============================
-
- /**
- * 普通缓存获取
- *
- * @param key 键
- * @return 值
- */
- public Object get(String key) {
- return key == null ? null : redisTemplate.opsForValue().get(key);
- }
-
- /**
- * 普通缓存放入
- *
- * @param key 键
- * @param value 值
- * @return true成功 false失败
- */
- public boolean set(String key, Object value) {
- try {
- redisTemplate.opsForValue().set(key, value);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
-
- /**
- * 普通缓存放入并设置时间
- *
- * @param key 键
- * @param value 值
- * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
- * @return true成功 false 失败
- */
- public boolean set(String key, Object value, long time) {
- try {
- if (time > 0) {
- redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
- } else {
- set(key, value);
- }
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
-
- /**
- * 递增
- *
- * @param key 键
- * @param delta 要增加几(大于0)
- * @return
- */
- public long incr(String key, long delta) {
- if (delta < 0) {
- throw new RuntimeException("递增因子必须大于0");
- }
- return redisTemplate.opsForValue().increment(key, delta);
- }
-
- /**
- * 递减
- *
- * @param key 键
- * @param delta 要减少几(小于0)
- * @return
- */
- public long decr(String key, long delta) {
- if (delta < 0) {
- throw new RuntimeException("递减因子必须大于0");
- }
- return redisTemplate.opsForValue().increment(key, -delta);
- }
-
- //================================Map=================================
-
- /**
- * HashGet
- *
- * @param key 键 不能为null
- * @param item 项 不能为null
- * @return 值
- */
- public Object hget(String key, String item) {
- return redisTemplate.opsForHash().get(key, item);
- }
-
- /**
- * 获取hashKey对应的所有键值
- *
- * @param key 键
- * @return 对应的多个键值
- */
- public Map<Object, Object> hmget(String key) {
- return redisTemplate.opsForHash().entries(key);
- }
-
- /**
- * HashSet
- *
- * @param key 键
- * @param map 对应多个键值
- * @return true 成功 false 失败
- */
- public boolean hmset(String key, Map<String, Object> map) {
- try {
- redisTemplate.opsForHash().putAll(key, map);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
-
- /**
- * HashSet 并设置时间
- *
- * @param key 键
- * @param map 对应多个键值
- * @param time 时间(秒)
- * @return true成功 false失败
- */
- public boolean hmset(String key, Map<String, Object> map, long time) {
- try {
- redisTemplate.opsForHash().putAll(key, map);
- if (time > 0) {
- expire(key, time);
- }
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
-
- /**
- * 向一张hash表中放入数据,如果不存在将创建
- *
- * @param key 键
- * @param item 项
- * @param value 值
- * @return true 成功 false失败
- */
- public boolean hset(String key, String item, Object value) {
- try {
- redisTemplate.opsForHash().put(key, item, value);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
-
- /**
- * 向一张hash表中放入数据,如果不存在将创建
- *
- * @param key 键
- * @param item 项
- * @param value 值
- * @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
- * @return true 成功 false失败
- */
- public boolean hset(String key, String item, Object value, long time) {
- try {
- redisTemplate.opsForHash().put(key, item, value);
- if (time > 0) {
- expire(key, time);
- }
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
-
- /**
- * 删除hash表中的值
- *
- * @param key 键 不能为null
- * @param item 项 可以使多个 不能为null
- */
- public void hdel(String key, Object... item) {
- redisTemplate.opsForHash().delete(key, item);
- }
-
- /**
- * 判断hash表中是否有该项的值
- *
- * @param key 键 不能为null
- * @param item 项 不能为null
- * @return true 存在 false不存在
- */
- public boolean hHasKey(String key, String item) {
- return redisTemplate.opsForHash().hasKey(key, item);
- }
-
- /**
- * hash递增 如果不存在,就会创建一个 并把新增后的值返回
- *
- * @param key 键
- * @param item 项
- * @param by 要增加几(大于0)
- * @return
- */
- public double hincr(String key, String item, double by) {
- return redisTemplate.opsForHash().increment(key, item, by);
- }
-
- /**
- * hash递减
- *
- * @param key 键
- * @param item 项
- * @param by 要减少记(小于0)
- * @return
- */
- public double hdecr(String key, String item, double by) {
- return redisTemplate.opsForHash().increment(key, item, -by);
- }
-
- //============================set=============================
-
- /**
- * 根据key获取Set中的所有值
- *
- * @param key 键
- * @return
- */
- public Set<Object> sGet(String key) {
- try {
- return redisTemplate.opsForSet().members(key);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
-
- /**
- * 根据value从一个set中查询,是否存在
- *
- * @param key 键
- * @param value 值
- * @return true 存在 false不存在
- */
- public boolean sHasKey(String key, Object value) {
- try {
- return redisTemplate.opsForSet().isMember(key, value);
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
-
- /**
- * 将数据放入set缓存
- *
- * @param key 键
- * @param values 值 可以是多个
- * @return 成功个数
- */
- public long sSet(String key, Object... values) {
- try {
- return redisTemplate.opsForSet().add(key, values);
- } catch (Exception e) {
- e.printStackTrace();
- return 0;
- }
- }
-
- /**
- * 将set数据放入缓存
- *
- * @param key 键
- * @param time 时间(秒)
- * @param values 值 可以是多个
- * @return 成功个数
- */
- public long sSetAndTime(String key, long time, Object... values) {
- try {
- Long count = redisTemplate.opsForSet().add(key, values);
- if (time > 0) expire(key, time);
- return count;
- } catch (Exception e) {
- e.printStackTrace();
- return 0;
- }
- }
-
- /**
- * 获取set缓存的长度
- *
- * @param key 键
- * @return
- */
- public long sGetSetSize(String key) {
- try {
- return redisTemplate.opsForSet().size(key);
- } catch (Exception e) {
- e.printStackTrace();
- return 0;
- }
- }
-
- /**
- * 移除值为value的
- *
- * @param key 键
- * @param values 值 可以是多个
- * @return 移除的个数
- */
- public long setRemove(String key, Object... values) {
- try {
- Long count = redisTemplate.opsForSet().remove(key, values);
- return count;
- } catch (Exception e) {
- e.printStackTrace();
- return 0;
- }
- }
- //===============================list=================================
-
- /**
- * 获取list缓存的内容
- *
- * @param key 键
- * @param start 开始
- * @param end 结束 0 到 -1代表所有值
- * @return
- */
- public List<Object> lGet(String key, long start, long end) {
- try {
- return redisTemplate.opsForList().range(key, start, end);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
-
- /**
- * 获取list缓存的长度
- *
- * @param key 键
- * @return
- */
- public long lGetListSize(String key) {
- try {
- return redisTemplate.opsForList().size(key);
- } catch (Exception e) {
- e.printStackTrace();
- return 0;
- }
- }
-
- /**
- * 通过索引 获取list中的值
- *
- * @param key 键
- * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
- * @return
- */
- public Object lGetIndex(String key, long index) {
- try {
- return redisTemplate.opsForList().index(key, index);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
-
- /**
- * 将list放入缓存
- *
- * @param key 键
- * @param value 值
- * @return
- */
- public boolean lSet(String key, Object value) {
- try {
- redisTemplate.opsForList().rightPush(key, value);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
-
- /**
- * 将list放入缓存
- *
- * @param key 键
- * @param value 值
- * @param time 时间(秒)
- * @return
- */
- public boolean lSet(String key, Object value, long time) {
- try {
- redisTemplate.opsForList().rightPush(key, value);
- if (time > 0) expire(key, time);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
-
- /**
- * 将list放入缓存
- *
- * @param key 键
- * @param value 值
- * @return
- */
- public boolean lSet(String key, List<Object> value) {
- try {
- redisTemplate.opsForList().rightPushAll(key, value);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
-
- /**
- * 将list放入缓存
- *
- * @param key 键
- * @param value 值
- * @param time 时间(秒)
- * @return
- */
- public boolean lSet(String key, List<Object> value, long time) {
- try {
- redisTemplate.opsForList().rightPushAll(key, value);
- if (time > 0) expire(key, time);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
-
- /**
- * 根据索引修改list中的某条数据
- *
- * @param key 键
- * @param index 索引
- * @param value 值
- * @return
- */
- public boolean lUpdateIndex(String key, long index, Object value) {
- try {
- redisTemplate.opsForList().set(key, index, value);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
-
- /**
- * 移除N个值为value
- *
- * @param key 键
- * @param count 移除多少个
- * @param value 值
- * @return 移除的个数
- */
- public long lRemove(String key, long count, Object value) {
- try {
- Long remove = redisTemplate.opsForList().remove(key, count, value);
- return remove;
- } catch (Exception e) {
- e.printStackTrace();
- return 0;
- }
- }
-
- //======================================
-
- /**
- * 批量删除对应的value
- *
- * @param keys
- */
- public void remove(final String... keys) {
- for (String key : keys) {
- remove(key);
- }
- }
-
- /**
- * 批量删除key
- *
- * @param pattern
- */
- public void removePattern(final String pattern) {
- Set<Serializable> keys = redisTemplate.keys(pattern);
- if (keys.size() > 0)
- redisTemplate.delete(keys);
- }
-
- /**
- * 删除对应的value
- *
- * @param key
- */
- public void remove(final String key) {
- if (exists(key)) {
- redisTemplate.delete(key);
- }
- }
-
- /**
- * 判断缓存中是否有对应的value
- *
- * @param key
- * @return
- */
- public boolean exists(final String key) {
- return redisTemplate.hasKey(key);
- }
-
- // /**
- // * 读取缓存
- // *
- // * @param key
- // * @return
- // */
- // public Object get(final String key) {
- // Object result = null;
- // ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
- // result = operations.get(key);
- // return result;
- // }
-
- // /**
- // * 写入缓存
- // *
- // * @param key
- // * @param value
- // * @return
- // */
- // public boolean set(final String key, Object value) {
- // boolean result = false;
- // try {
- // ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
- // operations.set(key, value);
- // result = true;
- // } catch (Exception e) {
- // e.printStackTrace();
- // }
- // return result;
- // }
-
- // /**
- // * 写入缓存
- // *
- // * @param key
- // * @param value
- // * @return
- // */
- // public boolean set(final String key, Object value, Long expireTime) {
- // boolean result = false;
- // try {
- // ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
- // operations.set(key, value);
- // redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
- // result = true;
- // } catch (Exception e) {
- // e.printStackTrace();
- // }
- // return result;
- // }
-
- /**
- * 删除单个key
- *
- * @param key
- */
- public void removeMemberSet(String key) {
- redisTemplate.opsForSet().getOperations().delete(key);
- }
-
- /**
- * 判断制定集合value是否存在
- *
- * @param key
- * @return
- */
- public boolean isMemberSet(String key, String value) {
- return redisTemplate.opsForSet().isMember(key, value);
- }
-
- /**
- * 向指定无序集合Set里添加value
- *
- * @param key
- */
- public void addMemberSet(String key, String value) {
- redisTemplate.opsForSet().add(key, value);
- }
-
- /**
- * 删除指定key获得集合里的值
- *
- * @param key
- * @param value
- * @return
- */
- public boolean removeMemberSetValue(String key, String value) {
-
- return redisTemplate.opsForSet().remove(key, value) == 1;
-
- }
-
- public Set<? extends Object> getMeberSetMeber(String key) {
- return redisTemplate.opsForSet().members(key);
- }
-
-
- //分布式锁工具类 start
-
-
- /**
- * 获取key的VALUE
- *
- * @param key
- * @return
- */
- @SuppressWarnings("finally")
- public String getByKey(String key) {
- Jedis jedis = null;
- String result = null;
- try {
- jedis = getResource();
- result = jedis.get(key);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (jedis != null) {
- returnResource(jedis);
- }
- return result;
- }
- }
-
- /**
- * 不存在新增 成功返回1 ,不成功返回0
- *
- * @param key
- * @param value
- * @return
- */
- @SuppressWarnings("finally")
- public Long setnxByKey(String key, String value) {
- Jedis jedis = null;
- Long result = null;
- try {
- jedis = getResource();
- result = jedis.setnx(key, value);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (jedis != null) {
- returnResource(jedis);
- }
- return result;
- }
- }
-
- /**
- * 获取老版本的旧值
- *
- * @param key
- * @param value
- * @return
- */
- @SuppressWarnings("finally")
- public String getSetByKey(String key, String value) {
- Jedis jedis = null;
- String result = null;
- try {
- jedis = getResource();
- result = jedis.getSet(key, value);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (jedis != null) {
- returnResource(jedis);
- }
- return result;
- }
- }
-
- /**
- * 制定KEY过期
- *
- * @param key
- * @param seconds
- * @return
- */
- @SuppressWarnings("finally")
- public Long expireByKey(String key, int seconds) {
- Jedis jedis = null;
- Long result = null;
- try {
- jedis = getResource();
- result = jedis.expire(key, seconds);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (jedis != null) {
- returnResource(jedis);
- }
- return result;
- }
- }
-
- /**
- * 删除制定KEY的VALUE
- *
- * @param key
- * @return
- */
- @SuppressWarnings("finally")
- public Long delByKey(String key) {
- Jedis jedis = null;
- Long result = null;
- try {
- jedis = getResource();
- result = jedis.del(key);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (jedis != null) {
- returnResource(jedis);
- }
- return result;
- }
- }
-
- /**
- * 获取分布式锁
- *
- * @param lockName
- * @return
- */
- public boolean lock(String lockName) {//lockName可以为共享变量名,也可以为方法名,主要是用于模拟锁信息
- System.out.println(Thread.currentThread() + "开始尝试加锁!");
- Long result = setnxByKey(lockName, String.valueOf(System.currentTimeMillis() + 10000));
- if (result != null && result.intValue() == 1) {
- System.out.println(Thread.currentThread() + "加锁成功!");
- expireByKey(lockName, 10);
- System.out.println(Thread.currentThread() + "获得锁可以开始执行业务逻辑!");
- // delByKey(lockName); //在业务代码里执行
- return true;
- } else {
- String lockValueA = getByKey(lockName);
- if (lockValueA != null && Long.parseLong(lockValueA) >= System.currentTimeMillis()) {
- String lockValueB = getSetByKey(lockName, String.valueOf(System.currentTimeMillis() + 10000));
- if (lockValueB == null || lockValueB.equals(lockValueA)) {
- System.out.println(Thread.currentThread() + "加锁成功!");
- expireByKey(lockName, 10);
- System.out.println(Thread.currentThread() + "获得锁可以开始执行业务逻辑!");
- // delByKey(lockName); //在业务代码里执行
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- }
- //分布式锁工具类 end
- }
5:写测试类来测试
- @RunWith(SpringRunner.class)
-
- @SpringBootTest
-
- public class NanawalletServerApplicationTests {
-
- @Autowired
- private RedisService redisService;
- @Test
- public void contextLoads() {
- }
- @Test
- public void testInsertRedis(){
- this.redisService.set("test_redis","123456");
- System.out.println( this.redisService.get("test_redis"));
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。