redis查询案例

来源:互联网 发布:中介软件哪个好 编辑:程序博客网 时间:2024/06/08 09:04
1.流程:
前台:发送html请求:shopsGet-80768.html
controller:
类实现@Controller注解;
方法实现@RequestMapping("/shopsGet-{shopsId}")注解
public String shopsGet(@PathVariable String shopsId,Model model,HttpServletRequest request,HttpServletResponse response){}
redis数据查询:
Jedis jedis = RedisUtil.getJedis();
String shopJds = jedis.get(shopsId);
RedisUtil.returnResource(jedis);
数据封装处理:
转化为前台格式
浏览量更新:mybatis操作




2.redis初始化:


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);
}
}
}
原创粉丝点击