redis.clients
jedis
2.8.1
org.springframework.data
spring-data-redis
1.7.1.RELEASE
#redis中心
#绑定的主机地址
redis.host=127.0.0.1
#指定Redis监听端口,默认端口为6379
redis.port=6379
#授权密码(可以不使用,默认无密码,如果需要,去安装目录下修改配置)
redis.password=******
#最大空闲数:空闲链接数大于maxIdle时,将进行回收
redis.maxIdle=100
#最大连接数:能够同时建立的“最大链接个数”
redis.maxTotal=200
#最大等待时间:单位ms
redis.maxWait=1000
#使用连接时,检测连接是否成功
redis.testOnBorrow=false
#当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能
redis.timeout=10000
/**
* redis工具类
* @author cg
* @date 2018年11月27日
*/
@Service("redisDaoSupport")
public class RedisDaoSupport{
//使用注解,引入redisTemplate对象
@Resource(name="redisTemplate")
RedisTemplate redisTemplate;
/**
* 删除/判断(修改此处暂不写,其实就是先删再存)
*/
//删除key和对应的value
public void delete(String key){
redisTemplate.delete(key);
}
//删除多个key和对应的value
public void delete(List keys){
redisTemplate.delete(keys);
}
//判断是否含key
public Boolean hasKey(String key){
return redisTemplate.hasKey(key);
}
/**
* 方式一:以Object形式存取,其实就是强行把各类型.toString之后存取,注意原则怎么存的怎么取,然后强转
*/
//保存key-Object
public void set(String key,Object obj){
redisTemplate.opsForValue().set(key, obj);
}
//获取key-Object
public Object get(String key){
return redisTemplate.opsForValue().get(key);
}
//保存key-Object,同时设置失效时间(秒),也可毫秒,分,时,天,具体进入TimeUnit查看更改。因为redis版本过低 我不支持毫秒级别及以下的 过期时间类型,如有需求升高版本就行
public void setAndTimeOut(String key,Object obj, Integer expise){
redisTemplate.opsForValue().set(key, obj, expise,TimeUnit.SECONDS);
}
/**
* 方式二:以各类型结构形式存取各对象,使用redisTemplate包方法
*/
//String存储
public void setString(String key,String str){
redisTemplate.opsForValue().set(key, str);
}
//String存储(设置失效)
public void setStringAndTimeOut(String key,String str,Integer expise){
redisTemplate.opsForValue().set(key, str);
redisTemplate.expire(key, expise, TimeUnit.SECONDS);
}
//String获取
public String getString(String key){
return redisTemplate.opsForValue().get(key).toString();
}
//List存储
public void setList(String key,List
//##############jquery请求准备(简易的)###############
//redis测试
function testRedis() {
$.post(' http://127.0.0.1:8080/cg/appRedisDemo/redisDemo', {
}, function(rescnt) {
console.log(rescnt);
})
}
/**
* RedisDemo
* @author cg
* @date 2018年11月28日
*/
@Controller
@RequestMapping(value="/appRedisDemo")
public class RedisDemoController extends BaseController{
@Resource(name="redisDaoSupport")
private RedisDaoSupport redisDaoSupport;
/**
* 请讲接口 http://127.0.0.1:8080/项目名称/appRedisDemo/redisDemo.do
* demo展示的测试redisDaoSupport各封装方法,前提打开redis服务端
* 具体redis的应用场景->百度下即可
*/
@SuppressWarnings("unchecked")
@RequestMapping(value="/redisDemo")
@ResponseBody
public Object redis(){
Map map = new HashMap();
//为避免错误,通过以下步骤来清理也就是理解为初始化,测试做准备,完全没必要
redisDaoSupport.set("aaa","111" );// debug处
redisDaoSupport.set("bbb","222" );
redisDaoSupport.set("ccc","333" );
redisDaoSupport.set("ddd","444" );
redisDaoSupport.set("eee","555" );
System.err.println(redisDaoSupport.hasKey("aaa"));//debug处
System.err.println(redisDaoSupport.hasKey("bbb"));
System.err.println(redisDaoSupport.hasKey("ccc"));
System.err.println(redisDaoSupport.hasKey("ddd"));
System.err.println(redisDaoSupport.hasKey("eee"));
redisDaoSupport.delete("aaa");// debug处
redisDaoSupport.delete("bbb");
redisDaoSupport.delete("ccc");
redisDaoSupport.delete("ddd");
redisDaoSupport.delete("eee");
System.err.println(redisDaoSupport.hasKey("aaa"));//判断缓存里是否有key debug处
System.err.println(redisDaoSupport.hasKey("bbb"));//判断缓存里是否有key
System.err.println(redisDaoSupport.hasKey("ccc"));//判断缓存里是否有key
System.err.println(redisDaoSupport.hasKey("ddd"));//判断缓存里是否有key
System.err.println(redisDaoSupport.hasKey("eee"));//判断缓存里是否有key
//模拟数据
String value1="大家好";//String debug处
Map value2 = new HashMap();//Map
value2.put("name", "岗子");
value2.put("age", "22");
List value3 = new ArrayList();//List
value3.add("你们好啊1");
value3.add("你们好啊1");
Set value4 = new HashSet();//Set
value4.add("你们好啊2");
value4.add("你们好啊3");
Map value5 = new HashMap();//Map
List value6 = new ArrayList();//List
value6.add("你们好啊1");
value6.add("你们好啊1");
value5.put("xxxxxxxxxx", value3);
value5.put("yyyyyyyyyy", value6);
System.out.println("######################################################################");
//测试方式一(未设置失效情况)
redisDaoSupport.set("aaa",value1 );//debug处
redisDaoSupport.set("bbb",value2 );
redisDaoSupport.set("ccc",value3 );
redisDaoSupport.set("ddd",value4 );
redisDaoSupport.set("eee",value5 );
System.err.println(redisDaoSupport.get("aaa")); //debug处 输出:大家好
System.err.println(redisDaoSupport.get("bbb")); //debug处 输出:{name=admin, age=22}
System.err.println(redisDaoSupport.get("ccc")); //debug处 输出:[你们好啊1, 你们好啊1]
System.err.println(redisDaoSupport.get("ddd")); //debug处 输出:[你们好啊2, 你们好啊3]
System.err.println(redisDaoSupport.get("eee")); //debug处 输出: {xxxxxxxxxx=[你们好啊1, 你们好啊1], yyyyyyyyyy=[你们好啊1, 你们好啊1]}
System.err.println(redisDaoSupport.hasKey("aaa")); //debug处
System.err.println(redisDaoSupport.hasKey("bbb"));
System.err.println(redisDaoSupport.hasKey("ccc"));
System.err.println(redisDaoSupport.hasKey("ddd"));
redisDaoSupport.delete("aaa"); //debug处
redisDaoSupport.delete("bbb");
redisDaoSupport.delete("ccc");
redisDaoSupport.delete("ddd");
redisDaoSupport.delete("eee");
System.out.println("######################################################################");
//测试方式一(设置失效情况)
redisDaoSupport.setAndTimeOut("aaa", value1, 30);//debug处
redisDaoSupport.setAndTimeOut("bbb", value2, 90);
redisDaoSupport.setAndTimeOut("ccc", value3, 40);
redisDaoSupport.setAndTimeOut("ddd", value4, 40);
redisDaoSupport.setAndTimeOut("eee",value5,90 );
System.err.println(redisDaoSupport.hasKey("aaa"));//debug处
System.err.println(redisDaoSupport.hasKey("bbb"));
System.err.println(redisDaoSupport.hasKey("ccc"));
System.err.println(redisDaoSupport.hasKey("ddd"));
System.err.println(redisDaoSupport.hasKey("eee"));
System.err.println(redisDaoSupport.get("aaa")); //根据失效时间,来进行debug下一步
System.err.println(redisDaoSupport.get("bbb")); //根据失效时间,debug处
System.err.println(redisDaoSupport.get("ccc")); //根据失效时间,debug处
System.err.println(redisDaoSupport.get("ddd")); //根据失效时间,debug处
System.err.println(redisDaoSupport.get("eee")); //根据失效时间,debug处
redisDaoSupport.delete("aaa");//debug处
redisDaoSupport.delete("bbb");
redisDaoSupport.delete("ccc");
redisDaoSupport.delete("ddd");
redisDaoSupport.delete("eee");
System.out.println("######################################################################");
//测试方式二(未设置失效情况)
redisDaoSupport.setString("aaa",value1 );// debug处
redisDaoSupport.setMap("bbb",value2 );
redisDaoSupport.setList("ccc",value3 );
redisDaoSupport.setSet("ddd",value4 );
redisDaoSupport.setMap("eee",value5 );
System.err.println(redisDaoSupport.getString("aaa"));//debug处 输出:大家好
System.err.println(redisDaoSupport.getMap("bbb"));//debug处 输出:{name=admin, age=22}
System.err.println(redisDaoSupport.getList("ccc"));//debug处 输出:[你们好啊1, 你们好啊1]
System.err.println(redisDaoSupport.getSet("ddd"));//debug处 输出:[[你们好啊2, 你们好啊3]] 通过此处初步就可看出两种方式的不同,后面也会通过redis可视化工具看出
System.err.println(redisDaoSupport.getMap("eee"));//debug处 输出:{xxxxxxxxxx=[你们好啊1, 你们好啊1], yyyyyyyyyy=[你们好啊1, 你们好啊1]}
System.err.println(redisDaoSupport.getMapValues("bbb"));//debug处 输出:[admin, 22]
System.err.println(redisDaoSupport.getValueByMapKey("bbb", "age"));//debug处 输出:22
redisDaoSupport.delete("aaa");//debug处
redisDaoSupport.delete("bbb");
redisDaoSupport.delete("ccc");
redisDaoSupport.delete("ddd");
redisDaoSupport.delete("eee");
System.out.println("######################################################################");
//测试方式二(设置失效情况)
redisDaoSupport.setStringAndTimeOut("aaa", value1, 30);//代表30秒后失效 debug处
redisDaoSupport.setMapAndTimeOut("bbb", value2, 60);
redisDaoSupport.setListAndTimeOut("ccc", value3, 90);
redisDaoSupport.setSetAndTimeOut("ddd", value4, 120);
redisDaoSupport.setMapAndTimeOut("eee", value5, 150);
System.err.println(redisDaoSupport.getString("aaa"));//根据失效时间,debug处
System.err.println(redisDaoSupport.getMap("bbb"));//根据失效时间,debug处
System.err.println(redisDaoSupport.getList("ccc"));//根据失效时间,debug处
System.err.println(redisDaoSupport.getSet("ddd"));//根据失效时间,debug处
System.err.println(redisDaoSupport.getMap("eee"));//根据失效时间,debug处
System.err.println(redisDaoSupport.getValueByMapKey("eee", "xxxxxxxxxx"));//根据失效时间,debug处
redisDaoSupport.delete("aaa");//debug处
redisDaoSupport.delete("bbb");
redisDaoSupport.delete("ccc");
redisDaoSupport.delete("ddd");
redisDaoSupport.delete("eee");
System.out.println("######################################################################");
//测试方式一存储,方式二获取(会发现除String类型外,其余类型获取会报redis里的结构转换错误(nested exception is redis.clients.jedis.exceptions.JedisDataException:),可以大致判断redisTemplate最底层是使用用了Jedis)
redisDaoSupport.set("aaa",value1 );//debug处
redisDaoSupport.set("bbb",value2 );
redisDaoSupport.set("ccc",value3 );
redisDaoSupport.set("ddd",value4 );
redisDaoSupport.set("eee",value5 );
System.err.println(redisDaoSupport.getString("aaa"));//debug处
System.err.println(redisDaoSupport.getMap("bbb"));//debug处 报错:WRONGTYPE Operation against a key holding the wrong kind of value; nested exception is redis.clients.jedis.exceptions.JedisDataException: WRONGTYPE Operation against a key holding the wrong kind of value
System.err.println(redisDaoSupport.getList("ccc"));//debug处
System.err.println(redisDaoSupport.getSet("ddd"));//debug处
System.err.println(redisDaoSupport.getMap("eee"));//debug处
//测试方式二存储,方式一获取(会发现除String类型外,其余类型获取会报redis里的结构转换错误,如上)
redisDaoSupport.setString("aaa",value1 );// debug处
redisDaoSupport.setMap("bbb",value2 );
redisDaoSupport.setList("ccc",value3 );
redisDaoSupport.setSet("ddd",value4 );
redisDaoSupport.setMap("eee",value5 );
System.err.println(redisDaoSupport.get("aaa"));// debug处
System.err.println(redisDaoSupport.get("bbb"));// debug处
System.err.println(redisDaoSupport.get("ccc"));// debug处
System.err.println(redisDaoSupport.get("ddd"));// debug处
System.err.println(redisDaoSupport.get("eee"));// debug处
System.out.println("######################################################################");
redisDaoSupport.delete("aaa");
redisDaoSupport.delete("bbb");
redisDaoSupport.delete("ccc");
redisDaoSupport.delete("ddd");
redisDaoSupport.delete("eee");
System.err.println(redisDaoSupport.hasKey("aaa"));
System.err.println(redisDaoSupport.hasKey("bbb"));
System.err.println(redisDaoSupport.hasKey("ccc"));
System.err.println(redisDaoSupport.hasKey("ddd"));
System.err.println(redisDaoSupport.hasKey("eee"));
System.out.println("######################################################################");
//测试方式一存储Map,获取时对Object进行类型强转Map>
redisDaoSupport.set("eee",value5);//存储Map
Map> map2 =(Map>)redisDaoSupport.get("eee");
List list = map2.get("xxxxxxxxxx");
for (Object object : list) {
System.err.println(object); // debug处
}
System.out.println("######################################################################");
map.put("result", "测试完成");
return AppUtil.returnObject(new PageData(), map);
}
}
好了,我也不知道我说了啥,写了一大堆,也不知道理解是不是有误,感谢大家查看,评论就关了,若有疑问或错误恳请邮件通知,感谢探讨,谢谢。[email protected]