redis学习笔记4: 在Java中操作Redis
Redis的Java客户端
-
Jedis [命令和原生Redis基本相同]
-
Lettuce [性能高效]
-
Spring Date Redis [可以在Spring项目中使用, 简化操作]
Spring Date Redis使用方式
导入maven坐标
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>3.2.4</version>
</dependency>
其他版本管理方式
<properties>
<redis>3.2.4</redis>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>${redis}</version>
</dependency>
配置Redis数据源
application-dev
spring: #是spirng分级下的
redis:
host: localhost
port: 6379
password: 123456 #redis 5.0密码需要被""包裹
database: 0 #一共有16个库 DB0 不同数据库的数据是完全隔离的 [在有些情况可能不被识别]
application
spring:
redis:
host: ${spring.redis.host}
port: ${spring.redis.port}
password: ${spring.redis.password}}
database: ${spring.redis.database}
编写相关配置类, 创建RedisTemplate对象
@Configuration
@Slf4j
public class RedisConfiguration {
@Bean //将第三方类交给IOC容器管理
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
log.info("开始创建redis模板类");
RedisTemplate redisTemplate = new RedisTemplate();
//设置key的序列化器, 默认为JdkSerializationRedisSerializer
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置redis连接工厂对象
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
通过RedisTemplate对象操作Redis
string
public void testString(){
redisTemplate.opsForValue().set("name","小明");
//get key
String name = (String) redisTemplate.opsForValue().get("name");
String city2 = (String) redisTemplate.opsForValue().get("city2");
System.out.println(name);
System.out.println(city2);
//SETEX key seconds value 设置过期时间为seconds的key
//SETEX code 3 1234
redisTemplate.opsForValue().set("code","1234",10, TimeUnit.SECONDS); //使用枚举类写时间单位
//setNX 当key不存在时创建
//redis的string和java的String不是完全不同, 最后都会转化成redis字符串存储
redisTemplate.opsForValue().setIfAbsent("lock",1);
redisTemplate.opsForValue().setIfAbsent("lock",2);
}
hash
// @Test
public void testHash() {
//hset hget hkeys hvals
HashOperations hashOperations = redisTemplate.opsForHash();
//hset key filed value
hashOperations.put("100","name","tom");
hashOperations.put("100","age","20");
hashOperations.put("100","gender","male");
//hget key field
String name = (String) hashOperations.get("100","name");
System.out.println(name);
//hkeys key
Set keys = hashOperations.keys("100");
System.out.println(keys);
//hvals key
List values = hashOperations.values("100");
System.out.println(values);
//hdel key field
hashOperations.delete("100","gender");
}
set
public void testSet1(){
SetOperations setOperations = redisTemplate.opsForSet();
//sadd 插入新数据
setOperations.add("set1","aa","bb","cc");
//smembers 遍历
setOperations.members("set1");
//scard 元素数量
Long size = setOperations.size("set1");
System.out.println(size);
//sinter 交集
System.out.println(setOperations.intersect("set1", "set2"));
//sunion 并集
System.out.println(setOperations.union("set1", "set2"));
//srem 删除
setOperations.remove("set1","aa","bb");
}
list
public void testList(){
ListOperations listOperations = redisTemplate.opsForList();
//Lpush 左压栈
listOperations.leftPush("mylist","aa");
listOperations.leftPush("mylist","bb");
listOperations.leftPush("mylist","cc");
listOperations.leftPush("mylist","dd");
//Llen 元素数量
System.out.println(listOperations.size("mylist"));
//Lrange 遍历
System.out.println(listOperations.range("mylist", 0, -1));
//Rpop 右弹出
listOperations.rightPop("mylist");
//Llen 元素数量
System.out.println(listOperations.size("mylist"));
}
zset
public void testZSet(){
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
//zadd key value score 向zset插入数据
zSetOperations.add("zset1","a",19);
//zrange 遍历zset
System.out.println(zSetOperations.range("zset1", 0, -1));
//zrange key start end withScores 遍历zset并显示分数
System.out.println(zSetOperations.rangeByScoreWithScores("myset1", 0, -1));
//zincreby 为指定key的分数添加一个增量
zSetOperations.incrementScore("zset1","c",10);
//zrem 删除指定key
zSetOperations.remove("zset1","a","b");
}
通用命令操作
public void testCommon(){
//keys * 查询所有key
Set keys = redisTemplate.keys("*");
System.out.println(keys);
//exists 查询指定key是否存在
Boolean name = redisTemplate.hasKey("name");
//type 查询keys集合中所有数据的类型
for (Object key : keys) {
System.out.println(redisTemplate.type(key));
}
//del 根据key删除数据
redisTemplate.delete("mylist");
}
其他的
在图形化界面key正常, valule显示乱码, 通过java输出是正常的
不设置序列化器, 会使用默认的序列化器, key乱码, 可以在java中查询到
标签:Java,Redis,redis,System,key,println,redisTemplate,out From: https://www.cnblogs.com/HIK4RU44/p/18190716