Redis在项目中的使用(JedisPool方式)
2、springboot的习惯优于配置。也在项目中使用了application.yml文件配置mysql的基本配置项。这里也在application.yml里面配置redis的配置项。
spring: datasource: # 驱动配置信息 url: jdbc:mysql://localhost:3306/spring_boot useUnicode=true characterEncoding=utf8 username: root password: root type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver # 连接池的配置信息 filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select "x" testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 redis: host: 127.0.0.1 port: 6379 password: pass1234 pool: max-active: 100 max-idle: 10 max-wait: 100000 timeout: 0springboot中redis相关类 项目操作redis是使用的RedisTemplate方式,另外还可以完全使用JedisPool和Jedis来操作redis。整合的内容也是从网上收集整合而来,网上整合的方式和方法非常的多,有使用注解形式的,有使用Jackson2JsonRedisSerializer来序列化和反序列化key value的值等等,很多很多。这里使用的是我认为比较容易理解和掌握的,基于JedisPool配置,使用RedisTemplate来操作redis的方式。
redis单独放在一个包redis里,在包里先创建RedisConfig.java文件。
RedisConfig.java
@Configuration @EnableAutoConfiguration public class RedisConfig { @Bean @ConfigurationProperties(prefix = "spring.redis.pool") public JedisPoolConfig getRedisConfig(){ JedisPoolConfig config = new JedisPoolConfig(); return config; @Bean @ConfigurationProperties(prefix = "spring.redis") public JedisConnectionFactory getConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setUsePool(true); JedisPoolConfig config = getRedisConfig(); factory.setPoolConfig(config); return factory; @Bean public RedisTemplate , getRedisTemplate() { JedisConnectionFactory factory = getConnectionFactory(); RedisTemplate , template = new StringRedisTemplate(factory); return template; }
public boolean set(final String key, final String value) { boolean result = redisTemplate.execute(new RedisCallback Boolean () { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer String serializer = redisTemplate.getStringSerializer(); connection.set(serializer.serialize(key), serializer.serialize(value)); return true; return result; @Override public String get(final String key) { String result = redisTemplate.execute(new RedisCallback String () { @Override public String doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer String serializer = redisTemplate.getStringSerializer(); byte[] value = connection.get(serializer.serialize(key)); return serializer.deserialize(value); return result; @Override public boolean expire(final String key, long expire) { return redisTemplate.expire(key, expire, TimeUnit.SECONDS); @Override public boolean remove(final String key) { boolean result = redisTemplate.execute(new RedisCallback Boolean () { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer String serializer = redisTemplate.getStringSerializer(); connection.del(key.getBytes()); return true; return result; }
在这里execute()方法具体的底层没有去研究,只知道这样能实现对于redis数据的操作。
redis保存的数据会在内存和硬盘上存储,所以需要做序列化;这个里面使用的StringRedisSerializer来做序列化,不过这个方式的泛型指定的是String,只能传String进来。所以项目中采用json字符串做redis的交互。
到此,redis在springboot中的整合已经完毕,下面就来测试使用一下。
5. springboot项目中使用redis在这里就直接使用springboot项目中自带的单元测试类SpringbootApplicationTests进行测试。
@RunWith(SpringRunner.class) @SpringBootTest public class SpringbootApplicationTests { private JSONObject json = new JSONObject(); @Autowired private RedisService redisService; @Test public void contextLoads() throws Exception {
public void setString() { redisService.set("redis_string_test", "springboot redis test"); * 获取字符串 @Test public void getString() { String result = redisService.get("redis_string_test"); System.out.println(result); * 插入对象 @Test public void setObject() { Person person = new Person("person", "male"); redisService.set("redis_obj_test", json.toJSONString(person)); * 获取对象 @Test public void getObject() { String result = redisService.get("redis_obj_test"); Person person = json.parseObject(result, Person.class); System.out.println(json.toJSONString(person)); * 插入对象List @Test public void setList() { Person person1 = new Person("person1", "male"); Person person2 = new Person("person2", "female"); Person person3 = new Person("person3", "male"); List Person list = new ArrayList (); list.add(person1); list.add(person2); list.add(person3); redisService.set("redis_list_test", json.toJSONString(list)); * 获取list @Test public void getList() { String result = redisService.get("redis_list_test"); List String list = json.parseArray(result, String.class); System.out.println(list); @Test public void remove() { redisService.remove("redis_test"); class Person { private String name; private String sex; public Person() { public Person(String name, String sex) { this.name = name; this.sex = sex; public String getName() { return name; public void setName(String name) { this.name = name; public String getSex() { return sex; public void setSex(String sex) { this.sex = sex; }
在这里先是用@Autowired注解把redisService注入进来,然后由于是使用json字符串进行交互,所以引入fastjson的JSONObject类。然后为了方便,直接在这个测试类里面加了一个Person的内部类。
一共测试了:对于string类型的存取,对于object类型的存取,对于list类型的存取,其实本质都是转成了json字符串。还有就是根据key来执行remove操作。
获取字符串:
获取对象:
获取list:
redis管理客户端数据:
到此,测试完成,对于常用的一些数据类型的转换存取操作也基本调试通过。所以本文对于springboot整合redis到此结束。
到此这篇关于Redis在项目中的使用(JedisPool方式)的文章就介绍到这了,更多相关Redis项目中使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
我想要获取技术服务或软件
服务范围:MySQL、ORACLE、SQLSERVER、MongoDB、PostgreSQL 、程序问题
服务方式:远程服务、电话支持、现场服务,沟通指定方式服务
技术标签:数据恢复、安装配置、数据迁移、集群容灾、异常处理、其它问题
本站部分文章参考或来源于网络,如有侵权请联系站长。
数据库远程运维 Redis在项目中的使用(JedisPool方式)
相关文章
- 使用Redis编写测试用例的指南(redis测试用例)
- 进行负载均衡使用CI框架实现Redis负载均衡(ci框架使用redis)
- 大规模更新:使用Redis重置所有缓存(redis更新所有缓存)
- 内存使用查看Redis内存使用:管理你的缓存(redis查看)
- 如何使用Redis实现分布式队列功能(怎么实现redis队列)
- 使用Redis构建常见KV应用(常用的kv组件redis)
- 使用Redis正确处理掉单情况(用redis对掉单处理)
- 如何快速查找Redis中的所有键(查找redis所有的键)
- 燕十八Redis项目源码实现细节(燕十八redis项目源码)
- 为什么使用 Redis让我们看看优势(为啥使用redis)
- 使用Yii框架连接Redis(yii如何连接redis)
- SSM框架结合Redis实现快速教学部署(ssm使用redis教程)
- 提升网站性能全站使用Redis缓存策略(全站使用redis缓存)
- 测试Redis,简单又高效(如何对redis进行测试)
- 保证Redis持续不阻塞实践方法介绍(如何保证redis不阻塞)
- 利用Redis提升系统性能(使用redis做什么)
- 从零开始Redis架构手册(从0开始搭建redis)
- 调试Redis高并发排查系统错误(redis高并发错误)
- Redis中常用命令指引你前行(redis项目中常用命令)
- Redis集群排查与解决错误(redis集群错误)
- Redis集群运行状况如何(redis 集群是否正常)
- Redis集群的安装与使用(redis集群安装和使用)
- 利用Redis随机获取数据的新思路(redis随机读取数据)
- 使用Redis链接查看工具探索数据的迷人世界(redis链接查看工具)
- Redis带来的1 简单却强大的快速回应(redis 返回值是1)
- 使用Redis让局域网内部更加连接(redis设置局域网链接)
- 使用Lua脚本来装载Redis(redis装载lua脚本)