上接文章
* */
基于Sentinel的Redis集群(主从&Sharding)的redis封装实现
* */
@RequestMapping(value = "/redis.json", produces = "application/json; charset=UTF-8")
public void getRedisData(@RequestParam("value") String value, HttpServletResponse response)
throws Exception{
JSONObject json = new JSONObject();
response.setContentType("application/json;charset=UTF-8");
Map result = new HashMap();
String time = "12:00:00";
String sales = "30";
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
jedis.hset("ps", "KEY3", time);
jedis.hset("ps", "KEY4", sales);
Thread.sleep(500);
String getTime = jedis.hget("ps", "KEY3");
String getSales = jedis.hget("ps", "KEY4");
result.put("dateTime", getTime);
result.put("sales", getSales);
} catch (JedisConnectionException e){
log.error("jedis连接异常", e);
}
json.put("result", result);
json.writeJSONString(response.getWriter());
return;
}
2、
/**
* 查看ps域中所有的key值
* */
@RequestMapping(value = "/lookUpRedisKey.json", produces = "application/json; charset=UTF-8")
public void lookUpRedisKey(HttpServletRequest request, HttpServletResponse response)
throws Exception{
JSONObject json = new JSONObject();
response.setContentType("application/json;charset=UTF-8");
String result = "";
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Set<String> keys = jedis.hkeys("ps");
int keyLen = keys.size();
result = String.valueOf(keyLen);
// Iterator<String> it=keys.iterator() ;
// while(it.hasNext()){
// String key = it.next();
// result+=key+" ";
// }
} catch (JedisConnectionException e){
log.error("jedis连接异常", e);
}
json.put("result", result);
json.writeJSONString(response.getWriter());
return;
}
* 查看ps域中所有的key值
* */
@RequestMapping(value = "/lookUpRedisKey.json", produces = "application/json; charset=UTF-8")
public void lookUpRedisKey(HttpServletRequest request, HttpServletResponse response)
throws Exception{
JSONObject json = new JSONObject();
response.setContentType("application/json;charset=UTF-8");
String result = "";
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Set<String> keys = jedis.hkeys("ps");
int keyLen = keys.size();
result = String.valueOf(keyLen);
// Iterator<String> it=keys.iterator() ;
// while(it.hasNext()){
// String key = it.next();
// result+=key+" ";
// }
} catch (JedisConnectionException e){
log.error("jedis连接异常", e);
}
json.put("result", result);
json.writeJSONString(response.getWriter());
return;
}
3、
/**
* 清空ps域内所有key
* */
@RequestMapping(value = "/flushRedis.json", produces = "application/json; charset=UTF-8")
public void flushRedis(HttpServletRequest request, HttpServletResponse response)
throws Exception{
JSONObject json = new JSONObject();
response.setContentType("application/json;charset=UTF-8");
String result = "";
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Long aa = jedis.del("ps");
result = aa.toString();
} catch (JedisConnectionException e){
log.error("jedis连接异常", e);
}
json.put("result", result);
json.writeJSONString(response.getWriter());
return;
}
* 清空ps域内所有key
* */
@RequestMapping(value = "/flushRedis.json", produces = "application/json; charset=UTF-8")
public void flushRedis(HttpServletRequest request, HttpServletResponse response)
throws Exception{
JSONObject json = new JSONObject();
response.setContentType("application/json;charset=UTF-8");
String result = "";
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
Long aa = jedis.del("ps");
result = aa.toString();
} catch (JedisConnectionException e){
log.error("jedis连接异常", e);
}
json.put("result", result);
json.writeJSONString(response.getWriter());
return;
}
4、
/**
* 通过设置域ps的有效时间来设置域中所有key值的有效时间
* */
@RequestMapping(value = "/setRedisExpire.json", produces = "application/json; charset=UTF-8")
public void setRedisExpire(HttpServletRequest request, HttpServletResponse response)
throws Exception{
JSONObject json = new JSONObject();
response.setContentType("application/json;charset=UTF-8");
String result = "";
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
jedis.hset("ps", "KEY1", "VALUE1");
jedis.expire("ps", 5);//设置key1的有效时间为5s
System.out.println("KEY1值为:"+jedis.hget("ps", "KEY1"));
System.out.println("剩余时间:"+jedis.ttl("ps"));
Thread.sleep(5000);
System.out.println("剩余时间:"+jedis.ttl("ps"));
System.out.println("KEY1值为:"+jedis.hget("ps", "KEY1"));
} catch (JedisConnectionException e){
log.error("jedis连接异常", e);
}
json.put("result", result);
json.writeJSONString(response.getWriter());
return;
}
* 通过设置域ps的有效时间来设置域中所有key值的有效时间
* */
@RequestMapping(value = "/setRedisExpire.json", produces = "application/json; charset=UTF-8")
public void setRedisExpire(HttpServletRequest request, HttpServletResponse response)
throws Exception{
JSONObject json = new JSONObject();
response.setContentType("application/json;charset=UTF-8");
String result = "";
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
jedis.hset("ps", "KEY1", "VALUE1");
jedis.expire("ps", 5);//设置key1的有效时间为5s
System.out.println("KEY1值为:"+jedis.hget("ps", "KEY1"));
System.out.println("剩余时间:"+jedis.ttl("ps"));
Thread.sleep(5000);
System.out.println("剩余时间:"+jedis.ttl("ps"));
System.out.println("KEY1值为:"+jedis.hget("ps", "KEY1"));
} catch (JedisConnectionException e){
log.error("jedis连接异常", e);
}
json.put("result", result);
json.writeJSONString(response.getWriter());
return;
}