1.RedisUtil
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public final class RedisUtil {
//Redis服务器IP
private static String ADDR = Constants.web_sp_redis_url;
//Redis的端口号
private static int PORT = Constants.web_sp_redis_port;
//访问密码
private static String AUTH = Constants.web_sp_redis_auth;
//可用连接实例的最大数目,默认值为8;
//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
private static int MAX_ACTIVE = 1024;
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
private static int MAX_IDLE = 200;
//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
private static int MAX_WAIT = 10000;
private static int TIMEOUT = 10000;
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
private static boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
/**
* 初始化Redis连接池
*/
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取Jedis实例
* @return
*/
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 释放jedis资源
* @param jedis
*/
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
2.JsonUtil
import java.util.Date;
import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONUtils;
import net.sf.json.util.PropertyFilter;
/**
* json工具类
* @author Administrator
*
*/
public class JsonUtil {
/**
* JSONObject转Bean,将固定时间格式注册到json中
* @param jsonObject
* @param beanClass
* @return
*/
public Object JSONObjectToBean(JSONObject jsonObject, Class<?> beanClass){
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {"yyyy-MM-dd HH:mm:ss"}));
return JSONObject.toBean(jsonObject, beanClass);
}
/**
* Bean转JSONObject,将null值默认不转换,将Date按固定格式转换
* @param object
* @return
*/
public JSONObject BeanToJSONObject(Object object){
JsonConfig jsonConfig = new JsonConfig();
PropertyFilter filter = new PropertyFilter() {
public boolean apply(Object object, String fieldName,
Object fieldValue) {
return null == fieldValue;
}
};
jsonConfig.setJsonPropertyFilter(filter);
jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor());
return JSONObject.fromObject(object,jsonConfig);
}
}
3.系统调用
有数据则从redis获取;无数据则从数据库查询,转换为jason.string后存入
Jedis jedis = RedisUtil.getJedis();
JsonUtil jsonUtil = new JsonUtil();
if(jedis.exists(shopsId)) {
String shopJds = jedis.get(shopsId);
shop = (Shops)jsonUtil.JSONObjectToBean(JSONObject.fromObject(shopJds),Shops.class);
if (shop == null || shop.getId() == null || shop.getId() <= 0) {
logger.info("***(shopmm)网店详情,(Redis)异常显示[shop=null],ID["+shopsId+"].");
return "error404";
}
isredis = 1;
}else {
shop = shopService.getShops(shopsId, "shopsub,shopType,sales,user,sprate");
if (shop == null || shop.getId() == null) {
logger.info("***(shopmm)网店详情,(读库)异常显示[shop=null],ID["+shopsId+"].");
return "error404";
}
JSONObject json = jsonUtil.BeanToJSONObject(shop);
jedis.setex(shopsId,600,json.toString()); //600(s),10分钟;
isredis = 2;
}
RedisUtil.returnResource(jedis);
logger.info("***(shopmm)网店详情,ID["+shopsId+"],isredis["+isredis+"].");