1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006 |
- package com.kmall.common.utils.redis;
- import com.google.common.collect.Sets;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Component;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
- import redis.clients.jedis.exceptions.JedisException;
- import java.util.*;
- @Component
- public class JedisUtil {
- protected static Logger logger = LoggerFactory.getLogger(JedisUtil.class);
- private static JedisPool jedisPool = null;
- static {
- JedisPoolConfig config = new JedisPoolConfig();
- /*config.setMaxIdle(Integer.parseInt(ResourceUtil.getConfigByName("redis.pool.maxIdle")));
- config.setMaxWaitMillis(Long.parseLong(ResourceUtil.getConfigByName("redis.pool.maxWait")));
- config.setMaxTotal(Integer.parseInt(ResourceUtil.getConfigByName("redis.pool.maxTotal")));
- config.setTestOnBorrow(Boolean.parseBoolean(ResourceUtil.getConfigByName("redis.pool.testOnBorrow")));
- String redisIp = ResourceUtil.getConfigByName("redis.host");
- int port = Integer.parseInt(ResourceUtil.getConfigByName("redis.port"));
- jedisPool = new JedisPool(config, redisIp, port, 10000);*/
- config.setMaxIdle(Integer.parseInt(JedisPropertiesBuilder.instance().getPool().getMaxIdle()));
- config.setMaxWaitMillis(Long.parseLong(JedisPropertiesBuilder.instance().getPool().getMaxWait()));
- config.setMaxTotal(Integer.parseInt(JedisPropertiesBuilder.instance().getPool().getMaxTotal()));
- config.setTestOnBorrow(Boolean.parseBoolean(JedisPropertiesBuilder.instance().getPool().getTestOnBorrow()));
- String redisIp = JedisPropertiesBuilder.instance().getHost();
- int port = Integer.parseInt(JedisPropertiesBuilder.instance().getPort());
- jedisPool = new JedisPool(config, redisIp, port, 10000);
- logger.info("redis连接成功: {}:{}, ", redisIp, port);
- }
- public static JedisPool getPool() {
- return jedisPool;
- }
- /**
- * 获取缓存
- *
- * @param key 键
- * @return 值
- */
- public static String get(String key) {
- String value = null;
- Jedis jedis = null;
- try {
- jedis = getResource();
- if (jedis.exists(key)) {
- value = jedis.get(key);
- value = org.apache.commons.lang.StringUtils.isNotBlank(value) && !"nil".equalsIgnoreCase(value) ? value : null;
- logger.debug("get {} = {}", key, value);
- }
- } catch (Exception e) {
- logger.warn("get {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return value;
- }
- /**
- * 获取缓存
- *
- * @param key 键
- * @return 值
- */
- public static Object getObject(String key) {
- Object value = null;
- Jedis jedis = null;
- try {
- jedis = getResource();
- if (jedis.exists(getBytesKey(key))) {
- value = toObject(jedis.get(getBytesKey(key)));
- logger.debug("getObject {} = {}", key, value);
- }
- } catch (Exception e) {
- logger.warn("getObject {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return value;
- }
- /**
- * 设置缓存
- *
- * @param key 键
- * @param value 值
- * @param cacheSeconds 超时时间,0为不超时
- * @return
- */
- public static String set(String key, String value, int cacheSeconds) {
- String result = null;
- Jedis jedis = null;
- try {
- jedis = getResource();
- result = jedis.set(key, value);
- if (cacheSeconds != 0) {
- jedis.expire(key, cacheSeconds);
- }
- logger.debug("set {} = {}", key, value);
- } catch (Exception e) {
- logger.warn("set {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 设置缓存过期时间
- *
- * @param key 键
- * @param cacheSeconds 超时时间,0为不超时
- * @return
- */
- public static void expire(String key, int cacheSeconds) {
- Jedis jedis = null;
- try {
- jedis = getResource();
- if (jedis.exists(getBytesKey(key))) {
- jedis.expire(key, cacheSeconds);
- logger.debug("expire {} = {}", key, cacheSeconds);
- }
- } catch (Exception e) {
- logger.warn("expire {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- }
- /**
- * 设置缓存
- *
- * @param key 键
- * @param value 值
- * @param cacheSeconds 超时时间,0为不超时
- * @return
- */
- public static String setObject(String key, Object value, int cacheSeconds) {
- String result = null;
- Jedis jedis = null;
- try {
- jedis = getResource();
- result = jedis.set(getBytesKey(key), toBytes(value));
- if (cacheSeconds != 0) {
- jedis.expire(key, cacheSeconds);
- }
- logger.debug("setObject {} = {}", key, value);
- } catch (Exception e) {
- logger.warn("setObject {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * key筛选
- *
- * @param pattern
- * @return
- */
- public static Set<String> keys(String pattern) {
- Set<String> result = Sets.newHashSet();
- Jedis jedis = null;
- try {
- jedis = getResource();
- result = jedis.keys(pattern);
- } catch (Exception e) {
- logger.warn("pattern {}", pattern, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 获取List缓存
- *
- * @param key 键
- * @return 值
- */
- public static List<String> getList(String key) {
- List<String> value = null;
- Jedis jedis = null;
- try {
- jedis = getResource();
- if (jedis.exists(key)) {
- value = jedis.lrange(key, 0, -1);
- }
- } catch (Exception e) {
- logger.warn("getList {}", key, e);
- } finally {
- returnResource(jedis);
- }
- return value;
- }
- /**
- * 获取List缓存
- *
- * @param key 键
- * @return 值
- */
- public static List<Object> getObjectList(String key) {
- List<Object> value = null;
- Jedis jedis = null;
- try {
- jedis = getResource();
- byte[] keybyte = getBytesKey(key);
- if (jedis.exists(keybyte)) {
- List<byte[]> list = jedis.lrange(keybyte, 0, -1);
- value = new ArrayList<>();
- for (byte[] bs : list) {
- value.add(toObject(bs));
- }
- logger.debug("getObjectList {} ", key, value);
- }
- } catch (Exception e) {
- logger.warn("getObjectList {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return value;
- }
- /**
- * @param keyPrefix
- * @return List<Object>
- * @Description:根据key前缀模糊查询缓存对象
- * @date 2016年9月5日
- * @author zhuliyun
- */
- public static List<Object> getObjectListByKeyPrefix(String keyPrefix) {
- List<Object> value = new ArrayList<>();
- Jedis jedis = null;
- try {
- jedis = getResource();
- Set<String> keys = getKeysByPrefix(keyPrefix);
- if (keys != null && keys.size() > 0) {
- for (String key : keys) {
- Object object = getObject(key);
- value.add(object);
- logger.debug("getObjectList {} ", key, value);
- }
- }
- } catch (Exception e) {
- logger.warn("getObjectList {} ", keyPrefix, e);
- } finally {
- returnResource(jedis);
- }
- return value;
- }
- /**
- * 设置List缓存
- *
- * @param key 键
- * @param value 值
- * @param cacheSeconds 超时时间,0为不超时
- * @return
- */
- public static long setList(String key, List<String> value, int cacheSeconds) {
- long result = 0;
- Jedis jedis = null;
- try {
- jedis = getResource();
- if (jedis.exists(key)) {
- jedis.del(key);
- }
- result = jedis.rpush(key, (String[]) value.toArray());
- if (cacheSeconds != 0) {
- jedis.expire(key, cacheSeconds);
- }
- logger.debug("setList {} ", key, value);
- } catch (Exception e) {
- logger.warn("setList {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 设置List缓存
- *
- * @param key 键
- * @param value 值
- * @param cacheSeconds 超时时间,0为不超时
- * @return
- */
- public static long setObjectList(String key, List<Object> value,
- int cacheSeconds) {
- long result = 0;
- Jedis jedis = null;
- try {
- jedis = getResource();
- if (jedis.exists(getBytesKey(key))) {
- jedis.del(key);
- }
- List<byte[]> list = new ArrayList<>();
- for (Object o : value) {
- list.add(toBytes(o));
- }
- result = jedis.rpush(getBytesKey(key), (byte[][]) list.toArray());
- if (cacheSeconds != 0) {
- jedis.expire(key, cacheSeconds);
- }
- logger.debug("setObjectList {} ", key, value);
- } catch (Exception e) {
- logger.warn("setObjectList {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 向List缓存中添加值
- *
- * @param key 键
- * @param value 值
- * @return
- */
- public static long listAdd(String key, String... value) {
- long result = 0;
- Jedis jedis = null;
- try {
- jedis = getResource();
- result = jedis.rpush(key, value);
- logger.debug("listAdd {} ", key, value);
- } catch (Exception e) {
- logger.warn("listAdd {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 向List缓存中添加值
- *
- * @param key 键
- * @param value 值
- * @return
- */
- public static long listObjectAdd(String key, Object... value) {
- long result = 0;
- Jedis jedis = null;
- try {
- jedis = getResource();
- List<byte[]> list = new ArrayList<>();
- for (Object o : value) {
- list.add(toBytes(o));
- }
- result = jedis.rpush(getBytesKey(key), (byte[][]) list.toArray());
- logger.debug("listObjectAdd {} ", key, value);
- } catch (Exception e) {
- logger.warn("listObjectAdd {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 获取缓存
- *
- * @param key 键
- * @return 值
- */
- public static Set<String> getSet(String key) {
- Set<String> value = null;
- Jedis jedis = null;
- try {
- jedis = getResource();
- if (jedis.exists(key)) {
- value = jedis.smembers(key);
- logger.debug("getSet {} ", key, value);
- }
- } catch (Exception e) {
- logger.warn("getSet {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return value;
- }
- /**
- * @param keyPrefix
- * @return Set<String>
- * @Description:获取以指定开头前缀的所有Key
- * @date 2016年9月5日
- * @author zhuliyun
- */
- public static Set<String> getKeysByPrefix(String keyPrefix) {
- Set<String> value = null;
- Jedis jedis = null;
- try {
- jedis = getResource();
- value = jedis.keys(keyPrefix + "*");
- logger.debug("getSet {} ", keyPrefix, value);
- } catch (Exception e) {
- logger.warn("getSet {} ", keyPrefix, e);
- } finally {
- returnResource(jedis);
- }
- return value;
- }
- /**
- * 获取缓存
- *
- * @param key 键
- * @return 值
- */
- public static Set<Object> getObjectSet(String key) {
- Set<Object> value = null;
- Jedis jedis = null;
- try {
- jedis = getResource();
- if (jedis.exists(getBytesKey(key))) {
- value = new HashSet<Object>();
- Set<byte[]> set = jedis.smembers(getBytesKey(key));
- for (byte[] bs : set) {
- value.add(toObject(bs));
- }
- logger.debug("getObjectSet {} ", key, value);
- }
- } catch (Exception e) {
- logger.warn("getObjectSet {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return value;
- }
- /**
- * 设置Set缓存
- *
- * @param key 键
- * @param value 值
- * @param cacheSeconds 超时时间,0为不超时
- * @return
- */
- public static long setSet(String key, Set<String> value, int cacheSeconds) {
- long result = 0;
- Jedis jedis = null;
- try {
- jedis = getResource();
- if (jedis.exists(key)) {
- jedis.del(key);
- }
- result = jedis.sadd(key, (String[]) value.toArray());
- if (cacheSeconds != 0) {
- jedis.expire(key, cacheSeconds);
- }
- logger.debug("setSet {} ", key, value);
- } catch (Exception e) {
- logger.warn("setSet {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 设置Set缓存
- *
- * @param key 键
- * @param value 值
- * @param cacheSeconds 超时时间,0为不超时
- * @return
- */
- public static long setObjectSet(String key, Set<Object> value,
- int cacheSeconds) {
- long result = 0;
- Jedis jedis = null;
- try {
- jedis = getResource();
- if (jedis.exists(getBytesKey(key))) {
- jedis.del(key);
- }
- Set<byte[]> set = new HashSet<byte[]>();
- for (Object o : value) {
- set.add(toBytes(o));
- }
- result = jedis.sadd(getBytesKey(key), (byte[][]) set.toArray());
- if (cacheSeconds != 0) {
- jedis.expire(key, cacheSeconds);
- }
- logger.debug("setObjectSet {} ", key, value);
- } catch (Exception e) {
- logger.warn("setObjectSet {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 向Set缓存中添加值
- *
- * @param key 键
- * @param value 值
- * @return
- */
- public static long setSetAdd(String key, String... value) {
- long result = 0;
- Jedis jedis = null;
- try {
- jedis = getResource();
- result = jedis.sadd(key, value);
- logger.debug("setSetAdd {} ", key, value);
- } catch (Exception e) {
- logger.warn("setSetAdd {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 向Set缓存中添加值
- *
- * @param key 键
- * @param value 值
- * @return
- */
- public static long setSetObjectAdd(String key, Object... value) {
- long result = 0;
- Jedis jedis = null;
- try {
- jedis = getResource();
- Set<byte[]> set = new HashSet<byte[]>();
- for (Object o : value) {
- set.add(toBytes(o));
- }
- result = jedis.rpush(getBytesKey(key), (byte[][]) set.toArray());
- logger.debug("setSetObjectAdd {} ", key, value);
- } catch (Exception e) {
- logger.warn("setSetObjectAdd {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 获取Map缓存
- *
- * @param key 键
- * @return 值
- */
- public static Map<String, String> getMap(String key) {
- Map<String, String> value = null;
- Jedis jedis = null;
- try {
- jedis = getResource();
- if (jedis.exists(key)) {
- value = jedis.hgetAll(key);
- logger.debug("getMap {} ", key, value);
- }
- } catch (Exception e) {
- logger.warn("getMap {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return value;
- }
- /**
- * 获取Map缓存
- *
- * @param key 键
- * @return 值
- */
- public static Map<String, Object> getObjectMap(String key) {
- Map<String, Object> value = null;
- Jedis jedis = null;
- try {
- jedis = getResource();
- if (jedis.exists(getBytesKey(key))) {
- value = new HashMap<String, Object>();
- Map<byte[], byte[]> map = jedis.hgetAll(getBytesKey(key));
- for (Map.Entry<byte[], byte[]> e : map.entrySet()) {
- value.put(String.valueOf(e.getKey()),
- toObject(e.getValue()));
- }
- logger.debug("getObjectMap {} ", key, value);
- }
- } catch (Exception e) {
- logger.warn("getObjectMap {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return value;
- }
- /**
- * 设置Map缓存
- *
- * @param key 键
- * @param value 值
- * @param cacheSeconds 超时时间,0为不超时
- * @return
- */
- public static String setMap(String key, Map<String, String> value,
- int cacheSeconds) {
- String result = null;
- Jedis jedis = null;
- try {
- jedis = getResource();
- if (jedis.exists(key)) {
- jedis.del(key);
- }
- result = jedis.hmset(key, value);
- if (cacheSeconds != 0) {
- jedis.expire(key, cacheSeconds);
- }
- logger.debug("setMap {} ", key, value);
- } catch (Exception e) {
- logger.warn("setMap {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 设置Map缓存
- *
- * @param key 键
- * @param value 值
- * @param cacheSeconds 超时时间,0为不超时
- * @return
- */
- public static String setObjectMap(String key, Map<String, Object> value,
- int cacheSeconds) {
- String result = null;
- Jedis jedis = null;
- try {
- jedis = getResource();
- if (jedis.exists(getBytesKey(key))) {
- jedis.del(key);
- }
- Map<byte[], byte[]> map = new HashMap<byte[], byte[]>();
- for (Map.Entry<String, Object> e : value.entrySet()) {
- map.put(getBytesKey(e.getKey()), toBytes(e.getValue()));
- }
- result = jedis.hmset(getBytesKey(key), (Map<byte[], byte[]>) map);
- if (cacheSeconds != 0) {
- jedis.expire(key, cacheSeconds);
- }
- logger.debug("setObjectMap {} ", key, value);
- } catch (Exception e) {
- logger.warn("setObjectMap {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 向Map缓存中添加值
- *
- * @param key 键
- * @param value 值
- * @return
- */
- public static String mapPut(String key, Map<String, String> value) {
- String result = null;
- Jedis jedis = null;
- try {
- jedis = getResource();
- result = jedis.hmset(key, value);
- logger.debug("mapPut {} ", key, value);
- } catch (Exception e) {
- logger.warn("mapPut {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 向Map缓存中添加值
- *
- * @param key 键
- * @param value 值
- * @return
- */
- public static String mapObjectPut(String key, Map<String, Object> value) {
- String result = null;
- Jedis jedis = null;
- try {
- jedis = getResource();
- Map<byte[], byte[]> map = new HashMap<byte[], byte[]>();
- for (Map.Entry<String, Object> e : value.entrySet()) {
- map.put(getBytesKey(e.getKey()), toBytes(e.getValue()));
- }
- result = jedis.hmset(getBytesKey(key), (Map<byte[], byte[]>) map);
- logger.debug("mapObjectPut {} ", key, value);
- } catch (Exception e) {
- logger.warn("mapObjectPut {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 移除Map缓存中的值
- *
- * @param key 键
- * @param mapKey 值
- * @return
- */
- public static long mapRemove(String key, String mapKey) {
- long result = 0;
- Jedis jedis = null;
- try {
- jedis = getResource();
- result = jedis.hdel(key, mapKey);
- logger.debug("mapRemove {} {}", key, mapKey);
- } catch (Exception e) {
- logger.warn("mapRemove {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 移除Map缓存中的值
- *
- * @param key 键
- * @param mapKey 值
- * @return
- */
- public static long mapObjectRemove(String key, String mapKey) {
- long result = 0;
- Jedis jedis = null;
- try {
- jedis = getResource();
- result = jedis.hdel(getBytesKey(key), getBytesKey(mapKey));
- logger.debug("mapObjectRemove {} {}", key, mapKey);
- } catch (Exception e) {
- logger.warn("mapObjectRemove {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 判断Map缓存中的Key是否存在
- *
- * @param key 键
- * @param mapKey 值
- * @return
- */
- public static boolean mapExists(String key, String mapKey) {
- boolean result = false;
- Jedis jedis = null;
- try {
- jedis = getResource();
- result = jedis.hexists(key, mapKey);
- logger.debug("mapExists {} {}", key, mapKey);
- } catch (Exception e) {
- logger.warn("mapExists {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 判断Map缓存中的Key是否存在
- *
- * @param key 键
- * @param mapKey 值
- * @return
- */
- public static boolean mapObjectExists(String key, String mapKey) {
- boolean result = false;
- Jedis jedis = null;
- try {
- jedis = getResource();
- result = jedis.hexists(getBytesKey(key), getBytesKey(mapKey));
- logger.debug("mapObjectExists {} {}", key, mapKey);
- } catch (Exception e) {
- logger.warn("mapObjectExists {} ", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 删除缓存
- *
- * @param key 键
- * @return
- */
- public static long del(String key) {
- long result = 0;
- Jedis jedis = null;
- try {
- jedis = getResource();
- if (jedis.exists(key)) {
- result = jedis.del(key);
- logger.debug("del {}", key);
- } else {
- logger.debug("del {} not exists", key);
- }
- } catch (Exception e) {
- logger.warn("del {}", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 删除缓存
- *
- * @param key 键
- * @return
- */
- public static long delObject(String key) {
- long result = 0;
- Jedis jedis = null;
- try {
- jedis = getResource();
- if (jedis.exists(getBytesKey(key))) {
- result = jedis.del(getBytesKey(key));
- logger.debug("delObject {}", key);
- } else {
- logger.debug("delObject {} not exists", key);
- }
- } catch (Exception e) {
- logger.warn("delObject {}", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 缓存是否存在
- *
- * @param key 键
- * @return
- */
- public static boolean exists(String key) {
- boolean result = false;
- Jedis jedis = null;
- try {
- jedis = getResource();
- result = jedis.exists(key);
- logger.debug("exists {}", key);
- } catch (Exception e) {
- logger.warn("exists {}", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 缓存是否存在
- *
- * @param key 键
- * @return
- */
- public static boolean existsObject(String key) {
- boolean result = false;
- Jedis jedis = null;
- try {
- jedis = getResource();
- result = jedis.exists(getBytesKey(key));
- logger.debug("existsObject {}", key);
- } catch (Exception e) {
- logger.warn("existsObject {}", key, e);
- } finally {
- returnResource(jedis);
- }
- return result;
- }
- /**
- * 获取资源
- *
- * @return
- * @throws JedisException
- */
- public static Jedis getResource() throws JedisException {
- Jedis jedis = null;
- try {
- jedis = jedisPool.getResource();
- // logger.debug("getResource.", jedis);
- } catch (JedisException e) {
- logger.warn("getResource.", e);
- returnBrokenResource(jedis);
- throw e;
- }
- return jedis;
- }
- /**
- * 归还资源
- *
- * @param jedis
- */
- public static void returnBrokenResource(Jedis jedis) {
- if (jedis != null) {
- jedisPool.returnBrokenResource(jedis);
- }
- }
- /**
- * 释放资源
- *
- * @param jedis
- */
- public static void returnResource(Jedis jedis) {
- if (jedis != null) {
- jedisPool.returnResource(jedis);
- }
- }
- /**
- * 获取byte[]类型Key
- *
- * @param object
- * @return
- */
- public static byte[] getBytesKey(Object object) {
- if (object instanceof String) {
- String ob =(String) object;
- return ob.getBytes();
- } else {
- return ObjectUtils.serialize(object);
- }
- }
- /**
- * 获取byte[]类型Key
- *
- * @param key
- * @return
- */
- public static Object getObjectKey(byte[] key) {
- try {
- return String.valueOf(key);
- } catch (UnsupportedOperationException uoe) {
- try {
- return JedisUtil.toObject(key);
- } catch (UnsupportedOperationException uoe2) {
- uoe2.printStackTrace();
- }
- }
- return null;
- }
- public static boolean isBlank(String str) {
- return str == null || "".equals(str.trim());
- }
- /**
- * Object转换byte[]类型
- *
- * @param object
- * @return
- */
- public static byte[] toBytes(Object object) {
- return ObjectUtils.serialize(object);
- }
- /**
- * byte[]型转换Object
- *
- * @param bytes
- * @return
- */
- public static Object toObject(byte[] bytes) {
- return ObjectUtils.unserialize(bytes);
- }
- }
|