首页 > 代码库 > springboot redis 缓存对象

springboot redis 缓存对象

只要加入spring-boot-starter-data-redis , springboot 会自动识别并使用redis作为缓存容器,使用方式如下

gradle加入依赖

    compile("org.springframework.boot:spring-boot-starter-data-redis:${springBootVersion}")

redis configuration 中启用缓存

@Configuration
@EnableCaching
public class RedisConfiguration 

redis 自定义key生成规则

@Bean
    public KeyGenerator wiselyKeyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();

                sb.append(target.getClass().getName());
                sb.append(":" + method.getName());
                for (Object obj : params) {
                    sb.append(":" + obj.toString());
                }
                return sb.toString();
            }
        };

    }

有时候我们需要 使用 redisTemplate, 可以这样子配置

@Bean
    public RedisConnectionFactory redisConnectionFactory() {
        return new JedisConnectionFactory();
    }

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }

    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        return new RedisCacheManager(redisTemplate);
    }

完整代码

package cn.xiaojf.today.data.redis.configuration;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import java.lang.reflect.Method;

/**
 * redis 缓存配置
 * @author xiaojf 2016/12/7 10:29.
 */
@Configuration
@EnableCaching
public class RedisConfiguration {

    @Bean
    public KeyGenerator wiselyKeyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();

                sb.append(target.getClass().getName());
                sb.append(":" + method.getName());
                for (Object obj : params) {
                    sb.append(":" + obj.toString());
                }
                return sb.toString();
            }
        };

    }

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        return new JedisConnectionFactory();
    }

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }

    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        return new RedisCacheManager(redisTemplate);
    }
}

使用例子

package cn.xiaojf.today.sys.dao.impl;

import cn.xiaojf.today.base.dao.impl.BaseDaoImpl;
import cn.xiaojf.today.base.model.PageInfo;
import cn.xiaojf.today.base.util.Asserts;
import cn.xiaojf.today.sys.dao.SysUserDao;
import cn.xiaojf.today.sys.repository.SysUserRepository;
import cn.xiaojf.today.sys.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Repository;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 用户数据访问接口实现类
 * @author xiaojf 2017/2/9 17:40.
 */
@Repository
@CacheConfig(cacheNames = {"sysUserCache"})
public class SysUserDaoImpl extends BaseDaoImpl<SysUser> implements SysUserDao {
    @Autowired
    private SysUserRepository userRepository;

    public SysUserDaoImpl() {
        super(SysUserRepository.class);
    }

    @Override
//    @Cacheable(key = "#username")
    public SysUser getByUsername(String username) {
        Asserts.notNull(username,"用户名不能为空");
        SysUser user = userRepository.findFirstByUsername(username);
        return user;
    }
}

 

 spring cache的其他使用方式很简单,可以直接看官网或者百度例子

springboot redis 缓存对象