Redis常用操作

上接文章   

基于Sentinel的Redis集群(主从&Sharding)的redis封装实现


首先可参考资料:http://www.cnblogs.com/edisonfeng/p/3571870.html


1、

/**

* 利用hset和hget插入并取redis数据,其中ps为域,key3、key4为键

* */
@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;
}


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;
}


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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值