redis学习笔记4: 在Java中操作Redis
Redis的Java客户端
Spring Date Redis使用方式
导入maven坐标
| |
| <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: |
| redis: |
| host: localhost |
| port: 6379 |
| password: 123456 |
| database: 0 |
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 |
| public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){ |
| log.info("开始创建redis模板类"); |
| RedisTemplate redisTemplate = new RedisTemplate(); |
| |
| redisTemplate.setKeySerializer(new StringRedisSerializer()); |
| |
| redisTemplate.setConnectionFactory(redisConnectionFactory); |
| return redisTemplate; |
| } |
通过RedisTemplate对象操作Redis
string
| public void testString(){ |
| redisTemplate.opsForValue().set("name","小明"); |
| |
| |
| String name = (String) redisTemplate.opsForValue().get("name"); |
| String city2 = (String) redisTemplate.opsForValue().get("city2"); |
| System.out.println(name); |
| System.out.println(city2); |
| |
| |
| |
| redisTemplate.opsForValue().set("code","1234",10, TimeUnit.SECONDS); |
| |
| |
| |
| redisTemplate.opsForValue().setIfAbsent("lock",1); |
| redisTemplate.opsForValue().setIfAbsent("lock",2); |
| } |
hash
| |
| public void testHash() { |
| |
| HashOperations hashOperations = redisTemplate.opsForHash(); |
| |
| hashOperations.put("100","name","tom"); |
| hashOperations.put("100","age","20"); |
| hashOperations.put("100","gender","male"); |
| |
| |
| String name = (String) hashOperations.get("100","name"); |
| System.out.println(name); |
| |
| |
| Set keys = hashOperations.keys("100"); |
| System.out.println(keys); |
| |
| |
| List values = hashOperations.values("100"); |
| System.out.println(values); |
| |
| |
| hashOperations.delete("100","gender"); |
| } |
set
| public void testSet1(){ |
| SetOperations setOperations = redisTemplate.opsForSet(); |
| |
| |
| setOperations.add("set1","aa","bb","cc"); |
| |
| |
| setOperations.members("set1"); |
| |
| |
| Long size = setOperations.size("set1"); |
| System.out.println(size); |
| |
| |
| System.out.println(setOperations.intersect("set1", "set2")); |
| |
| |
| System.out.println(setOperations.union("set1", "set2")); |
| |
| |
| setOperations.remove("set1","aa","bb"); |
| } |
list
| public void testList(){ |
| ListOperations listOperations = redisTemplate.opsForList(); |
| |
| |
| listOperations.leftPush("mylist","aa"); |
| listOperations.leftPush("mylist","bb"); |
| listOperations.leftPush("mylist","cc"); |
| listOperations.leftPush("mylist","dd"); |
| |
| |
| System.out.println(listOperations.size("mylist")); |
| |
| |
| System.out.println(listOperations.range("mylist", 0, -1)); |
| |
| |
| listOperations.rightPop("mylist"); |
| |
| |
| System.out.println(listOperations.size("mylist")); |
| } |
zset
| public void testZSet(){ |
| ZSetOperations zSetOperations = redisTemplate.opsForZSet(); |
| |
| |
| zSetOperations.add("zset1","a",19); |
| |
| |
| System.out.println(zSetOperations.range("zset1", 0, -1)); |
| |
| |
| System.out.println(zSetOperations.rangeByScoreWithScores("myset1", 0, -1)); |
| |
| |
| zSetOperations.incrementScore("zset1","c",10); |
| |
| |
| zSetOperations.remove("zset1","a","b"); |
| } |
通用命令操作
| public void testCommon(){ |
| |
| Set keys = redisTemplate.keys("*"); |
| System.out.println(keys); |
| |
| |
| Boolean name = redisTemplate.hasKey("name"); |
| |
| |
| for (Object key : keys) { |
| System.out.println(redisTemplate.type(key)); |
| } |
| |
| |
| redisTemplate.delete("mylist"); |
| } |
其他的
在图形化界面key正常, valule显示乱码, 通过java输出是正常的


不设置序列化器, 会使用默认的序列化器, key乱码, 可以在java中查询到


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现