@Override public void put(Object key, Object value) { if (value == null) { return; } @SuppressWarnings("unchecked") T val = (T) value; byte[] bVal; try { bVal = serializer.serialize(val); } catch (Exception e) { throw new RuntimeException(e); } byte[] prefixKey = prefixKey(key); if (logger.isDebugEnabled()) { logger.debug( "Putting into cache, key:" + new String(prefixKey, charset) + ", value:" + new String(bVal, charset)); } if (entryTimeout != null) { client.setex(prefixKey, entryTimeout, bVal); } else { client.set(prefixKey, bVal); } }
@Override public void evict(final Object key) { final String sKey = String.valueOf(key); final byte[] prefixKey = prefixKey(key); if (logger.isDebugEnabled()) { logger.debug( "Removing from cache, key " + (sKey.endsWith("*") ? "pattern:" : ":") + new String(prefixKey, charset)); } client.nativeExecute( new JedisOperation() { @Override public void call(Jedis jedis) { if (sKey.endsWith("*")) { Set<byte[]> keys = jedis.keys(prefixKey); if (keys != null && keys.size() > 0) { jedis.del(keys.toArray(new byte[0][0])); } } else { jedis.del(prefixKey); } } }); }
@Override public void clear() { if (logger.isDebugEnabled()) { logger.debug("Clearing caches, key pattern:" + keyPrefix.concat("*")); } client.nativeExecute( new JedisOperation() { @Override public void call(Jedis jedis) { if (keyPrefix.isEmpty()) { jedis.flushDB(); } else { Set<byte[]> keys = jedis.keys(keyPrefix.concat("*").getBytes(charset)); jedis.del(keys.toArray(new byte[0][0])); } } }); }
@Override public ValueWrapper get(Object key) { byte[] prefixKey = prefixKey(key); if (logger.isDebugEnabled()) { logger.debug("Getting from cache, key:" + new String(prefixKey, charset)); } byte[] bVal = client.get(prefixKey); if (bVal == null) { return null; } T val; try { val = serializer.deserialize(bVal); } catch (Exception e) { throw new RuntimeException(e); } return (val != null ? new SimpleValueWrapper(val) : null); }