首页 / REDIS / redis学习(二)
redis学习(二)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了redis学习(二),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2183字,纯文字阅读大概需要4分钟。
内容图文
简单了解一下
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();
原文:https://www.cnblogs.com/jerrys/p/10901368.html
内容总结
以上是互联网集市为您收集整理的redis学习(二)全部内容,希望文章能够帮你解决redis学习(二)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。