Redis常用操作
来源:互联网 发布:交大医学院网络 编辑:程序博客网 时间:2024/06/06 12:34
上接文章
首先可参考资料:http://www.cnblogs.com/edisonfeng/p/3571870.html
* */
基于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;
}
* 查看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;
}
阅读全文
0 0
- 常用redis操作命令
- redis常用操作
- redis常用操作
- Redis常用操作
- redis常用操作
- 常用的 redis操作
- php redis 常用操作
- redis常用操作
- redis的常用操作
- Redis常用操作
- Redis常用操作
- redis常用操作
- redis常用操作指令
- Redis常用操作命令
- redis操作常用
- redis 常用操作
- ServiceStack.Redis常用操作
- redis常用操作
- 蓝桥杯 分解质因数
- 学习:传输层之TCP协议
- Java检查异常和非检查异常区别
- LCS算法
- hibernate之where子句
- Redis常用操作
- C实现简单TCP的C/S模型(Linux & Mac OS系统)
- 我的第一个博客
- 【SQL】SQL中distinct的用法
- 查看class 文件编译的jdk 版本号
- DeepLearning 笔记(一) ImageNet Classification with Deep Convolutional Neural Networks
- 常用cron表达式
- 如何使用python处理JSON数据
- 简单记下httpclientjava实现get,post请求