美文网首页
redis 分布式缓存使用

redis 分布式缓存使用

作者: 白沙forever | 来源:发表于2019-05-29 11:36 被阅读0次

spring boot 中使用redis 分布式缓存

1.引入pom

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

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 hwei1233@163.com
 * @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<UserPublicKey, UserPublicKeyDao> 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);
    }



 
}

相关文章

网友评论

      本文标题:redis 分布式缓存使用

      本文链接:https://www.haomeiwen.com/subject/zfvetctx.html