spring boot + redis 分布式缓存

1.引入pom

        
            org.springframework.boot
            spring-boot-starter-redis
        

2. 默认redis缓存配置


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @Description 默认redis缓存配置
 * @Author hewei [email protected]
 * @Date 2019-05-07
 */
@Configuration
public class DefaultRedisCacheConfig {

    private final RedisTemplate redisTemplate;

    @Autowired
    public DefaultRedisCacheConfig(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * 缓存默认超时配置
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        //使用前缀
        cacheManager.setUsePrefix(true);
        // 设置默认过期时间为864000秒(10天)
        cacheManager.setDefaultExpiration(864000);
        return cacheManager;
    }
}

3. 在spring boot启动类加上注解

@EnableCaching
开启缓存

4. 在serviceImpl实现类加上注解

@CacheConfig(cacheNames = "userKey")
"userKey" 为key名称

方法需要单独加上注解@CachePut , @Cacheable

例如


import com.alibaba.dubbo.config.annotation.Service;
import com.sql.tillo.dao.user.UserPublicKeyDao;
import com.sql.tillo.enumerate.UserPublicKeyTypeEnum;
import com.sql.tillo.service.AbstractBaseService;
import com.sql.tillo.user.UserPublicKey;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;



@CacheConfig(cacheNames = "userKey")
public class UserPublicKeyServiceImpl extends AbstractBaseService implements UserPublicKeyService {


    @Override
    @Async
    @Transactional(rollbackFor = Exception.class)
    @CachePut(key = "('sign')+#p0.userId")
    public void saveSignedKey(UserPublicKey userPublicKey) {

        dao.save(userPublicKey);
    }

    @Override
    @Async
    @Transactional(rollbackFor = Exception.class)
    @CachePut(key = "('iden')+#p0.userId")
    public void saveIdentitykey(UserPublicKey userPublicKey) {

        dao.save(userPublicKey);
    }

    @Override
    @Cacheable(key = "('sign')+#p0")
    public UserPublicKey findSignedKeyByUserId(Long userId) {

        return dao.findByUserIdAndKeyType(userId, UserPublicKeyTypeEnum.signedKey.getType());
    }

    @Override
    @Cacheable(key = "('iden')+#p0")
    public UserPublicKey findIdentitykeyByUserId(Long userId) {

        return dao.findByUserIdAndKeyType(userId, UserPublicKeyTypeEnum.identitykey.getType());
    }

    @Override
    @Caching(evict = {@CacheEvict(key = "('iden')+#p0"), @CacheEvict(key = "('sign')+#p0")})
    public void deleteByUserId(Long userId) {
        dao.deleteByUserId(userId);
    }



 
}

你可能感兴趣的:(spring boot + redis 分布式缓存)