Redis es una base de datos de alto rendimiento clave-valor, utilizando el lenguaje C, Redis nombre completo: Diccionario remota del servidor (Remote Data Service), Redis utiliza para almacenar algunos datos requieren la recuperación frecuente, el ahorro de sobrecarga de memoria, sino también en gran medida mejorar la velocidad, almacenada en algunos datos Redis calientes, utilizar el tiempo, tomado de la memoria, que mejora en gran medida la velocidad y el ahorro de los gastos generales del servidor directamente.
Uso ReDiS las ideas de caché: En primer lugar, establecer una caché local: ConcurrentHashMap, a continuación, genere datos de objeto RedisTemplate para determinar cuándo tomar la caché local y Redis no hay datos.
Entonces alguien preguntó: ¿por qué ReDiS que, con el mapa como una caché local directamente en la línea ah. De hecho, la caché local sólo reduce el número y la hora de los Redis de consulta, pero cuando se produce la situación Tomcat reinicio, se borrará la caché local, pero los datos en caché también es de Redis.
base de datos relacional tradicional para cumplir con el ácido, y Redis es un proceso sencillo, la base de datos por defecto 16.
La base de datos NoSQL clásica no relacional a conocer a dos de la PAC, no puede cumplir con los tres tipos
C: fuerte consistencia
A: Alta disponibilidad
P: tolerancia a fallos partición
Aquí nos fijamos en la configuración de la agrupación de conexiones Redis
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;
}
}
Después de la clase de configuración de escritura le encanta escribir una interfaz
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) ;
}
Entonces escribimos una clase de implementación que implementa la interfaz anterior, la operación de Redis
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;
}
}
Después de escribir estos, se puede utilizar directamente
@RestController
@RequestMapping ( "/consumer/" )
public class TestConsumerApi {
@Autowired
@Lazy
RedisHashConsumerManager redisHashConsumerManager;
@RequestMapping ( "login" )
public void login ( Map< String, Object> consumer) {
redisHashConsumerManager. addConsumer ( consumer) ;
}
}