Springboot統合redisストレージセッション
1.依存
2.redis構成クラス
3.カスタムkeyシーケンス化クラス
4.key値ネーミングスペースである接頭辞を追加
5.プロファイル
6.redisを個別に作成する.propertiesファイルはspringboot起動クラスに構成を追加し、session生存時間を構成する必要があります.
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.session
spring-session-data-redis
2.redis構成クラス
package com.jlh.complete.moudle.redis.config;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/***
* @description:
* @author: Jianglh
* @date: 2019/11/26 13:43
*/
@Configuration
public class RedisConfig {
@Autowired
private MyStringSerializer myStringSerializer;
@Bean
@SuppressWarnings("all")
public RedisTemplate redisTemplate(RedisConnectionFactory factory){
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(factory);
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);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key String
template.setKeySerializer(myStringSerializer);
// hash key String
template.setHashKeySerializer(myStringSerializer);
// value jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash value jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
3.カスタムkeyシーケンス化クラス
package com.jlh.complete.moudle.redis.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.nio.charset.Charset;
/***
* @description:
* @author: Jianglh
* @date: 2019/11/26 14:26
*/
@Component
public class MyStringSerializer implements RedisSerializer {
private final Logger logger = LoggerFactory.getLogger ( this.getClass () );
@Autowired
private RedisProperties redisProperties;
private final Charset charset;
public MyStringSerializer() {
this ( Charset.forName ( "UTF8" ) );
}
public MyStringSerializer(Charset charset) {
Assert.notNull ( charset, "Charset must not be null!" );
this.charset = charset;
}
@Override
public String deserialize(byte[] bytes) {
String keyPrefix = redisProperties.getKeyPrefix ();
String saveKey = new String ( bytes, charset );
int indexOf = saveKey.indexOf ( keyPrefix );
if (indexOf > 0) {
logger.info ( "key " );
} else {
saveKey = saveKey.substring ( indexOf );
}
logger.info ( "saveKey:{}",saveKey);
return (saveKey.getBytes () == null ? null : saveKey);
}
@Override
public byte[] serialize(String string) {
String keyPrefix = redisProperties.getKeyPrefix ();
String key = keyPrefix + string;
logger.info ( "key:{},getBytes:{}",key, key.getBytes ( charset ));
return (key == null ? null : key.getBytes ( charset ));
}
}
4.key値ネーミングスペースである接頭辞を追加
package com.jlh.complete.moudle.redis.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/***
* @description:
* @author: Jianglh
* @date: 2019/11/26 14:28
*/
@Data
@Component
@ConfigurationProperties(prefix = "redis")
@PropertySource("classpath:redis.properties")
public class RedisProperties {
/**
* key
*/
private String keyPrefix;
}
5.プロファイル
redis.properties
#
spring.redis.host=*******
#
spring.redis.port=6379
#
spring.redis.database=0
#
spring.redis.password=****
# 300000 300
spring.redis.timeout=300000
# themilef ,
#spring.thymeleaf.cache=false
redis.keyPrefix=0000-->
6.redisを個別に作成する.propertiesファイルはspringboot起動クラスに構成を追加し、session生存時間を構成する必要があります.
package com.jlh.complete;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
//import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@SpringBootApplication(scanBasePackages = "com.jlh.complete")
@MapperScan("com.jlh.complete.moudle.*.dao")
// redis
@PropertySource(value = {"classpath:redis.properties"})
// redis session 1800
@EnableRedisHttpSession(maxInactiveIntervalInSeconds=60)
public class CompleteApplication {
public static void main(String[] args) {
SpringApplication.run(CompleteApplication.class, args);
}
}