1、引入 spring-boot-starter-redis(1.4版本前),spring-boot-starter-data-redis(1.4版本后)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>
2,添加配置文件
# REDIS (RedisProperties) # Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址 spring.redis.host=192.168.0.58 # 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.Redis缓存配置类提供redisTemplate(获得配置文件中连接参数后的)
@Configuration @EnableCaching public class RedisCacheConfig { @Bean public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate){ CacheManager cacheManager = new RedisCacheManager(redisTemplate); return cacheManager; } @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory){ RedisTemplate<String, String> redisTemplate = new RedisTemplate<String,String>(); redisTemplate.setConnectionFactory(factory); // key序列化方式;(不然会出现乱码;),但是如果方法上有Long等非String类型的话,会报类型转换错误; // 所以在没有自己定义key生成策略的时候,以下这个代码建议不要这么写,可以不配置或者自己实现ObjectRedisSerializer // 或者JdkSerializationRedisSerializer序列化方式; RedisSerializer<String> redisSerializer = new StringRedisSerializer();// Long类型不可以会出现异常信息; redisTemplate.setKeySerializer(redisSerializer); redisTemplate.setHashKeySerializer(redisSerializer); return redisTemplate; } }
4,Redis工具类
package com.ty.tyzxtj.util; import java.io.Serializable; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.stereotype.Component; /** * redicache 工具类 * */ @SuppressWarnings("unchecked") @Component public class RedisUtil { @SuppressWarnings("rawtypes") @Autowired private RedisTemplate redisTemplate; /** * 批量删除对应的value * * @param keys */ public void remove(final String... keys) { for (String key : keys) { remove(key); } } /** * 批量删除key * * @param pattern */ public void removePattern(final String pattern) { Set<Serializable> keys = redisTemplate.keys(pattern); if (keys.size() > 0) redisTemplate.delete(keys); } /** * 删除对应的value * * @param key */ public void remove(final String key) { if (exists(key)) { redisTemplate.delete(key); } } /** * 判断缓存中是否有对应的value * * @param key * @return */ public boolean exists(final String key) { return redisTemplate.hasKey(key); } /** * 读取缓存 * * @param key * @return */ public String get(final String key) { Object result = null; redisTemplate.setValueSerializer(new StringRedisSerializer()); ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); result = operations.get(key); if(result==null){ return null; } return result.toString(); } /** * 写入缓存 * * @param key * @param value * @return */ public boolean set(final String key, Object value) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 写入缓存 * * @param key * @param value * @return */ public boolean set(final String key, Object value, Long expireTime) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } public boolean hmset(String key, Map<String, String> value) { boolean result = false; try { redisTemplate.opsForHash().putAll(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } public Map<String,String> hmget(String key) { Map<String,String> result =null; try { result= redisTemplate.opsForHash().entries(key); } catch (Exception e) { e.printStackTrace(); } return result; } } 复制代码
5.使用方法
相关推荐
Springboot整合Redis集群,零配置方式;另外用AOP实现了操作缓存的三套自定义注解,有兴趣的小伙伴可以试试看.
springboot整合redis.算是比较全面的一种整合方式了. springboot整合redis.算是比较全面的一种整合方式了.
springboot整合redis集群(三种方式)源码
springboot集成redis集群,redis安装包配置,参考文章:https://blog.csdn.net/zhizhuodewo6/article/details/82316164
springboot整合redis选用 `Lettuce` 作为redis框架,选用 `Protobuf` 作为序列化框架
通过配置文件实现连接单机redis或集群redis demo,实现开发时使用单机redis,线上使用集群redis
SpringBoot基于redis的分布式锁,有word使用文档,根据文档配置即可使用
SpringBoot整合Redis,包括整合单机版Redis、redis-cluster集群、redis哨兵模式
springboot集成redis、mybatis...5、springboot实现初始化加载配置(实现缓存预热)的两种方式 6、二级缓存联合使用 方式一:redis原生方法工具类(RedisService)+redis+mybatis 方式二:CacheManager+redis+mybatis
SpringBoot集成Redis应用配置(附完整配置文件)
核心技术:Redis开启过期监听 springboot整合redis,springboot配置redis监听。 案例中通过访问addOrder方法实现向mysql数据库添加一条数据并在redis中设置该条数据的有效期为10秒,10秒过后触发redis监听,在监听中...
基于Springboot+Redis+Mysql+Kafka开发的高并发限时的商品秒杀系统.zip 毕业设计 基于springboot mysql Vue的系统开发,供参考,提供说明材料+源代码 毕业设计 基于springboot mysql Vue的系统开发,供参考,提供...
SpringBoot集成Redis集群
主要介绍了springboot配置redis过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
搭建redis集群,springboot关联redis的配置,实例代码操作,及运行结果展示,包含某互联网教程书籍上面截图,完整详细讲解了redis三种模式,单机模式、哨兵模式、集群模式
基于springboot最新版本2.1.4.RELEASE,采用jpa为dao层框架,配置redis(redisson)为hibernate的二级缓存。
springboot + redis 集群配置+测试case; linux 上安装部署 redis 集群图文文档;
Spring Boot为Redis, MongoDB, Elasticsearch, Solr和Gemfire提供自动配置。本文详细介绍了springboot配置多个redis连接,有兴趣的可以了解一下。
系统的特点 高性能:秒杀涉及大量的并发读和并发写,因此支持高并发访问这点非常关键 一致性:秒杀商品减库存的实现方式同样关键,有限数量的商品在同一时刻被很多倍的请求同时来减库存,在大并发更新的过程中都要...