Redis入门2
在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: localhost
port: 6379
password: root
database: 0
3.编写配置类,创建RedisTemplate对象
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate redisTemplate = new RedisTemplate();
//设置redis的连接工厂对象
redisTemplate.setConnectionFactory(redisConnectionFactory);
//设置redis key的序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
4.通过RedisTemplate对象操作Redis
@Autowired
private RedisTemplate redisTemplate;
@Test
public void contextLoads() {
//得到对应操作5种redis数据类型的对象
ValueOperations valueOperations = redisTemplate.opsForValue();
HashOperations hashOperations = redisTemplate.opsForHash();
ListOperations listOperations = redisTemplate.opsForList();
SetOperations setoperations =redisTemplate.opsForSet();
ZSetOperations zSetOperations =redisTemplate.opsForZSet();
}
字符串类型操作
/**
* 操作字符串类型的数据
*/
@Test
public void testString(){
//set
redisTemplate.opsForValue().set("city","北京");
//get
String city = (String) redisTemplate.opsForValue().get("city");
System.out.println("city = " + city);
//setex
redisTemplate.opsForValue().set("code","1234",4, TimeUnit.MINUTES);
//setnx
redisTemplate.opsForValue().setIfAbsent("lock","1");
redisTemplate.opsForValue().setIfAbsent("lock","2");
}
哈希类型操作
/**
* 操作哈希类型的数据
*/
@Test
public void testHash(){
HashOperations hashOperations = redisTemplate.opsForHash();
//hset
hashOperations.put("100","name","tom");
hashOperations.put("100","age","20");
//hget
String name = (String) hashOperations.get("100", "name");
System.out.println("name = " + name);
//hkeys
Set keys = hashOperations.keys("100");
System.out.println("keys = " + keys);
//hvals
List values = hashOperations.values("100");
System.out.println("values = " + values);
//hdel
hashOperations.delete("100", "age");
}
列表类型操作
/**
* 操作列表类型的数据
*/
@Test
public void testList() {
//lpush lrange rpop llen
ListOperations listOperations = redisTemplate.opsForList();
//lpush
listOperations.leftPushAll("mylist", "a", "b", "c");
listOperations.leftPush("mylist", "d");
//lrange
List mylist = listOperations.range("mylist", 0, -1);
System.out.println(mylist);
//llen
Long size = listOperations.size("mylist");
System.out.println(size);
//rpop
listOperations.rightPop("mylist");
}
集合类型操作
/**
* 操作集合类型的数据
*/
@Test
public void testset() {
//sadd smembers scard sinter sunion srem
SetOperations 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");
}
有序集合类型操作
/**
* 操作有序集合类型的数据
*/
@Test
public void testZset() {
//zadd zrange zincrby zrem
ZSetOperations 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");
}
通用命令
/**
* 通用命令操作
*/
@Test
public void testcommon() {
//keys exists type del
//keys
Set keys = redisTemplate.keys("*");
System.out.println(keys);
//exists
Boolean name = redisTemplate.hasKey("name");
Boolean set1 = redisTemplate.hasKey("set1");
System.out.println("name = " + name);
System.out.println("set1 = " + set1);
//type
for (Object key : keys) {
DataType type = redisTemplate.type(key);
System.out.println(type.name());
}
//del
redisTemplate.delete("mylist");
}