在java中操作Redis
Redis的Java客户端
Redis 的 Java 客户端很多,常用的几种:
Jedis
Lettuce
Spring Data Redis
Spring Data Redis 是 Spring 的一部分,对 Redis 底层开发包进行了高度封装。
在 Spring 项目中,可以使用Spring Data Redis来简化操作。
Spring Data Redis使用方式
1.导入Spring Data Redis 的maven坐标
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
2.配置Redis数据源
spring:data:redis:host: localhostport: 6379password: rootdatabase: 0
3.编写配置类,创建RedisTemplate对象
@Beanpublic RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){RedisTemplate redisTemplate = new RedisTemplate();//设置redis的连接工厂对象redisTemplate.setConnectionFactory(redisConnectionFactory);//设置redis key的序列化器redisTemplate.setKeySerializer(new StringRedisSerializer());return redisTemplate;}
4.通过RedisTemplate对象操作Redis
@Autowiredprivate RedisTemplate redisTemplate; @Testpublic void contextLoads() {//得到对应操作5种redis数据类型的对象ValueOperations valueOperations = redisTemplate.opsForValue();HashOperations hashOperations = redisTemplate.opsForHash();ListOperations listOperations = redisTemplate.opsForList();SetOperations setoperations =redisTemplate.opsForSet();ZSetOperations zSetOperations =redisTemplate.opsForZSet();}
字符串类型操作
/*** 操作字符串类型的数据*/@Testpublic void testString(){//setredisTemplate.opsForValue().set("city","北京");//getString city = (String) redisTemplate.opsForValue().get("city");System.out.println("city = " + city);//setexredisTemplate.opsForValue().set("code","1234",4, TimeUnit.MINUTES);//setnxredisTemplate.opsForValue().setIfAbsent("lock","1");redisTemplate.opsForValue().setIfAbsent("lock","2");}
哈希类型操作
/*** 操作哈希类型的数据*/@Testpublic void testHash(){HashOperations hashOperations = redisTemplate.opsForHash();//hsethashOperations.put("100","name","tom");hashOperations.put("100","age","20");//hgetString name = (String) hashOperations.get("100", "name");System.out.println("name = " + name);//hkeysSet keys = hashOperations.keys("100");System.out.println("keys = " + keys);//hvalsList values = hashOperations.values("100");System.out.println("values = " + 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);//llenLong size = listOperations.size("mylist");System.out.println(size);//rpoplistOperations.rightPop("mylist");}
集合类型操作
/*** 操作集合类型的数据*/@Testpublic void testset() {//sadd smembers scard sinter sunion sremSetOperations setOperations = redisTemplate.opsForSet();//sadd 添加setOperations.add("set1", "a", "b", "c", "d");setOperations.add("set2", "a", "b", "x", "y");//smembers 返回集合中所有元素Set members = setOperations.members("set1");System.out.println(members);//scard 获取集合的成员数Long size = setOperations.size("set1");System.out.println(size);//sinter 返回给定所有集合的交集Set intersect = setOperations.intersect("set1", "set2");System.out.println(intersect);//sunion 返回给定所有集合的并集Set union = setOperations.union("set1", "set2");System.out.println(union);//srem 删除集合中一个或多个成员setOperations.remove("set1", "a", "b");}
有序集合类型操作
/*** 操作有序集合类型的数据*/@Testpublic void testZset() {//zadd zrange zincrby zremZSetOperations zSetOperations = redisTemplate.opsForZSet();//zadd 添加zSetOperations.add("zset1", "a", 10);zSetOperations.add("zset1", "b", 12);zSetOperations.add("zset1", "c", 9);//zrange 获取区间内的元素Set zset1 = zSetOperations.range("zset1", 0, -1);System.out.println(zset1);//zincrby 有序集合中对指定成员的分数加上增量zSetOperations.incrementScore("zset1", "c", 10);//zrem 删除zSetOperations.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");System.out.println("name = " + name);System.out.println("set1 = " + set1);//typefor (Object key : keys) {DataType type = redisTemplate.type(key);System.out.println(type.name());}//delredisTemplate.delete("mylist");}