赞
踩
日常开发工作中,用到缓存Redis,整理了一个工具类,供大家参考使用
工具类如下:
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.dao.DataAccessException;
- import org.springframework.data.redis.RedisConnectionFailureException;
- import org.springframework.data.redis.connection.RedisConnection;
- import org.springframework.data.redis.core.*;
-
- import java.util.*;
- import java.util.concurrent.TimeUnit;
-
- /**
- * @author user
- */
- @RequiredArgsConstructor
- @Slf4j
- public class RedisUtils {
-
- public final static int RETRY_CNT = 10;
-
- public final RedisTemplate<String, Object> redisTemplate;
-
- public Boolean hasKey(String key) {
- return retryHasKey(key, 0);
- }
-
- public void saveObject(String key, Object value) {
- retrySaveObject(key, value, 0);
- }
-
- public void saveHash(String key, String hashKey, Object value) {
- retrySaveHash(key, hashKey, value, 0);
- }
-
- public void addSetObject(String key, Object value) {
- retrySetAddObject(key, value, 0);
- }
-
- public Set<Object> getSetObject(String key) {
- return retrySetGetObject(key, 0);
- }
-
- public <T> List<T> getMultiObjects(Collection<String> keys) {
- return retryMultiGet(keys, 0);
- }
-
- public <T> List<T> getHashMultiValues(String key, List<Object> hashkeys) {
- return retryGetHashValues(key, hashkeys, 0);
- }
-
- public void saveTimeOut(String key, Object value, long timeout, TimeUnit unit) {
- retrySaveObjectTimeOut(key, value, timeout, unit, 0);
- }
-
- public void saveTimeOut(String key, Object value, int timeout) {
- retrySaveObjectTimeOut(key, value, timeout, TimeUnit.MINUTES, 0);
- }
-
- public void removeSetObject(String key, Object value) {
- retrySetRemoveObject(key, value, 0);
- }
-
- public Long getSetSize(String key) {
- return retryGetSetSize(key, 0);
- }
-
- public Map<Object, Object> getHashEntries(String key) {
- return retryGetHashEntries(key, 0);
- }
-
- public <T> T getHashValue(String key, Object hashKey) {
- return retryGetHashValue(key, hashKey, 0);
- }
-
- public Long IncrementHashValue(String key, Object hashKey, long delta) {
- return retryIncrementHashValue(key, hashKey, delta, 0);
- }
-
- public Set<String> getPatternkeys(String pattern) {
- return redisTemplate.execute(new RedisCallback<Set<String>>() {
- @Override
- public Set<String> doInRedis(RedisConnection connection) throws DataAccessException {
- Set<String> keys = new HashSet<>();
- ScanOptions.ScanOptionsBuilder scanOptionsBuilder = ScanOptions.scanOptions();
- scanOptionsBuilder.match(pattern);
- Cursor<byte[]> cursor = connection.scan(scanOptionsBuilder.build());
- while (cursor.hasNext()) {
- keys.add(new String(cursor.next()));
- }
- return keys;
- }
-
- });
- }
-
- private Long retryIncrementHashValue(String key, Object hashKey, long delta, int cnt) {
- try {
- return redisTemplate.opsForHash().increment(key, hashKey, delta);
- } catch (RedisConnectionFailureException ex) {
- RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
- log.error("redis 操作异常,当前第" + cnt + "次", ex);
- }
- if (cnt > RETRY_CNT) {
- throw new RuntimeException(
- "retryIncrementHashValues(String key, Object hashKey)方法没有成功,key is " + key + " hashKey is " + hashKey);
- }
- return retryIncrementHashValue(key, hashKey, delta, cnt + 1);
- }
-
- private <T> List<T> retryGetHashValues(String key, List<Object> hashKeys, int cnt) {
- try {
- List<Object> value = redisTemplate.opsForHash().multiGet(key, hashKeys);
- return (List<T>) value;
- } catch (RedisConnectionFailureException ex) {
- RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
- log.error("redis 操作异常,当前第" + cnt + "次", ex);
- }
- if (cnt > RETRY_CNT) {
- throw new RuntimeException(
- "retryGetHashValues(String key, List<Object> hashKeys)方法没有成功,key is " + key + " hashKeys is " + hashKeys);
- }
- return retryGetHashValues(key, hashKeys, cnt + 1);
- }
-
- private <T> T retryGetHashValue(String key, Object hashKey, int cnt) {
- try {
- Object value = redisTemplate.opsForHash().get(key, hashKey);
- if (value != null) {
- return (T) value;
- } else {
- return null;
- }
- } catch (RedisConnectionFailureException ex) {
- RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
- log.error("redis 操作异常,当前第" + cnt + "次", ex);
- }
- if (cnt > RETRY_CNT) {
- throw new RuntimeException(
- "retryGetHashValue(String key, Object hashKey)方法没有成功,key is " + key + " hashKey is " + hashKey);
- }
- return retryGetHashValue(key, hashKey, cnt + 1);
- }
-
- private Map<Object, Object> retryGetHashEntries(String key, int cnt) {
- try {
- return redisTemplate.opsForHash().entries(key);
- } catch (RedisConnectionFailureException ex) {
- RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
- log.error("redis 操作异常,当前第" + cnt + "次", ex);
- }
- if (cnt > RETRY_CNT) {
- throw new RuntimeException("getHashEntries(String key)方法没有成功,key is " + key);
- }
- return retryGetHashEntries(key, cnt + 1);
- }
-
- public <T> T getObject(String key) {
- return retryGetObject(key, 0);
- }
-
- private <T> T retryGetObject(String key, int cnt) {
- try {
- Object value = redisTemplate.opsForValue().get(key);
- if (value != null) {
- return (T) value;
- } else {
- return null;
- }
- } catch (RedisConnectionFailureException ex) {
- RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
- log.error("redis 操作异常,当前第" + cnt + "次", ex);
- }
- if (cnt > RETRY_CNT) {
- throw new RuntimeException("getObjByString(String key)方法没有成功,key is " + key);
- }
- return retryGetObject(key, cnt + 1);
- }
-
- private <T> List<T> retryMultiGet(Collection<String> keys, int cnt) {
- try {
- List<Object> value = redisTemplate.opsForValue().multiGet(keys);
- if (value != null) {
- return (List<T>) value;
- } else {
- return null;
- }
- } catch (RedisConnectionFailureException ex) {
- RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
- log.error("redis 操作异常,当前第" + cnt + "次", ex);
- }
- if (cnt > RETRY_CNT) {
- throw new RuntimeException("retryMultiGet(String keys)方法没有成功,keys is " + keys);
- }
- return retryMultiGet(keys, cnt + 1);
- }
-
- private void retrySaveObject(String key, Object value, int cnt) {
- try {
- redisTemplate.opsForValue().set(key, value);
- return;
- } catch (RedisConnectionFailureException ex) {
- RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
- log.error("redis 操作异常,当前第" + cnt + "次", ex);
- }
- if (cnt > RETRY_CNT) {
- throw new RuntimeException("retrySaveObject(String key, String value)方法没有成功,key is " + key + " value is " + value);
- }
- retrySaveObject(key, value, cnt + 1);
- }
-
- private void retrySaveObjectTimeOut(String key, Object value, long timeout, TimeUnit unit, int cnt) {
- try {
- redisTemplate.opsForValue().set(key, value, timeout, unit);
- return;
- } catch (RedisConnectionFailureException ex) {
- RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
- log.error("redis 操作异常,当前第" + cnt + "次", ex);
- }
- if (cnt > RETRY_CNT) {
- throw new RuntimeException(
- "retrySaveObjectTimeOut(String key, String value)方法没有成功,key is " + key + " value is " + value);
- }
- retrySaveObjectTimeOut(key, value, timeout, unit, cnt + 1);
- }
-
- private void retrySetAddObject(String key, Object value, int cnt) {
- try {
- redisTemplate.opsForSet().add(key, value);
- return;
- } catch (RedisConnectionFailureException ex) {
- RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
- log.error("redis 操作异常,当前第" + cnt + "次", ex);
- }
- if (cnt > RETRY_CNT) {
- throw new RuntimeException("retrySetAdd(String key, String value)方法没有成功,key is " + key + " value is " + value);
- }
- retrySetAddObject(key, value, cnt + 1);
- }
-
- private Set<Object> retrySetGetObject(String key, int cnt) {
- try {
- return redisTemplate.opsForSet().members(key);
- } catch (RedisConnectionFailureException ex) {
- RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
- log.error("redis 操作异常,当前第" + cnt + "次", ex);
- }
- if (cnt > RETRY_CNT) {
- throw new RuntimeException("retrySetGetObject(String key)方法没有成功,key is " + key);
- }
- return retrySetGetObject(key, cnt + 1);
- }
-
- private void retrySetRemoveObject(String key, Object value, int cnt) {
- try {
- redisTemplate.opsForSet().remove(key, value);
- return;
- } catch (RedisConnectionFailureException ex) {
- RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
- log.error("redis 操作异常,当前第" + cnt + "次", ex);
- }
- if (cnt > RETRY_CNT) {
- throw new RuntimeException(
- "retrySetRemoveObject(String key, String value)方法没有成功,key is " + key + " value is " + value);
- }
- retrySetRemoveObject(key, value, cnt + 1);
- }
-
- private Long retryGetSetSize(String key, int cnt) {
- try {
- return redisTemplate.opsForSet().size(key);
- } catch (RedisConnectionFailureException ex) {
- RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
- log.error("redis 操作异常,当前第" + cnt + "次", ex);
- }
- if (cnt > RETRY_CNT) {
- throw new RuntimeException("retryGetSetSize(String key, String value)方法没有成功,key is " + key);
- }
- return retryGetSetSize(key, cnt + 1);
- }
-
- private void retrySaveHash(String key, String hashKey, Object value, int cnt) {
- try {
- redisTemplate.opsForHash().put(key, hashKey, value);
- return;
- } catch (RedisConnectionFailureException ex) {
- RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
- log.error("redis 操作异常,当前第" + cnt + "次", ex);
- }
- if (cnt <= RETRY_CNT) {
- throw new RuntimeException("saveHash(String key, String hashKey, String value)方法没有成功,key is " + key + ",field is "
- + hashKey + ", value is " + value);
- }
- retrySaveHash(key, hashKey, value, cnt + 1);
- }
-
- private Boolean retryHasKey(String key, int cnt) {
- try {
- return redisTemplate.hasKey(key);
- } catch (RedisConnectionFailureException ex) {
- RedisConnectionUtils.unbindConnection(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
- log.error("redis 操作异常,当前第" + cnt + "次", ex);
- }
- if (cnt > RETRY_CNT) {
- throw new RuntimeException("hasKey(String key) 方法没有成功,key is " + key);
- }
- return retryHasKey(key, cnt + 1);
- }
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。