Spring Boot加入Redis来实现用户信息的缓存

代码示例来源于:http://www.cnblogs.com/ityouknow/p/5748830.html

使用接口的时候发过来的token我们要对其进行验证并获取用户的实体,由于很多地方都要用到token的验证,所以查询用户实体是一个频繁的操作,以前我们可能需要查一次数据库来获取用户实体,但是如果使用缓存那么我们就可以在第二次及以后不必查询数据库从而减少访问接口的时间。

首先我们引入redis

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

之后再application.properties中添加redis的相关配置

#redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.pool.maxActive=8    
spring.redis.pool.maxWait=-1    
spring.redis.pool.maxIdle=8    
spring.redis.pool.minIdle=0    
spring.redis.timeout=0 
spring.redis.password=slience //密码默认为空

弄好配置后我们写一个配置类
RedisConfig.java

package slience.config;
import java.lang.reflect.Method;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
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.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport{
    
    @Bean
    public KeyGenerator keyGenerator() {
        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();
            }
        };
    }

    @SuppressWarnings("rawtypes")
    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
        //设置缓存过期时间
        //rcm.setDefaultExpiration(60);//秒
        return rcm;
    }
    
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

}

之后我们可以写个测试方法去看看效果

package slience.test;

import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import slience.SpringMvcTest2Application;
import slience.entity.User;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringMvcTest2Application.class)
public class TestRedis {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void test() throws Exception {
        stringRedisTemplate.opsForValue().set("aaa", "111");
        Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa"));
    }
    
    @Test
    public void testObj() throws Exception {
        User user=new User("slience", "123456", 12);
        ValueOperations<String, User> operations=redisTemplate.opsForValue();
        operations.set("com.neox", user);
        operations.set("com.neo.f", user,1,TimeUnit.SECONDS);
        Thread.sleep(1000);
        //redisTemplate.delete("com.neo.f");
        boolean exists=redisTemplate.hasKey("com.neo.f");
        User temp = operations.get("com.neox");
        System.out.println(temp.toString());
       // Assert.assertEquals("aa", operations.get("com.neo.f").getUserName());
    }
}

能够输出刚刚缓存的数据说明成功了
之后我们把Redis加入到我们项目代码中,因为我这边token是放在拦截器中,所以以下是拦截器的代码

package slience.interceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import slience.entity.Msg;
import slience.entity.User;
import slience.repository.UserRepository;
import slience.util.Code;
import slience.util.HttpTool;
import slience.util.TokenUtil;

/**
 * 权限拦截器
 * @author slience
 *
 */
public class AuthInterceptor implements HandlerInterceptor {

    @Autowired
    private Msg msg;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private RedisTemplate redisTemplate;
    
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        System.out.println("验证token");
        String token = request.getParameter("token");
        if(token != null) {
            long userId = TokenUtil.verify(token);
            ValueOperations<String, User> operations=redisTemplate.opsForValue();
            boolean exists = redisTemplate.hasKey(userId + "");
            if(exists) {
                //有缓存的情况,直接从缓存中取
                User user = operations.get(userId+"");
                request.getSession().setAttribute("user", user);
                return true;
            } else {
                //没有缓存的情况,从数据库中取
                if(userId > 0) {
                    User user = userRepository.findOne(userId);
                    if(user != null) {
                        request.getSession().setAttribute("user", user);
                        operations.set(userId+"", user);
                        return true;
                    } else {
                        msg.set("没有找到指定用户", Code.ERR_AUTH, null);
                    }
                } else if(userId == 0) {
                    msg.set("token超时,请重新登录", Code.ERR_AUTH, null);
                } else {
                    msg.set("token有误", Code.ERR_AUTH, null);
                }
            }
        } else {
            msg.set("请先登录", Code.ERR_AUTH, null);
        }
        HttpTool.jsonMsg(response, msg);
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }

}

有个比较奇怪的地方就是redis的key理论上是什么都可以,但是实际使用的时候只能是String,在RedisTemplate的源代码中对K值得注释是这样的

 * @param <K> the Redis key type against which the template works (usually a String)

但是如果我们把key设置为Long将会报“Long不能强制转换成String”的错误,不懂是为什么。

之后我们就可以使用这个换成啦,我个人实测第二次及以后的访问在用户信息获取这一环能够减少为原来时间的1/4,还是不错的。

推荐阅读更多精彩内容