1. Redis Java API的使用
redis java Api 和redis的命令很类似,可以参考下面的代码
1.1如果使用的是maven工程,引入下面的依赖
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.6.1</version> </dependency>
1.2 测试代码
package com.zimmor.redis; import redis.clients.jedis.Jedis; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; /** * @Author:zimmor * @Date: 2021/7/4 13:21 * @Description redis Java Api使用 * @version: 1.0.0 */ public class RedisDemo1 { //建立了连接的redis客户端jedis private static Jedis jedis; public static void main(String[] args) { //redis的java Api使用和redis的命令很类似 //和服务器建立连接 connectServer("192.168.204.129"); //String类型 testString(); //Hash类型 testHash(); //List类型 testList(); //Set类型 testSet(); //Zset类型 testZset(); } /** * redis Zset类型 */ private static void testZset() { jedis.zadd("myZset", 5, "spring"); jedis.zadd("myZset", 1, "springboot"); Long code = jedis.zadd("myZset", 2, "springmvc"); Long code2 = jedis.zadd("myZset", 2, "springData"); Set<String> myZset = jedis.zrange("myZset", 0, 10); System.out.println("Zset类型 返回值1:" + code); System.out.println("Zset类型 返回值2:" + code2); System.out.println("Zset类型 返回所有结果" + myZset); } /** * redis Set类型 */ private static void testSet() { Long code = jedis.sadd("nums", "1", "2", "5", "3", "7"); Set<String> numbers = jedis.smembers("nums"); System.out.println("Set类型返回值:" + code); System.out.println("Set类型返回结果" + numbers); } /** * redis List类型 */ private static void testList() { jedis.lpush("language", "英语"); jedis.rpush("language", "汉语"); Long code = jedis.lpush("language", "法语", "德语"); List<String> language = jedis.lrange("language", 0, 10); System.out.println("List类型返回值:" + code); System.out.println("List类型返回结果:" + language); } /** * 和redis服务器建立连接 * @param hostIp 主机ip */ private static void connectServer(String hostIp) { jedis = new Jedis(hostIp); } /** *redis String类型的操作 */ private static void testString() { String code = jedis.set("name", "nacos"); String result = jedis.get("name"); System.out.println("String类型 返回值" + code ); System.out.println("String类型 获取结果:" + result); } /** * redis Hash类型的操作 */ private static void testHash() { //redis中的hash对应的是java中的key-value都为字符串的Map Map myHash = new HashMap(); myHash.put("name", "niko"); myHash.put("age", "45"); String code = jedis.hmset("teacher", myHash); String name = jedis.hget("teacher", "name"); //获取map中的所有元素 Map<String, String> teacher = jedis.hgetAll("teacher"); System.out.println("Hash的返回值" + code ); System.out.println("Hash类型 获取name元素的值:" + name); System.out.println("Hash类型 获取的所有元素的结果" + teacher); } }
2.springboot中使用redis
2.1 引入相关依赖
<!--springDataRedis依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--测试时使用,如果不测试可以不引入--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
2.2 application.yml配置文件
spring: redis: host: 192.168.204.129
3.3 测试代码,代码是使用springboot的单元测试进行的测试
package com.zimmor.redis; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.*; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import java.util.List; import java.util.Set; /** * @Author:zimmor * @Date: 2021/7/4 16:31 * @Description 测试springboot中使用redis * @version: 1.0.0 */ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = RedisApplication.class) public class SpringDataRedisTest { @Autowired private RedisTemplate<String, String> redisTemplate; /** * redisTemplate使用时,通过ops***()方法得到相关类型的操作对象,然后的操作和jedis类似 */ /** * 测试String类型 */ @Test public void testString() { ValueOperations valueOperations = redisTemplate.opsForValue(); valueOperations.set("name", "java"); Object name = valueOperations.get("name"); System.out.println("name = " + name); } /** * 测试Hash类型 */ @Test public void testHash() { HashOperations hashOperations = redisTemplate.opsForHash(); hashOperations.put("user", "name", "tony"); hashOperations.put("user", "age", "18"); String name = hashOperations.get("user", "name").toString(); String age = hashOperations.get("user", "age").toString(); System.out.println("name = " + name); System.out.println("age = " + age); } /** * 测试List类型 */ @Test public void testList() { ListOperations listOperations = redisTemplate.opsForList(); listOperations.leftPush("language", "汉语"); listOperations.rightPush("language", "英语"); listOperations.leftPush("language", "法语"); listOperations.leftPush("language", "德语"); listOperations.leftPush("language", "西班牙语", "意大利语"); List language = listOperations.range("language", 0, 10); System.out.println("language = " + language); } /** * 测试Set类型 */ @Test public void testSet() { SetOperations setOperations = redisTemplate.opsForSet(); setOperations.add("numbers", "1", "2", "3", "5", "4"); Set numbers = setOperations.members("numbers"); System.out.println("numbers = " + numbers); } /** * 测试Zset类型 */ @Test public void testZset() { ZSetOperations zSetOperations = redisTemplate.opsForZSet(); zSetOperations.add("myZset", "Spring", 5); zSetOperations.add("myZset", "Springmvc", 51); zSetOperations.add("myZset", "Springboot", 1); zSetOperations.add("myZset", "Springcloud", 3); Set myZset = zSetOperations.range("myZset", 0, 10); System.out.println("myZset = " + myZset); } }