redis学习(二)
2024-09-05 23:31:39
简单了解一下
1.build.gradle中添加 依赖 org.springframework.boot:spring-boot-starter-data-redis
//定义依赖:声明项目中需要哪些依赖
dependencies { //当前模块依赖项
//compile 'org.springframework.boot:spring-boot-starter'
//testCompile 'org.springframework.boot:spring-boot-starter-test' compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-data-redis'
testCompile group:'junit',name:'junit',version:'4.12'
2.application.properties中添加 redis的配置
#Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
3.程序入库处配置允许缓存 @EnableCaching
@SpringBootApplication
@EnableCaching
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} }
4.简单写入读取:
@RestController
public class TestController { @Autowired
private StringRedisTemplate stringRedisTemplate=new StringRedisTemplate(); @RequestMapping(value = "/getinfo")
public String getInfo(){
stringRedisTemplate.opsForValue().set("name","二庄");
stringRedisTemplate.opsForValue().set("age","18"); System.out.println("***姓名:"+stringRedisTemplate.opsForValue().get("name")+" 年龄:"+stringRedisTemplate.opsForValue().get("age"));
String result=String.format("***姓名:"+stringRedisTemplate.opsForValue().get("name")+" 年龄:"+stringRedisTemplate.opsForValue().get("age"));
return result;
}
中途遇到的问题:
问题1:存入redis的数据有中文,显示的是中文对应的Unicode编码
在启动redis-cli时在其后面加上--raw参数即可启动后 ,此时Unicode编码变成乱码了。
然后打开cmd的属性,设置字体为 Lucida Console,问题解决!
问题2: 使用 stringRedisTemplate.opsForValue().set("name","二庄"); 报错 :template not initialized; call afterPropertiesSet() before using it
原因: 没加@Autowired
@Autowired
private StringRedisTemplate stringRedisTemplate=new StringRedisTemplate();
最新文章
- MSP430FR2系列单片机破解芯片解密多少钱?
- Win+Ctrl键设置
- [LintCode] Min Stack 最小栈
- 开启 mysql 远程访问
- android学习视频分享
- OSPF+LVS ,qugga,vconfig,...感觉这些很有想法啊
- 自定义searchview的编辑框,搜索按钮,删除按钮,光标等
- Spring学习(14)--- 基于Java类的配置Bean 之 @ImportResource &; @Value 注解
- Android Picasso最详细的使用指南
- linux关机(重启)命令
- JsonBuilder初出茅庐
- python 全栈开发,Day120(路由系统, 实例化Flask的参数, 蓝图(BluePrint), before_request after_request)
- Android学习--------实现增删改查数据库操作以及实现相似微信好友对话管理操作
- install package
- iOS - Bundle 资源文件包
- 将日期转换为指定的格式:比如转换成 年月日时分秒 这种格式:yyyy-MM-dd hh:mm:ss 或者 yyyy-MM-dd。总结下。
- Python【每日一问】17
- 局域网内python socket实现windows与linux间简单的消息传送
- ES6常用对象操作整理
- AOP - PostSharp 2.0