/** * 获得byte[]型的key * * @param key * @return */ private byte[] getByteKey(K key) { if (key instanceof String) { String preKey = this.keyPrefix + key; return preKey.getBytes(); } else { return SerializeUtils.serialize(key); } }
@Override public V put(K key, V value) throws CacheException { logger.debug("根据key从存储 key [" + key + "]"); try { cache.set(getByteKey(key), SerializeUtils.serialize(value)); return value; } catch (Throwable t) { throw new CacheException(t); } }
@Override public V get(K key) throws CacheException { logger.debug("根据key从Redis中获取对象 key [" + key + "]"); try { if (key == null) { return null; } else { byte[] rawValue = cache.get(getByteKey(key)); @SuppressWarnings("unchecked") V value = (V) SerializeUtils.unserialize(rawValue); return value; } } catch (Throwable t) { throw new CacheException(t); } }