123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023 |
- package com.kmall.manager.manager.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.io.Serializable;
- import java.util.*;
- @Component
- public class JedisUtil implements Serializable {
- 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);*/
- try {
- if (null == JedisPropertiesBuilder.instance()) {
- logger.error("jedis init is error.");
- throw new Exception("jedis init is error.");
- }
- JedisProperties jp = JedisPropertiesBuilder.instance();
- config.setMaxIdle(Integer.parseInt(jp.getPool().getMaxIdle()));
- config.setMaxWaitMillis(Long.parseLong(jp.getPool().getMaxWait()));
- config.setMaxTotal(Integer.parseInt(jp.getPool().getMaxTotal()));
- config.setTestOnBorrow(Boolean.parseBoolean(jp.getPool().getTestOnBorrow()));
- String redisIp = jp.getHost();
- int port = Integer.parseInt(jp.getPort());
- String password = jp.getPassword();
- int database = jp.getDatabase();
- jedisPool = new JedisPool(config, redisIp, port, 10000, password, database);
- logger.info("redis连接成功: {}:{}, 连接池:{}", redisIp, port, jedisPool);
- } catch (Exception e) {
- logger.error("init RedisUtils is error", e);
- e.printStackTrace();
- }
- }
- 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 {
- logger.debug("jedisPool.{}", jedisPool);
- 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);
- }
- }
|