Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
title连接Redis服务器的CacheService接口实现 类源码
linenumberstrue
languagejava
collapsetrue
package test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import redis.clients.jedis.Jedis;
import com.bstek.uflo.expr.impl.ProcessMapContext;
import com.bstek.uflo.model.ProcessDefinition;
import com.bstek.uflo.service.CacheService;
/**
 * @author Jacky.gao
 * @since 2017年11月6日
 */
public class RedisCacheService implements CacheService {
	private static final String PREFIX="UFLO_CAChe_";
	private Jedis jedis=new Jedis("localhost");
	@Override
	public ProcessDefinition getProcessDefinition(long processId) {
		if(!containsProcessDefinition(processId)){
			return null;
		}
		String key=PREFIX+"PD_"+processId;
		byte[] bytes=jedis.get(key.getBytes());
		return (ProcessDefinition)SerializeUtil.unserialize(bytes);
	}
	@Override
	public void putProcessDefinition(long processId, ProcessDefinition process) {
		String key=PREFIX+"PD_"+processId;
		jedis.set(key.getBytes(), SerializeUtil.serialize(process));
	}
	@Override
	public boolean containsProcessDefinition(long processId) {
		String key=PREFIX+"PD_"+processId;
		return jedis.exists(key.getBytes());
	}
	@Override
	public Collection<ProcessDefinition> loadAllProcessDefinitions() {
		String key=PREFIX+"PD_*";
		Set<byte[]> set=jedis.keys(key.getBytes());
		List<ProcessDefinition> list=new ArrayList<ProcessDefinition>();
		for(byte[] bytes:set){
			ProcessDefinition pd=(ProcessDefinition)SerializeUtil.unserialize(bytes);
			list.add(pd);
		}
		return list;
	}
	@Override
	public void removeProcessDefinition(long processId) {
		String key=PREFIX+"PD_"+processId;
		jedis.del(key.getBytes());
	}
	@Override
	public ProcessMapContext getContext(long processInstanceId) {
		if(!containsContext(processInstanceId)){
			return null;
		}
		String key=PREFIX+"PI_"+processInstanceId;
		byte[] bytes=jedis.get(key.getBytes());
		ProcessMapContext context=(ProcessMapContext)SerializeUtil.unserialize(bytes);
		return context;
	}
	@Override
	public void putContext(long processInstanceId, ProcessMapContext context) {
		String key=PREFIX+"PI_"+processInstanceId;
		jedis.set(key.getBytes(), SerializeUtil.serialize(context));
	}
	@Override
	public void removeContext(long processInstanceId) {
		String key=PREFIX+"PI_"+processInstanceId;
		jedis.del(key.getBytes());
	}
	@Override
	public boolean containsContext(long processInstanceId) {
		String key=PREFIX+"PI_"+processInstanceId;
		return jedis.exists(key.getBytes());
	}
}
Code Block
titleSerializeUtils类源码
linenumberstrue
languagejava
collapsetrue
package test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
 * @author Jacky.gao
 * @since 2017年11月6日
 */
public class SerializeUtil {
	public static byte[] serialize(Object object) {
		ObjectOutputStream oos = null;
		ByteArrayOutputStream baos = null;
		try {
			// 序列化
			baos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(baos);
			oos.writeObject(object);
			byte[] bytes = baos.toByteArray();
			return bytes;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	public static Object unserialize(byte[] bytes) {
		ByteArrayInputStream bais = null;
		try {
			// 反序列化
			bais = new ByteArrayInputStream(bytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			return ois.readObject();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}

...