首页 > 代码库 > redis代码

redis代码

public class MmailRedis {


/**

* 商品库存缓存

*/

private JedisDo stockJedis;


/**

* 个人信息缓存

*/

private JedisDo personalJedis;


/**

* 短期缓存

*/

private JedisDo shorttermJedis;


/**

* 导购宝用户信息缓存

*/

private JedisDo dgbJedis;

public JedisDo getStockJedis() {

return stockJedis;

}


@Autowired

public void setStockJedis(JedisDo stockJedis) {

this.stockJedis = stockJedis;

}


public JedisDo getPersonalJedis() {

return personalJedis;

}


@Autowired

public void setPersonalJedis(JedisDo personalJedis) {

this.personalJedis = personalJedis;

}


public JedisDo getShorttermJedis() {

return shorttermJedis;

}


@Autowired

public void setShorttermJedis(JedisDo shorttermJedis) {

this.shorttermJedis = shorttermJedis;

}


public JedisDo getDgbJedis() {

return dgbJedis;

}

@Autowired

public void setDgbJedis(JedisDo dgbJedis) {

this.dgbJedis = dgbJedis;

}


}


====================================

public interface StockCache {

public MmailRedis getMmailRedis();


public List<Long> decrBys(List<Product> list);


public List<Long> loadBys(List<Product> list);

/**

* 获取导购员信息

* @param shopGuideCode

* @return

*/

public Map<String,String> hgetAll(String shopGuideCode);

/**

* 获取某个Key下的values里的字段

* @param shopGuideCode

* @param returnColumn

* @return

*/

public String hget(String shopGuideCode,String returnColumn);


}

===================================================

public class JedisDo {

private static Logger logger = LoggerFactory.getLogger(JedisDo.class);


@Autowired

private JedisPool jedisPool;


public void setJedisPool(JedisPool jedisPool) {

this.jedisPool = jedisPool;

}


public JedisDo(JedisPool jedisPool) {

this.jedisPool = jedisPool;

}


public JedisDo() {

}


/**

* Execute with a call back action with result.

*/

public <T> T execute(JedisAction<T> jedisAction) throws JedisException {

Jedis jedis = null;

// boolean broken = false;

logger.debug("execute");

try {

jedis = jedisPool.getResource();

return jedisAction.action(jedis);

} catch (JedisException e) {

throw e;

} finally {

if (jedis != null) {

jedis.close();

}

}

}


/**

* Return the internal JedisPool.

*/

public JedisPool getJedisPool() {

return jedisPool;

}


public static void main(String[] args) {

Jedis jedis = null;

//Redis服务器IP

   String ADDR = "192.168.2.127";

   //Redis的端口号

   int PORT = 6379;

   //访问密码

   String AUTH = "appcan.cn";

   int TIMEOUT = 10000;

   

try {

ResourceBundle bundle = ResourceBundle.getBundle("redis");

if (bundle == null) {

   throw new IllegalArgumentException("[redis.properties] is not found!");

}

JedisPoolConfig config = new JedisPoolConfig();

config.setMaxTotal(Integer.valueOf(bundle.getString("redis.pool.maxTotal")));

config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));

config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWait")));

logger.info("redis.pool.maxTotal="+bundle.getString("redis.pool.maxTotal"));

JedisPool pool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);

jedis = pool.getResource();

jedis.hset("STOCK:","prod" ,"1");

} catch (JedisException e) {

throw e;

} finally {

if (jedis != null) {

jedis.close();

}

}

}

}

=============================================

public interface JedisAction<T> {

T action(Jedis jedis);

}


redis代码