您好,欢迎访问一九零五行业门户网

Java与SpringBoot对redis的使用方式是什么

1.java连接redisredis支持哪些语言可以操作 (去redis官网查询)
1.1 使用jedis (1)添加jedis依赖
<dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.12</version> <!--只能在测试类中使用--> <scope>test</scope> </dependency> <dependency> <groupid>redis.clients</groupid> <artifactid>jedis</artifactid> <version>3.6.0</version> </dependency>
(2)代码测试
public class testjedis { @test public void test01(){ //连接redis--必须保证你的redis服务运行远程连接 //该对象把每个redis命令封装成对应的方法 //注意端口号 //xshell中的redis要运行起来,注意防火墙释放端口号,注意配置文件的修改 jedis jedis=new jedis("192.168.1.16",6379); //对于字符串操作的命令 string set = jedis.set("k1", "v1"); system.out.println(set); string set1 = jedis.set("k2", "v2"); system.out.println(set1); string set2 = jedis.set("k3", "v3"); system.out.println(set2); //对于hash的操作 jedis.hset("k4","name","小花"); long hset = jedis.hset("k4", "age", "18"); system.out.println(hset); map<string ,string> map=new hashmap<>(); map.put("name","小明"); map.put("age","20"); long k = jedis.hset("k5", map); system.out.println(k); jedis.close(); }}
1.2 使用连接池连接redis @test public void test02(){ //创建连接池的配置类 jedispoolconfig jedispoolconfig=new jedispoolconfig(); jedispoolconfig.setmaxidle(20); jedispoolconfig.setminidle(5); jedispoolconfig.setmaxwait(duration.ofmillis(3000)); jedispool jedispool=new jedispool(jedispoolconfig,"192.168.1.16",6379); long start = system.currenttimemillis(); for (int i = 0; i < 1000; i++) { //从jedis连接池获取资源 jedis jedis=jedispool.getresource(); string ping = jedis.ping(); jedis.close();//是否关闭池子 } long end=system.currenttimemillis(); //是为了比较使用池子还是不使用快,结论是使用池子快 system.out.println("总耗时:"+(end-start)); }
1.3 java连接redis集群模式连接集群时要保证集群里面没有存值, 要是存值需要删除之前生成的文件(注意删除干净)
还有就是放行对应的端口:6001、6002、6003、6004、6005、6006,因为之前放行的实在10000端口,注意以上两点,才可以使用idea创建成功。
@test public void test03(){ set<hostandport> nodes=new hashset<>(); nodes.add(new hostandport("192.168.227.175",6001)); nodes.add(new hostandport("192.168.227.175",6002)); nodes.add(new hostandport("192.168.227.175",6003)); nodes.add(new hostandport("192.168.227.175",6004)); nodes.add(new hostandport("192.168.227.175",6005)); nodes.add(new hostandport("192.168.227.175",6006)); jediscluster jediscluster=new jediscluster(nodes); jediscluster.set("k6", "小老虎和小兔子"); jediscluster.close(); }
2.springboot整合redisspringboot对redis的操作封装了两个stringredistemplate和redistemplate类,stringredistemplate是redistemplate的子类,stringredistemplate它只能存储字符串类型,无法存储对象类型。要想用stringredistemplate存储对象必须把对象转为json字符串。
springboot整合redis时提供了两个模板工具类,stringredistemplate和redistemplate。
2.1 stringredistemplate(1) 引入相关的依赖
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> </dependency>
(2)注入stringredistemplate该类对象
@autowired private stringredistemplate redistemplate;
(3)使用stringredistemplate
该类把对每种数据类型的操作,单独封了相应的内部类。
此处不会有乱码,因为已经给它序列化方式和反序列化方式换成为string型。
@autowired private stringredistemplate stringredistemplate; @test public void test01(){ //对hash类型的操作 hashoperations<string, object, object> forhash = stringredistemplate.opsforhash(); forhash.put("k1","name","张三"); forhash.put("k1","age","15"); map<string,string> map=new hashmap<>(); map.put("name","李四"); map.put("age","25"); forhash.putall("k36",map); object o = forhash.get("k1", "name"); system.out.println(o); set<object> k1 = forhash.keys("k1"); system.out.println(k1); list<object> k11 = forhash.values("k1"); system.out.println(k11); //获取k1对于的所有的field和value map<object, object> k12 = forhash.entries("k1"); system.out.println(k12); } @test void contextloads() { //删除指定的key // stringredistemplate.delete("k"); //查看所有的key //stringredistemplate.keys("k"); //是否存在指定的key //stringredistemplate.haskey("k"); //对字符串数据类型的操作valueoperations valueoperations<string, string> forvalue = stringredistemplate.opsforvalue(); //存储字符串类型--key value long uint setex() forvalue.set("k1","张三",30, timeunit.seconds); //等价于setnx 存入成功返回true ,失败返回false boolean absent = forvalue.setifabsent("k11", "李四", 30, timeunit.seconds); system.out.println(absent); //append拼接 integer append = forvalue.append("k11", "真好看"); string k11 = forvalue.get("k11"); system.out.println(k11); }
2.2 redistemplate此处会有乱码,因为它序列化方式和反序列化方式默认为jdk。
@springboottestclass sbredisapplicationtests02 { //当你存储的value类型为对象类型使用redistemplate //存储的value类型为字符串。stringredistemplate 验证码 @autowired private redistemplate redistemplate; @test public void test01(){ //必须认为指定序列化方式 redistemplate.setkeyserializer(new stringredisserializer()); redistemplate.setvalueserializer(new jackson2jsonredisserializer<object>(object.class)); //对string类型操作类 valueoperations forvalue = redistemplate.opsforvalue(); //redis中key和value都变成了乱码 //key和value都没有指定序列化方式,默认采用jdk的序列化方式 forvalue.set("k1","张三"); //value默认采用jdk,类必须实现序列化接口 forvalue.set("k44",new user(1,"haha",12)); }}
上面的redistemplate需要每次都指定key value以及field的序列化方式,能不能搞一个配置类,已经为redistemplate指定好序列化。以后再用就无需指定。
@configurationpublic class redisconfig { @bean public redistemplate<string, object> redistemplate(redisconnectionfactory factory) { redistemplate<string, object> template = new redistemplate<>(); redisserializer<string> redisserializer = new stringredisserializer(); jackson2jsonredisserializer jackson2jsonredisserializer = new jackson2jsonredisserializer(object.class); objectmapper om = new objectmapper(); om.setvisibility(propertyaccessor.all, jsonautodetect.visibility.any); om.enabledefaulttyping(objectmapper.defaulttyping.non_final); jackson2jsonredisserializer.setobjectmapper(om); template.setconnectionfactory(factory); //key序列化方式 template.setkeyserializer(redisserializer); //value序列化 template.setvalueserializer(jackson2jsonredisserializer); //value hashmap序列化 filed value template.sethashvalueserializer(jackson2jsonredisserializer); template.sethashkeyserializer(redisserializer); return template; }}
以上就是java与springboot对redis的使用方式是什么的详细内容。
其它类似信息

推荐信息