前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot整合redis

springboot整合redis

作者头像
chenchenchen
发布2019-09-02 16:28:38
5470
发布2019-09-02 16:28:38
举报
文章被收录于专栏:chenchenchenchenchenchen

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/qq_34409255/article/details/80196578

1、引入maven配置

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>

2、配置application.properties

#默认访问索引为0的库spring.redis.database=0#redis服务器地址spring.redis.host=192.168.52.128#端口spring.redis.port=6379# 目标为保持在池中的最小空闲连接数。这个设置只有在正面的情况下才有效果spring.redis.jedis.pool.min-idle=0#池在给定时间可以分配的最大连接数。使用负值无限制。spring.redis.jedis.pool.max-active=8#池中“空闲”连接的最大数量。使用负值表示无限数量的空闲连接。spring.redis.jedis.pool.max-idle=8#连接分配在池被耗尽时抛出异常之前应该阻塞的最长时间量(以毫秒为单位)。使用负值可以无限期地阻止。spring.redis.jedis.pool.max-wait=-1#以毫秒为单位的连接超时。spring.redis.timeout=1000

3、创建RedisComponent类,注入StringRedisTemplate

package cn.itfeiyue.springbootredis.config;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.stereotype.Component;/** * @Description: java类作用描述 * @Author: ZHANGPENGFEI * @CreateDate: 2018/5/4 15:45 * @Version: 1.0 */@Componentpublic class RedisComponent { @Autowired private StringRedisTemplate stringRedisTemplate; //保存 public void set(String key, String value){ //获取ValueOperations对象 (这里对应Redis中数据类型String类型,还有HashOperations对应Hash类型<参见博客:“https://blog.csdn.net/qq_34409255/article/details/80198458”>) ValueOperations<String,String> ops = this.stringRedisTemplate.opsForValue(); //判断当前库中是否存在该key boolean bExistent = this.stringRedisTemplate.hasKey(key); if (bExistent) { System.out.println("this key is bExistent!"); }else{ ops.set(key, value); } } //根据key获取value public String get(String key){ return this.stringRedisTemplate.opsForValue().get(key); } //根据key删除对应的信息 public void del(String key){ this.stringRedisTemplate.delete(key); }}

4、创建Controller进行测试

package cn.itfeiyue.springbootredis.controller;import cn.itfeiyue.springbootredis.config.RedisComponent;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @Description: java类作用描述 * @Author: ZHANGPENGFEI * @CreateDate: 2018/5/4 15:46 * @Version: 1.0 */@RestController@RequestMapping(value="/web")public class WebController { @Autowired private RedisComponent redisComponet; @RequestMapping(value="/set/{key}/{value}") public String set(@PathVariable String key, @PathVariable String value){ redisComponet.set(key, value); return "set key succ!"; } @RequestMapping(value="/get/{key}") public String get(@PathVariable String key){ return redisComponet.get(key); } @RequestMapping(value="/del/{key}") public void del(@PathVariable String key){ redisComponet.del(key); }}

5、效果图

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年05月04日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档