2. Configure Redis in spring boot in

Use Redis as cache in the java spring boot project.
Operation is
1. First, in dependence redis added POM file,
2. Add redis in application.yml path, port and password
spring:
    repeat:
        host: xxx.xxx.xxx.xxx
        port: xxxxx
        password: xxxx
3. Add a package disposed below class config
Configuration RedisTemplate
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
    {
//        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//        RedisObjectSerializer redisObjectSerializer = new RedisObjectSerializer();
//        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
//        template.setConnectionFactory(redisConnectionFactory);
//        template.setKeySerializer(stringRedisSerializer);
//        template.setValueSerializer (redisObjectSerializer);
 //         return Template; 
        RedisTemplate <String, Object> Template = new new RedisTemplate <> ();
         // use serialization --- fastjson used herein StringRedis serialization 
        StringRedisSerializer stringRedisSerializer = new new StringRedisSerializer ( );
         // serialized value using values StringRedisSerializer 
        template.setValueSerializer (stringRedisSerializer); 
        template.setHashValueSerializer (stringRedisSerializer); 
        // serialization key using StringRedisSerializer 
        template.setKeySerializer ( new new StringRedisSerializer ()); 
        template.setHashKeySerializer ( new new StringRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

 

 
Redis knowledge added:
Redis can store the mapping between keys and 5 different types of data structures, these five types of data structures are String (String), List (list), the Set (set), the Hash (hash) and zset (orderly set).
 
 
For the "serialization / de-serialization" of data, offers a variety of alternative strategies (RedisSerializer)
1.JdkSerializationRedisSerializer: Access POJO scene objects using the serialization mechanism JDK itself, will serialize it pojo operated by ObjectInputStream / ObjectOutputStream, eventually redis-server is stored into a sequence of bytes. It is the most common sequence strategy.
2.StringRedisSerializer: Key value or string for the scene, according to the named charset string into a sequence of bytes of the coded data, is "newString (bytes, charset)" and "string.getBytes (charset)" direct encapsulation. Is the most lightweight and efficient strategies.
3.JacksonJsonRedisSerializer: jackson-json tool provides the ability to translate between javabean and json, pojo instance may be serialized format json redis stored, it is also possible to convert the data format into json pojo instance. Because jackson tool in the serialization and de-serialization, you need to explicitly specify the Class types, so this policy package together a bit more complicated.
4.OxmSerializer:提供了将javabean与xml之间的转换能力,目前可用的三方支持包括jaxb,apache-xmlbeans;redis存储的数据将是xml工具。不过使用此策略,编程将会有些难度,而且效率最低;不建议使用。【需要spring-oxm模块的支持】
 

Guess you like

Origin www.cnblogs.com/TJGKK/p/11075317.html