spring boot 使用 redis

一个访问量大的项目对于缓存是必须的,但对于一个小项目使用缓存,只会增加项目的复杂度。

导入依赖

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

修改配置文件

spring:
		redis:
		    database: 0
		    host: localhost
		    port: 6379
		    password:    # 密码(默认为空)
		    timeout: 6000ms  # 连接超时时长(毫秒)
		    jedis:
		      pool:
		        max-active: 1000  # 连接池最大连接数(使用负值表示没有限制)
		        max-wait: -1ms      # 连接池最大阻塞等待时间(使用负值表示没有限制)
		        max-idle: 10      # 连接池中的最大空闲连接
		        min-idle: 5       # 连接池中的最小空闲连接

配置类

@Configuration
public class RedisConfig {
    @Autowired
    private RedisConnectionFactory factory;

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }

    @Bean
    public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForHash();
    }

    @Bean
    public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
        return redisTemplate.opsForValue();
    }

    @Bean
    public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForList();
    }

    @Bean
    public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForSet();
    }

    @Bean
    public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForZSet();
    }
}

redisTemplate是一个模板类,可以对redis服务端进行操作,剩余的Bean都是针对其他数据结构的操作类,如Set,List,Hash…

工具类

@Component
public class RedisUtils {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ValueOperations<String, String> valueOperations;
    @Autowired
    private HashOperations<String, String, Object> hashOperations;
    @Autowired
    private ListOperations<String, Object> listOperations;
    @Autowired
    private SetOperations<String, Object> setOperations;
    @Autowired
    private ZSetOperations<String, Object> zSetOperations;
    /**  默认过期时长,单位:秒 */
    public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
    /**  不设置过期时长 */
    public final static long NOT_EXPIRE = -1;
    private final static Gson gson = new Gson();

    public void set(String key, Object value, long expire){
        valueOperations.set(key, toJson(value));
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
    }

    public void set(String key, Object value){
        set(key, value, DEFAULT_EXPIRE);
    }

    public <T> T get(String key, Class<T> clazz, long expire) {
        String value = valueOperations.get(key);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value == null ? null : fromJson(value, clazz);
    }

    public <T> T get(String key, Class<T> clazz) {
        return get(key, clazz, NOT_EXPIRE);
    }

    public String get(String key, long expire) {
        String value = valueOperations.get(key);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }

    public String get(String key) {
        return get(key, NOT_EXPIRE);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    /**
     * Object转成JSON数据
     */
    private String toJson(Object object){
        if(object instanceof Integer || object instanceof Long || object instanceof Float ||
                object instanceof Double || object instanceof Boolean || object instanceof String){
            return String.valueOf(object);
        }
        return gson.toJson(object);
    }

    /**
     * JSON数据,转成Object
     */
    private <T> T fromJson(String json, Class<T> clazz){
        return gson.fromJson(json, clazz);
    }
}

对于对象的存储是将对象转为JSON字符串再存入redis

示例代码

	@Autowired
	RedisUtils redisUtils;
	@Test
	public void contextLoads() {
		UserEntity userEntity = new UserEntity();
		userEntity.setUserId(1L);
		userEntity.setUsername("Ay");
		userEntity.setPassword("123456");
		redisUtils.set("userEntity",userEntity);
		UserEntity userEntity1 = redisUtils.get("userEntity",UserEntity.class);
	}

缓存

  • 查询数据的时候,尽量减少DB查询,DB主要负责写数据
  • 数据库 insert 、 update 、 delete 时,同步更新缓存数据

1.在dao层方法上加入注解

@CacheConfig(cacheNames = "post")   //redis缓存一组以post命名的数据
public interface PostRepository extends PagingAndSortingRepository<Post, Integer> {
    /**
     * #p0是第一个参数,先查缓存等于id的对象,再查DB
     * @param id
     * @return
     */
    @Cacheable(key = "#p0")
    Post findById(int id);

    /**
     * 以#p0.id为key,即以post属性id为key,新增和修改会刷新缓存
     * @param post
     * @return
     */
    @CachePut(key = "#p0.id")
    @Override
    Post save(Post post);

    /**
     * 同时删除缓存和DB
     * @param id
     * @return
     */
    @CacheEvict(key = "#p0")
    @Transactional
    @Modifying
    int deleteById(int id);

}

2.启动类上开启缓存@EnableCaching
可以通过控制台的sql语句发现缓存确实起了作用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值