标签:
在做电商系统时,库存是一个非常严格的数据,根据CAS(check and swap)原来下面对库存扣减提供两种方法,一种是redis,一种用java实现CAS。
第一种 redis实现:
以下这个类是工具类,稍作修改就可运行
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCommands;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.util.Pool;
import com.maowu.commons.conf.FileUpdate;
import com.maowu.commons.conf.NotifyFileUpdate;
import com.maowu.commons.logutil.LogProxy;
import com.maowu.commons.util.LogicUtil;
public class JedisPoolFactory
{
private static Logger log = LogProxy.getLogger(JedisPoolFactory.class);
private static String configFile = "jedisconf";
/**
* 主服务器数据源连接池,主要用于写操作
*/
private static JedisPool jedisPool = null;
/**
* 数据源共享连接池,主要用于读取数据
*/
private static ShardedJedisPool shardedJedisPool = null;
static
{
loadXmlConfig();
NotifyFileUpdate.registInterface(new FileUpdate()
{
@Override
public void updateFile(String fileName)
{
if (fileName.startsWith(configFile))
{
log.debug("updateFile = " + fileName);
loadXmlConfig();
}
}
});
}
/**
* @author: smartlv
* @date: 2014年2月17日下午3:36:30
*/
private static void loadX