뿐만 아니라 크게, 레디 스 일부 데이터는 자주 검색을 필요로 저장하는 데 사용되는 원격 사전 서버 (원격 데이터 서비스) 오버 헤드 메모리 저장 : 레디 스는 C 언어, 레디 스 전체 이름을 사용하여 고성능의 키 - 값 데이터베이스입니다 어떤 뜨거운 레디 스 데이터 저장 속도를 크게 향상 속도와 직접 서버의 오버 헤드 절약을 향상 메모리에서 소요되는 시간을 사용한다.
먼저, 로컬 캐시를 설정 : 사용은 캐시 아이디어를 레디 스 로컬 캐시를 가지고 데이터가없는 레디 스 때 ConcurrentHashMap의 후 결정 RedisTemplate 객체 데이터를 구축 할 수 있습니다.
그런 사람은 왜 선 아에 직접 로컬 캐시로지도, 그것을 레디 스 물었다. 사실, 로컬 캐시는 쿼리 레디 스의 수와 시간을 줄일 수 있지만, 재부팅 바람둥이 상황이 발생했을 때, 로컬 캐시가 삭제됩니다,하지만 캐시 된 데이터는 레디 스의도이다.
전통적인 관계형 데이터베이스는 ACID를 충족하고, 레디 스는 단일 프로세스, 기본 (16) 데이터베이스입니다.
캡의 두 가지를 충족 고전되는 NoSQL 비 관계형 데이터베이스는 3 종류 충족시킬 수 없다
C : 강력한 일관성
A : 고 가용성
P : 파티션 내결함성
여기에서 우리는 연결 풀 레디 스의 구성을 보면
package com. terse. develop. loginmanage. config;
import com. fasterxml. jackson. annotation. JsonAutoDetect;
import com. fasterxml. jackson. annotation. PropertyAccessor;
import com. fasterxml. jackson. databind. ObjectMapper;
import org. slf4j. Logger;
import org. slf4j. LoggerFactory;
import org. springframework. beans. factory. annotation. Qualifier;
import org. springframework. beans. factory. annotation. Value;
import org. springframework. boot. autoconfigure. EnableAutoConfiguration;
import org. springframework. boot. context. properties. ConfigurationProperties;
import org. springframework. context. annotation. Bean;
import org. springframework. context. annotation. Configuration;
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. serializer. Jackson2JsonRedisSerializer;
import org. springframework. data. redis. serializer. StringRedisSerializer;
import redis. clients. jedis. JedisPoolConfig;
@Configuration
@EnableAutoConfiguration
public class RedisConfig {
private Logger logger = LoggerFactory. getLogger ( this . getClass ( ) ) ;
@Value ( "${spring.redis.hostName}" )
private String host;
@Value ( "${spring.redis.port}" )
private int port;
@Value ( "${spring.redis.password}" )
private String password;
@Value ( "${spring.redis.timeout}" )
private int timeout;
@Value ( "${redis.pool.maxWaitMillis}" )
private long maxWaitMillis;
@Value ( "${redis.pool.maxIdle}" )
private int maxIdle;
@Bean ( "JPC" )
@ConfigurationProperties ( prefix = "redis.pool" )
public JedisPoolConfig getRedisConfig ( ) {
JedisPoolConfig config = new JedisPoolConfig ( ) ;
config. setMaxIdle ( maxIdle) ;
config. setMaxWaitMillis ( maxWaitMillis) ;
return config;
}
@Bean ( "JCF" )
@ConfigurationProperties ( prefix = "spring.redis" )
public JedisConnectionFactory getConnectionFactory ( ) {
JedisConnectionFactory factory = new JedisConnectionFactory ( ) ;
JedisPoolConfig config = getRedisConfig ( ) ;
factory. setPoolConfig ( config) ;
factory. setHostName ( host) ;
factory. setPassword ( password) ;
factory. setTimeout ( timeout) ;
logger. info ( "JedisConnectionFactory bean init success." ) ;
return factory;
}
@Bean ( "RT" )
public RedisTemplate< String, Object> redisTemplate ( @Qualifier ( "JCF" ) RedisConnectionFactory factory) {
RedisTemplate< String, Object> template = new RedisTemplate < > ( ) ;
template. setConnectionFactory ( factory) ;
Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer ( Object. class ) ;
ObjectMapper om = new ObjectMapper ( ) ;
om. setVisibility ( PropertyAccessor. ALL, JsonAutoDetect. Visibility. ANY) ;
om. enableDefaultTyping ( ObjectMapper. DefaultTyping. NON_FINAL) ;
jacksonSeial. setObjectMapper ( om) ;
template. setValueSerializer ( jacksonSeial) ;
template. setKeySerializer ( new StringRedisSerializer ( ) ) ;
template. setHashKeySerializer ( new StringRedisSerializer ( ) ) ;
template. setHashValueSerializer ( jacksonSeial) ;
template. afterPropertiesSet ( ) ;
return template;
}
}
쓰기 구성 클래스는 인터페이스를 작성하는 사랑 후에
import java. util. Map;
public interface RedisHashConsumerManager < T> {
boolean hasConsumer ( String token) ;
String addConsumer ( Map< String, Object> consumer)
void deleteConsumer ( String token) ;
T getConsumer ( String token) ;
void addParameter ( String token, String key, String value) ;
void delParameter ( String token, String key) ;
String getParameter ( String token, String key) ;
}
그럼 우리가 구현 클래스를 작성하는 구현 위의 인터페이스, 레디 스의 작동
import org. springframework. data. redis. core. *;
import javax. annotation. Resource;
import java. util. Map;
public class HashConsumerManageImpl < P, PS> implements RedisHashConsumerManager < Map< String, Object> > {
protected final HashOperations< String, String, Object> redisHas;
protected final RedisTemplate< String, Object> redisTemplate;
protected long activityTime = 7 ;
protected TimeUnit activityTimeUnit = TimeUnit. DAYS;
public HashConsumerManageImpl ( RedisTemplate< String, Object> redisTemplate) {
this . redisHas = redisTemplate. opsForHash ( ) ;
this . redisTemplate = redisTemplate;
}
public long getActivityTime ( ) {
return activityTime;
}
public TimeUnit getActivityTimeUnit ( ) {
return activityTimeUnit;
}
@Override
public boolean hasConsumer ( String token) {
return redisTemplate. hasKey ( token) ;
}
@Override
public String addConsumer ( Map< String, Object> consumer) {
String consumerToken = ( String) consumer. get ( "token" ) ;
redisHas. putAll ( consumerToken, consumer) ;
redisTemplate. expire ( consumerToken, getActivityTime ( ) , getActivityTimeUnit ( ) ) ;
return consumerToken;
}
@Override
public void deleteConsumer ( String token) {
redisTemplate. delete ( token) ;
}
@Override
public Map< String, Object> getConsumer ( String token) {
return redisHas. entries ( token) ;
}
@Override
public void addParameter ( String token, String key, String value) {
if ( hasConsumer ( token) )
redisHas. put ( token, key, value) ;
}
@Override
public void delParameter ( String token, String key) {
if ( hasConsumer ( token) )
redisHas. delete ( token, key) ;
}
@Override
public String getParameter ( String token, String key) {
if ( hasConsumer ( token) )
return ( String) redisHas. get ( token, key) ;
return null;
}
}
@RestController
@RequestMapping ( "/consumer/" )
public class TestConsumerApi {
@Autowired
@Lazy
RedisHashConsumerManager redisHashConsumerManager;
@RequestMapping ( "login" )
public void login ( Map< String, Object> consumer) {
redisHashConsumerManager. addConsumer ( consumer) ;
}
}