Redis的Java客户端
Redis的Java客户端常用的有:
- Jedis
- Lettuce
- Spring Data Redis
Spring Data Redis是Spring的一部分,对Redis底层开发包进行了高度封装。在Spring项目中,可以使用Spring Data Redis来简化操作。
Spring Data Redis使用方式
操作步骤:
- 导入Spring Data Redis的Maven
- 配置Redis数据源
- 编写配置类,创建RedisTemplate对象
- 通过RedisTemplate对象操作Redis
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置Redis数据源详情
4.SpringBoot整合Redis
spring:redis:host:port:password:
@Configuration
@Slf4j
public class RedisConfiguration{@Beanpublic RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){log.info("开始创建Redis模板类...");RedisTemplate redisTemplate=new RedisTemplate();//设置key的序列化器,默认为JdkSerializationRedisSerializerredisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setConnectionFactory(redisConnectionFactory);return redisTemplate;}
}
/*** 操作字符串类型的数据*/@Testpublic void testString(){// set get setex setnx//setredisTemplate.opsForValue().set("city","北京");//getString city = (String)redisTemplate.opsForValue().get("city");System.out.println(city);//setex// set(key,value,timeout,TimeUnit) key:键,value:值,timeout:时间,TimeUnit:时间单位(枚举型)redisTemplate.opsForValue().set("code","1234",3, TimeUnit.MINUTES);//setnxredisTemplate.opsForValue().setIfAbsent("lock","1");redisTemplate.opsForValue().setIfAbsent("lock","2");}
/*** 操作哈希类型的数据*/@Testpublic void testHash(){//hset hget hdel hkeys hvalsHashOperations hashOperations = redisTemplate.opsForHash();//hsethashOperations.put("100","name","Tom");hashOperations.put("100","age","20");//hgetString name = (String) hashOperations.get("100", "name");System.out.println(name);//hkeysSet keys = hashOperations.keys("100");System.out.println(keys);//hvalsList values = hashOperations.values("100");System.out.println(values);//hdelhashOperations.delete("100","age");}
/*** 操作列表类型的数据*/@Testpublic void testList(){//lpush lrange rpop llenListOperations listOperations = redisTemplate.opsForList();//lpushlistOperations.leftPushAll("mylist","a","b","c");listOperations.leftPush("mylist","d");//lrangeList mylist = listOperations.range("mylist", 0, -1);System.out.println(mylist);//rpoplistOperations.rightPop("mylist");//llenLong size = listOperations.size("mylist");System.out.println(size);}
/*** 操作集合类型的数据*/@Testpublic void testSet(){//sadd smembers scard sinter sunion sremSetOperations setOperations = redisTemplate.opsForSet();//saddsetOperations.add("set1","a","b","c","d");setOperations.add("set2","a","b","x","y");//smembersSet set1 = setOperations.members("set1");System.out.println(set1);//scardLong size = setOperations.size("set1");System.out.println(size);//sinterSet intersect = setOperations.intersect("set1", "set2");System.out.println(intersect);//sunionSet union = setOperations.union("set1", "set2");System.out.println(union);//sremsetOperations.remove("set1","a","b");}
/*** 操作有序集合类型的数据*/@Testpublic void testZset(){//zadd zrange zincrby zremZSetOperations zSetOperations = redisTemplate.opsForZSet();//zaddzSetOperations.add("zset1","a",10);zSetOperations.add("zset1","b",12);zSetOperations.add("zset1","c",9);//zrangeSet zset1 = zSetOperations.range("zset1", 0, -1);System.out.println(zset1);//zincrbyzSetOperations.incrementScore("zset1","c",10);//zremzSetOperations.remove("zset1","a","b");}
/*** 通用命令操作*/@Testpublic void testCommon(){//keys exists type del//keysSet keys = redisTemplate.keys("*");System.out.println(keys);//existsBoolean name = redisTemplate.hasKey("name");Boolean set1 = redisTemplate.hasKey("set1");//typefor (Object key:keys){DataType type = redisTemplate.type(key);System.out.println(type);}//delredisTemplate.delete("mylist");}