Skip to content

Commit

Permalink
修改了redis内部key的序列化
Browse files Browse the repository at this point in the history
  • Loading branch information
junxworks committed Jul 19, 2018
1 parent 8b2ea67 commit 1019b2e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package io.github.junxworks.junx.cache.adapter;

import java.util.List;
import java.nio.charset.Charset;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -38,22 +38,38 @@ public abstract class AbstractCacheAdapter implements Cache {

protected Logger log = LoggerFactory.getLogger(getClass());

public static final String CHARSET = "UTF-8";

/**
* 获得组合过后的key值,如果子类cache本身不支持group的话,可以通过这种方式让key分组
* 子类可以使用,也可以不使用
*
* @param kv
* the kv
* @return composed key 属性
* @return 组合过后的Key属性
*/
protected String getComposedKey(KV kv) {
return getKeyPrefix(kv) + kv.getKey();
}

/**
* 获得组合过后的key值
* 获得组合过后的key byte数组
*
* @param kv the kv
* @return composed key 属性
*/
protected String getComposedKey(KV kv) {
return getKeyPrefix(kv) + kv.getKey();
protected byte[] getComposedKeyBytes(KV kv) {
return getComposedKey(kv).getBytes(Charset.forName(CHARSET));
}

/**
* 获得组合过后的key byte数组
*
* @param key the key
* @return composed key 属性
*/
protected byte[] getComposedKeyBytes(String key) {
return key.getBytes(Charset.forName(CHARSET));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void setJedis(Jedis jedis) {
public KV get(KV kv) {
if (kv == null)
throw new NullParameterException("When the value is read in redis, the parameter cannot be null");
kv.setValue(jedis.get(getComposedKey(kv).getBytes()));
kv.setValue(jedis.get(getComposedKeyBytes(kv)));
return kv;
}

Expand All @@ -79,7 +79,7 @@ public List<KV> get(List<KV> kvs) {
kv = kvs.get(i);
String key = kv.getKey();
if (!newMap.containsKey(key)) {
newMap.put(key, p.get(getComposedKey(kv).getBytes()));
newMap.put(key, p.get(getComposedKeyBytes(kv)));
}
}
p.sync();
Expand All @@ -102,7 +102,7 @@ public void set(KV kv) {
log.warn("Value of KV[group is \"{}\",key is \"{}\"] which will be cached can not bt null.This KV object will be ignored.", kv.getGroup(), kv.getKey());
return;
}
byte[] key = getComposedKey(kv).getBytes();
byte[] key = getComposedKeyBytes(kv);
jedis.set(key, value);
long expireTime = getTTLSeconds(kv);
if (expireTime > 0) {
Expand Down Expand Up @@ -130,7 +130,7 @@ public void set(List<KV> kvs) {
log.warn("Value of KV[group is \"{}\",key is \"{}\"] which will be cached can not bt null.This KV object will be ignored.", kv.getGroup(), kv.getKey());
continue;
}
byte[] key = getComposedKey(kv).getBytes();
byte[] key = getComposedKeyBytes(kv);
p.set(key, value);
long expireTime = getTTLSeconds(kv);
if (expireTime > 0) {
Expand All @@ -150,7 +150,7 @@ public void set(List<KV> kvs) {
public void delete(KV kv) {
if (kv == null)
throw new NullParameterException("When the redis delete value, parameter cannot be null");
jedis.del(getComposedKey(kv).getBytes());
jedis.del(getComposedKeyBytes(kv));
}

/**
Expand All @@ -163,15 +163,15 @@ public void delete(List<KV> kvs) {
if (!kvs.isEmpty()) {
Pipeline p = jedis.pipelined();
for (int i = 0, len = kvs.size(); i < len; i++) {
p.del(getComposedKey(kvs.get(i)).getBytes());
p.del(getComposedKeyBytes(kvs.get(i)));
}
p.sync();
}
}

@Override
public boolean exists(KV kv) {
return jedis.exists(getComposedKey(kv));
return jedis.exists(getComposedKeyBytes(kv));
}

@Override
Expand All @@ -185,7 +185,7 @@ public Map<KV, Boolean> exists(List<KV> kvs) {
KV kv = null;
for (int i = 0, len = kvs.size(); i < len; i++) {
kv = kvs.get(i);
newMap.put(kv.getKey(), p.exists(getComposedKey(kv)));
newMap.put(kv.getKey(), p.exists(getComposedKeyBytes(kv)));
}
p.sync();
for (int i = 0; i < kvs.size(); i++) {
Expand All @@ -209,7 +209,7 @@ public List<KV> getGroupValues(String group, String separator) {
Pipeline p = jedis.pipelined();
Map<String, Response<byte[]>> newMap = new HashMap<String, Response<byte[]>>();
for (int i = 0; i < keys.length; i++) {
newMap.put(keys[i], p.get(keys[i].getBytes()));
newMap.put(keys[i], p.get(getComposedKeyBytes(keys[i])));
}
p.sync();
KV _kv = null;
Expand Down

0 comments on commit 1019b2e

Please sign in to comment.