博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot(七)redis 实现session共享
阅读量:4299 次
发布时间:2019-05-27

本文共 10931 字,大约阅读时间需要 36 分钟。

1、引入jar包

org.springframework.boot
spring-boot-starter-redis
org.springframework.session
spring-session-data-redis
2、在resources下创建redis.properties文件

# REDIS (RedisProperties)# Redis数据库索引(默认为0)spring.redis.database=0spring.redis.host=localhostspring.redis.password=spring.redis.port=6379spring.redis.timeout=300spring.redis.maxIdle=8spring.redis.minIdle=0spring.redis.maxActive=8spring.redis.maxWait=-1#spring.redis.sentinel.master= # name of Redis server#spring.redis.sentinel.nodes= # comma-separated list of host:port pairs

3、创建RedisConfig,添加@EnableRedisHttpSession来开启spring session支持

package com.demo.redis;import com.demo.model.Redis;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.context.properties.ConfigurationProperties;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.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;import java.lang.reflect.Method;/** * Created by huguoju on 2016/12/29. * 缓存管理(注解用) */@Configuration@EnableCaching@EnableRedisHttpSession  //开启spring session支持  实现session共享public class RedisConfig extends CachingConfigurerSupport {    @Autowired    private Redis redis;    private int expireTime = 60 * 60;    /**     * 生成key的策略     * @return     */    @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();            }        };    }    @Bean    public JedisConnectionFactory redisConnectionFactory() {        JedisConnectionFactory factory = new JedisConnectionFactory();        factory.setHostName(redis.getHost());        factory.setPort(redis.getPort());        factory.setTimeout(redis.getTimeout()); //设置连接超时时间        return factory;    }    @Bean    public CacheManager cacheManager(RedisTemplate redisTemplate) {        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);        //Number of seconds before expiration. Defaults to unlimited (0)        cacheManager.setDefaultExpiration(expireTime); //设置key-value超时时间        return cacheManager;        //return new RedisCacheManager(redisTemplate);    }    @SuppressWarnings("unchecked")    @Bean    public RedisTemplate
redisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); setSerializer(template); //设置序列化工具,这样ReportBean不需要实现Serializable接口 template.afterPropertiesSet(); return template; } private void setSerializer(StringRedisTemplate template) { Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); }}
如果redis配置信息是放在application.properties里,可以直接用

@Value("${spring.redis.host}")private String host;
 
直接注入属性,我是将redis配置放在redis.properties里了,所以不能用@value了,
创建redis文件
 
