去除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>
<version>2.9.0</version>
</dependency>
<!-- fst -->
<dependency>
<groupId>de.ruedigermoeller</groupId>
<artifactId>fst</artifactId>
<version>2.48</version>
</dependency>
<!--web -->
<dependency>
<groupId>javax.servlet</groupId>
@ -178,7 +172,6 @@
<artifactId>shiro-ehcache</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<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.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.serializer.FstSerializer;
import com.smallchill.core.toolbox.redis.serializer.RedisSerializer;
public class ConnectionPlugin implements IPlugin{
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";
@ -41,7 +41,7 @@ public class ConnectionPlugin implements IPlugin{
return sqlManagerPool;
}
public Map<String, Cache> getRedisCachePool(){
public Map<String, RedisCache> getRedisCachePool(){
return redisCachePool;
}
@ -74,7 +74,7 @@ public class ConnectionPlugin implements IPlugin{
for(String key : BladeConfig.getJedisPool().keySet()){
JedisPool jedisPool = BladeConfig.getJedisPool().get(key);
//创建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);
}
if(!redisCachePool.containsKey(MASTER) && redisCachePool.size() > 0){

View File

@ -19,21 +19,21 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import com.smallchill.core.toolbox.redis.Cache;
import com.smallchill.core.toolbox.redis.RedisCache;
/**
* 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);
}
private Redis() {}
private static Cache getRedisCache() {
private static RedisCache getRedisCache() {
if (null == redisCache) {
synchronized (Redis.class) {
redisCache = RedisManager.init();

View File

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

View File

@ -37,7 +37,7 @@ import redis.clients.jedis.JedisPool;
* 即可快速掌握使用方法
* Redis 命令参考: http://redisdoc.com/
*/
public class Cache {
public class RedisCache {
protected String name;
protected JedisPool jedisPool;
@ -46,11 +46,11 @@ public class Cache {
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.jedisPool = jedisPool;
this.serializer = serializer;

View File

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