去除fst依赖,更新为RedisSerializer

This commit is contained in:
zhuangqian 2017-01-05 19:01:15 +08:00
parent 38c14338c4
commit 08a9456646
6 changed files with 56 additions and 47 deletions

View File

@ -68,12 +68,6 @@
<artifactId>jedis</artifactId> <artifactId>jedis</artifactId>
<version>2.9.0</version> <version>2.9.0</version>
</dependency> </dependency>
<!-- fst -->
<dependency>
<groupId>de.ruedigermoeller</groupId>
<artifactId>fst</artifactId>
<version>2.48</version>
</dependency>
<!--web --> <!--web -->
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
@ -178,7 +172,6 @@
<artifactId>shiro-ehcache</artifactId> <artifactId>shiro-ehcache</artifactId>
<version>${shiro.version}</version> <version>${shiro.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.sf.ehcache</groupId> <groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId> <artifactId>ehcache-core</artifactId>

View File

@ -26,14 +26,14 @@ import redis.clients.jedis.JedisPool;
import com.smallchill.core.config.BladeConfig; import com.smallchill.core.config.BladeConfig;
import com.smallchill.core.interfaces.IPlugin; import com.smallchill.core.interfaces.IPlugin;
import com.smallchill.core.toolbox.redis.Cache; import com.smallchill.core.toolbox.redis.RedisCache;
import com.smallchill.core.toolbox.redis.IKeyNamingPolicy; import com.smallchill.core.toolbox.redis.IKeyNamingPolicy;
import com.smallchill.core.toolbox.redis.serializer.FstSerializer; import com.smallchill.core.toolbox.redis.serializer.RedisSerializer;
public class ConnectionPlugin implements IPlugin{ public class ConnectionPlugin implements IPlugin{
private static Map<String, SQLManager> sqlManagerPool = new ConcurrentHashMap<String, SQLManager>(); private static Map<String, SQLManager> sqlManagerPool = new ConcurrentHashMap<String, SQLManager>();
private static Map<String, Cache> redisCachePool = new ConcurrentHashMap<String, Cache>(); private static Map<String, RedisCache> redisCachePool = new ConcurrentHashMap<String, RedisCache>();
public String MASTER = "master"; public String MASTER = "master";
@ -41,7 +41,7 @@ public class ConnectionPlugin implements IPlugin{
return sqlManagerPool; return sqlManagerPool;
} }
public Map<String, Cache> getRedisCachePool(){ public Map<String, RedisCache> getRedisCachePool(){
return redisCachePool; return redisCachePool;
} }
@ -74,7 +74,7 @@ public class ConnectionPlugin implements IPlugin{
for(String key : BladeConfig.getJedisPool().keySet()){ for(String key : BladeConfig.getJedisPool().keySet()){
JedisPool jedisPool = BladeConfig.getJedisPool().get(key); JedisPool jedisPool = BladeConfig.getJedisPool().get(key);
//创建redis通用cache操作类 //创建redis通用cache操作类
Cache rc = new Cache(key, jedisPool, FstSerializer.me, IKeyNamingPolicy.defaultKeyNamingPolicy); RedisCache rc = new RedisCache(key, jedisPool, RedisSerializer.me, IKeyNamingPolicy.defaultKeyNamingPolicy);
redisCachePool.put(key, rc); redisCachePool.put(key, rc);
} }
if(!redisCachePool.containsKey(MASTER) && redisCachePool.size() > 0){ if(!redisCachePool.containsKey(MASTER) && redisCachePool.size() > 0){

View File

@ -19,21 +19,21 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import com.smallchill.core.toolbox.redis.Cache; import com.smallchill.core.toolbox.redis.RedisCache;
/** /**
* Redis操作工具类 * Redis操作工具类
*/ */
public class Redis { public class Redis {
private static Cache redisCache = null; private static RedisCache redisCache = null;
public static Cache init(String name) { public static RedisCache init(String name) {
return RedisManager.init(name); return RedisManager.init(name);
} }
private Redis() {} private Redis() {}
private static Cache getRedisCache() { private static RedisCache getRedisCache() {
if (null == redisCache) { if (null == redisCache) {
synchronized (Redis.class) { synchronized (Redis.class) {
redisCache = RedisManager.init(); redisCache = RedisManager.init();

View File

@ -19,20 +19,20 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import com.smallchill.core.plugins.connection.ConnectionPlugin; import com.smallchill.core.plugins.connection.ConnectionPlugin;
import com.smallchill.core.toolbox.redis.Cache; import com.smallchill.core.toolbox.redis.RedisCache;
/** /**
* Redis操作工具类 * Redis操作工具类
*/ */
public class RedisManager { public class RedisManager {
private static Map<String, Cache> pool = new ConcurrentHashMap<String, Cache>(); private static Map<String, RedisCache> pool = new ConcurrentHashMap<String, RedisCache>();
public static Cache init() { public static RedisCache init() {
return init(ConnectionPlugin.init().MASTER); return init(ConnectionPlugin.init().MASTER);
} }
public static Cache init(String name) { public static RedisCache init(String name) {
Cache rc = pool.get(name); RedisCache rc = pool.get(name);
if (null == rc) { if (null == rc) {
synchronized (RedisManager.class) { synchronized (RedisManager.class) {
rc = pool.get(name); rc = pool.get(name);

View File

@ -37,7 +37,7 @@ import redis.clients.jedis.JedisPool;
* 即可快速掌握使用方法 * 即可快速掌握使用方法
* Redis 命令参考: http://redisdoc.com/ * Redis 命令参考: http://redisdoc.com/
*/ */
public class Cache { public class RedisCache {
protected String name; protected String name;
protected JedisPool jedisPool; protected JedisPool jedisPool;
@ -46,11 +46,11 @@ public class Cache {
protected final ThreadLocal<Jedis> threadLocalJedis = new ThreadLocal<Jedis>(); protected final ThreadLocal<Jedis> threadLocalJedis = new ThreadLocal<Jedis>();
protected Cache() { protected RedisCache() {
} }
public Cache(String name, JedisPool jedisPool, ISerializer serializer, IKeyNamingPolicy keyNamingPolicy) { public RedisCache(String name, JedisPool jedisPool, ISerializer serializer, IKeyNamingPolicy keyNamingPolicy) {
this.name = name; this.name = name;
this.jedisPool = jedisPool; this.jedisPool = jedisPool;
this.serializer = serializer; this.serializer = serializer;

View File

@ -19,20 +19,19 @@ package com.smallchill.core.toolbox.redis.serializer;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream;
import org.nustaq.serialization.FSTObjectInput; import java.io.ObjectOutputStream;
import org.nustaq.serialization.FSTObjectOutput;
import redis.clients.util.SafeEncoder; import redis.clients.util.SafeEncoder;
import com.smallchill.core.toolbox.kit.LogKit; import com.smallchill.core.toolbox.kit.LogKit;
/** /**
* FstSerializer. * RedisSerializer.
*/ */
public class FstSerializer implements ISerializer { public class RedisSerializer implements ISerializer {
public static final ISerializer me = new FstSerializer(); public static final ISerializer me = new RedisSerializer();
public byte[] keyToBytes(String key) { public byte[] keyToBytes(String key) {
return SafeEncoder.encode(key); return SafeEncoder.encode(key);
@ -51,40 +50,57 @@ public class FstSerializer implements ISerializer {
} }
public byte[] valueToBytes(Object value) { public byte[] valueToBytes(Object value) {
FSTObjectOutput fstOut = null; ByteArrayOutputStream byteOut = null;
ObjectOutputStream ObjOut = null;
try { try {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); byteOut = new ByteArrayOutputStream();
fstOut = new FSTObjectOutput(bytesOut); ObjOut = new ObjectOutputStream(byteOut);
fstOut.writeObject(value); ObjOut.writeObject(value);
fstOut.flush(); ObjOut.flush();
return bytesOut.toByteArray(); return byteOut.toByteArray();
} }
catch (Exception e) { catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
finally { finally {
if(fstOut != null) try {
try {fstOut.close();} catch (IOException e) {LogKit.error(e.getMessage(), e);} if (null != ObjOut) {
ObjOut.close();
}
}
catch (IOException e) {
ObjOut = null; LogKit.error(e.getMessage(), e);
}
} }
} }
public Object valueFromBytes(byte[] bytes) { public Object valueFromBytes(byte[] bytes) {
if(bytes == null || bytes.length == 0) if(bytes == null || bytes.length == 0) {
return null; return null;
}
FSTObjectInput fstInput = null; ObjectInputStream ObjIn = null;
Object retVal = null;
try { try {
fstInput = new FSTObjectInput(new ByteArrayInputStream(bytes)); ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
return fstInput.readObject(); ObjIn = new ObjectInputStream(byteIn);
retVal = ObjIn.readObject();
return retVal;
} }
catch (Exception e) { catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
finally { finally {
if(fstInput != null) try {
try {fstInput.close();} catch (IOException e) {LogKit.error(e.getMessage(), e);} if(null != ObjIn) {
ObjIn.close();
}
}
catch (IOException e) {
ObjIn = null; LogKit.error(e.getMessage(), e);
}
} }
} }
} }