package com.demo.model;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;/** * Created by huguoju on 2016/12/29. */@Configuration@Data@ConfigurationProperties(prefix = "spring.redis",locations = "classpath:redis/redis.properties")public class Redis {    private String database;    private String host;    private String password;    private int port;    private int timeout;    private String maxIdle;    private String minIdle;    private String maxActive;    private String maxWait;} 在redisConfig里通过
@Autowired    private Redis redis;
调用就可以了。
4、测试,在testController中,
package com.demo.controller;import com.demo.model.User;import com.demo.service.TestService;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import io.swagger.annotations.ApiParam;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.annotation.Cacheable;import org.springframework.http.MediaType;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;import java.util.HashMap;import java.util.Map;/** * Created by huguoju on 2016/12/28. */@RestController@RequestMapping("test")@Api(value = "测试类",tags = "测试接口")public class TestController {    /**     * @Cacheable(key = "'user:'.concat(#userCode)",value = "user")     * value的意思是当调用这个方法的时候,会从一个名叫 user 的缓存中查询,如果没有,则执行实际的方法(即查询数据库),并将执行的结果存入缓存中,否则返回缓存中的对象     */    @Autowired    private TestService testService;    @RequestMapping(value = "testData",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = {RequestMethod.POST,RequestMethod.GET})    @ApiOperation("测试读写分离、缓存")    @Cacheable(key = "'user:'.concat(#userCode)",value = "user")    public User testDateSource(            @ApiParam(name = "userCode",value = "用户id",required = true)            @RequestParam Integer userCode){        User user=testService.selectByUserCode(userCode);        return user;    }} @Cacheable 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存
@Cacheable 作用和配置方法  
参数
解释
example
value
缓存的名称,在 spring 配置文件中定义,必须指定至少一个
例如: @Cacheable(value=”mycache”) @Cacheable(value={”cache1”,”cache2”}
key
缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合
@Cacheable(value=”testcache”,key=”#userName”)
condition
缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存
@Cacheable(value=”testcache”,condition=”#userName.length()>2”)
 
 
 
 
@CachePut 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用 @CachePut 作用和配置方法  
参数
解释
example
value
缓存的名称,在 spring 配置文件中定义,必须指定至少一个
@CachePut(value=”my cache”)
key
缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合
@CachePut(value=”testcache”,key=”#userName”)
condition
缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存
@CachePut(value=”testcache”,condition=”#userName.length()>2”)
 
@CacheEvict @CachEvict 的作用 主要针对方法配置,能够根据一定的条件对缓存进行清空 @CacheEvict 作用和配置方法  
参数
解释
example
value
缓存的名称,在 spring 配置文件中定义,必须指定至少一个
@CacheEvict(value=”my cache”)
key
缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合
@CacheEvict(value=”testcache”,key=”#userName”)
condition
缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存
@CacheEvict(value=”testcache”,condition=”#userName.length()>2”)
allEntries
是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存
@CachEvict(value=”testcache”,allEntries=true)
beforeInvocation
是否在方法执行前就清空,缺省为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存
@CachEvict(value=”testcache”,beforeInvocation=true)
@Caching:多个不同的需求放在同一个方法上,就用Caching,比如清除多个缓存
@Caching(evict = {    @CacheEvict(value="test1", key="#id",beforeInvocation = true),         @CacheEvict(value="test2", key="#id",beforeInvocation = true)})
Caching源代码如下:
ps:redisUtil:
 
package com.example.util;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.stereotype.Component;import java.io.Serializable;import java.util.Set;import java.util.concurrent.TimeUnit;/** * Created by huguoju on 2016/12/30. */@SuppressWarnings("unchecked")@Componentpublic class RedisUtils {    @SuppressWarnings("rawtypes")    @Autowired    private RedisTemplate redisTemplate;    /**     * 批量删除对应的value     *     * @param keys     */    public void remove(final String... keys) {        for (String key : keys) {            remove(key);        }    }    /**     * 批量删除key     *     * @param pattern     */    public void removePattern(final String pattern) {        Set
keys = redisTemplate.keys(pattern); if (keys.size() > 0) redisTemplate.delete(keys); } /** * 删除对应的value * * @param key */ public void remove(final String key) { if (exists(key)) { redisTemplate.delete(key); } } /** * 判断缓存中是否有对应的value * * @param key * @return */ public boolean exists(final String key) { return redisTemplate.hasKey(key); } /** * 读取缓存 * * @param key * @return */ public Object get(final String key) { Object result = null; ValueOperations
operations = redisTemplate.opsForValue(); result = operations.get(key); return result; } /** * 写入缓存 * * @param key * @param value * @return */ public boolean set(final String key, Object value) { boolean result = false; try { ValueOperations
operations = redisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 写入缓存 * * @param key * @param value * @return */ public boolean set(final String key, Object value, Long expireTime) { boolean result = false; try { ValueOperations
operations = redisTemplate.opsForValue(); operations.set(key, value); redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); result = true; } catch (Exception e) { e.printStackTrace(); } return result; }}

转载地址:http://rmpws.baihongyu.com/

你可能感兴趣的文章
打包app出现的一个问题
查看>>
iOS在Xcode6中怎么创建OC category文件
查看>>
Expanding User-Defined Runtime Attributes in Xcode with Objective-C
查看>>
iOS7 UITabBar自定义选中图片显示为默认蓝色的Bug
查看>>
提升UITableView性能-复杂页面的优化
查看>>
25 iOS App Performance Tips & Tricks
查看>>
那些好用的iOS开发工具
查看>>
iOS最佳实践
查看>>
使用CFStringTransform将汉字转换为拼音
查看>>
更轻量的 View Controllers
查看>>
Chisel-LLDB命令插件,让调试更Easy
查看>>
时间格式化hh:mm:ss和HH:mm:ss区别
查看>>
Objective-C Autorelease Pool 的实现原理
查看>>
编程语言大牛王垠:编程的智慧,带你少走弯路
查看>>
ios指令集以及基于指令集的app包压缩策略
查看>>
iOS开发者的福利 — — iOS9+Xcode7免越狱免证书直接调试
查看>>
3、JavaWeb学习之基础篇—JSP
查看>>
4、JavaWeb学习之基础篇—Session
查看>>
5、JavaWeb学习之基础篇—标签(自定义&JSTL)
查看>>
8、JavaWEB学习之基础篇—文件上传&下载
查看>>