springboot整合redis进行数据操作(一)
来源:互联网 发布:确保成本测算数据准确 编辑:程序博客网 时间:2024/06/08 09:43
redis是一种常见的nosql,日常开发中,我们使用它的频率比较高,因为它的多种数据接口,很多场景中我们都可以用到,并且redis对分布式这块做的非常好。
springboot整合redis比较简单,并且使用redistemplate可以让我们更加方便的对数据进行操作。
1、添加依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
2、在application.properties中加入相关配置
spring.redis.database=0spring.redis.host=127.0.0.1spring.redis.port=6379spring.redis.password=spring.redis.pool.max-idle=8spring.redis.pool.min-idle=0spring.redis.pool.max-active=8spring.redis.pool.max-wait=-1spring.redis.timeout=50003、编写配置类
import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;@Configuration@EnableCachingpublic class RedisConfig { @Bean public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) { CacheManager cacheManager = new RedisCacheManager(redisTemplate); return cacheManager; } @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); redisTemplate.setConnectionFactory(factory); return redisTemplate; } @Bean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) { StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); stringRedisTemplate.setConnectionFactory(factory); return stringRedisTemplate; }}
这里定义了两个bean,一个是redisTemplate,另一个是stringRedisTemplate,它们的序列化方式不同,前者默认jdk序列方式,后者默认string的序列化方式,后者一般专门用于存储string格式,前者我们可以用来保存对象等,这里我们都配置上,根据不同业务进行不同使用。
4、编写实体类
public class User implements Serializable{/** * */private static final long serialVersionUID = 3221700752972709820L;private int id;private String name;private int age;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public User(int id, String name, int age) {super();this.id = id;this.name = name;this.age = age;}}
5、编写测试service
@Servicepublic class UserService {@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Autowiredprivate RedisTemplate<String, Object> redisTemplate;public void set(String key, User user) {redisTemplate.opsForValue().set(key, user);}public User get(String key) {return (User) redisTemplate.boundValueOps(key).get();}public void setCode(String key, String code) {stringRedisTemplate.opsForValue().set(key, code, 60, TimeUnit.SECONDS);}public String getCode(String key) {return stringRedisTemplate.boundValueOps(key).get();}}
这里我们模拟两种操作,一种是根据key存储user对象,另一种是存储key value均为string的操作,并且赋予数据过期时间,这种操作我们可以用于验证码存储,在setcode方法中,我们存储了一个有效时长为60s的数据,当60s过后,数据会自动销毁。
6、编写测试controller访问
@RestController@RequestMapping("rest_redis")public class RedisController {@Resourceprivate UserService userService;@GetMapping("set")public void set() {userService.set("key1", new User(1, "meepoguan", 26));}@GetMapping("get")public String get() {return userService.get("key1").getName();}@GetMapping("stringset")public void stringset() {userService.setCode("stringkey", "meepoguan_coke");}@GetMapping("stringget")public String stringget() {return userService.getCode("stringkey");}}对service中的方法进行测试。
阅读全文
1 0
- springboot整合redis进行数据操作(一)
- springboot整合redis进行数据操作
- SpringBoot(三)整合Redis
- springboot整合redis(二)
- spring整合redis(2)spring操作redis数据
- springboot与redis整合案例(上)
- SpringBoot学习:整合Redis
- Springboot整合Redis
- springboot整合redis缓存
- springboot整合redis
- Springboot+redis 整合
- SpringBoot整合Redis
- Springboot整合Redis
- springBoot整合redis
- Springboot整合Redis缓存
- springboot整合redis集群
- springboot整合redis
- springBoot整合redis报错报错
- 不求甚解-zookeeper
- Excel根据身份证号提取省份
- 窗体泄露(存在问题)
- java反射例子
- 欢迎使用CSDN-markdown编辑器
- springboot整合redis进行数据操作(一)
- 滴滴打车,打车软件app实现。小车在地图上平滑移动的实现,Android,基于高德地图开发实现
- 对用户进行操作日志审计
- 支付宝:sign check fail: check Sign and Data Fail!JSON also!
- LeetCode 55 Jump Game
- 传奇私服 搭建
- 千投量化体验:采用均线加风控建模(一)
- UIP和lwip的区别
- 基于R语言的Kaggle案例分析学习笔记(二)