Spring-boot-starter-redis Learning Notes

Source: Internet
Author: User
Tags redis serialization

Personal notes are for informational purposes only.

1, spring data Redis recommended Jedis,jedis is a Redis Java client.

<dependency>  
	    <groupId>org.springframework.boot</groupId><!--will be accompanied by the introduction of Jedis package-->  
	    <artifactId>spring-boot-starter-redis</artifactId>  
</dependency>


2, redisconnectionfactory

The spring data Redis is connected to the Redis through redisconnectionfactory to obtain a valid redisconnection. Redisconnection is responsible for establishing and processing and redis back-end communications. Redisconnection provides getnativeconnection to return the underlying connection for communication.


3,redistemplate

The spring data Redis supports low-level connector connections to Redis via connectors, and supports high-level, friendly template classes redistemplate,redistemplate based on low-level connection. Redisconnection receive or return a byte array requires itself to process the connection, such as shutting down the connection, while Redistemplate is responsible for handling serialization and drag and managing the connection. Redistemplate provides operations views, such as (Bound) valueoperations, (Bound) listoperations, (Bound) setoperations, (Bound) zsetoperations, (Bound) Hashoperations. Redistemplate are thread-safe and can be used in multiple instances.

Redistemplate The default selection is java-based serialization, you can switch to another serialization mode, or set Enableddefaultserializer to False or set the serializer to NULL, The redistemplate uses raw byte arrays to represent the data. Other serialization methods include protocol Buffer,json, recursive binary byte throttling, and so on.

Stringredistemplate is the only subclass of Redistemplate.

Redistemplate two commonly used serialization classes are defined by default

Private redisserializer<?> Defaultserializer = Newjdkserializationredisserializer (); As well as private redisserializer<string> Stringserializer = Newstringredisserializer ();

4. Serialization of

Before the serialization mode is configured, use the

Redistemplate.opsforvalue (). Set (key, value);
The resulting key and value will all be serialized digits or symbols, such as

To solve this problem, you need to serialize key and value, if the XML configuration

We inject the official Keyserializer,valueserializer,hashkeyserializer directly, then we need to write our own Rediscacheconfig configuration class using annotations.

There are mainly several classes to implement for caching:

1, CacheManager cache Manager;

2, the concrete operation realization class;

3, the CacheManager factory class (This can use the configuration file configuration to inject, also can be implemented by the encoding method);

4, the Cache key production strategy (of course, spring's own generation strategy, but in the Redis client to view the words is serialized key, for our naked eye is the feeling is garbled, here we first use the cache strategy with).

/** * Cache Management (annotations) * @author Administrator/@Configuration @EnableCaching//Enable caching meaning public class CacheConfig extends Cach ingconfigurersupport{/** * Custom key. This method will generate a unique key based on the class name + method name + The value of all parameters, even if the Value property in @cacheable is the same, the key
     Will not be the same.
       * * * @Override public keygenerator keygenerator () {System.out.println ("rediscacheconfig.keygenerator ()"); Returnnew Keygenerator () {@Override public object generate (object O, Method method, object. . objects) {//This'll generate a unique key of the class name, the method name//and all met
              Hod parameters appended.
              StringBuilder sb = new StringBuilder ();
              Sb.append (O.getclass (). GetName ());
              Sb.append (Method.getname ());
              for (Object obj:objects) {sb.append (obj.tostring ());
              } System.out.println ("keygenerator=" + sb.tostring ()); Returnsb.tostring ();
           }
       }; } * * @Bean public CacheManager CacheManager (redistemplate redistemplate) {Rediscachemanager RCM
       = new Rediscachemanager (redistemplate); ///Set cache expiration/rcm.setdefaultexpiration (60);//sec/Set Value expiration Time map<string,long> map=new Ha
        Shmap ();
        Map.put ("Test", 60L);
    Rcm.setexpires (map); */return RCM; /** * Redistemplate Configuration * @param factory * @return * * * @Bean public redistemplate<string , string> Redistemplate (Redisconnectionfactory Factory) {stringredistemplate template = new Stringredistemplat
        E (Factory); Defines the key serialization method//redisserializer<string> Redisserializer = new Stringredisserializer (); The//long type will receive exception information; we need the above. Custom key generation policy, generally not necessary//define the serialization way of value jackson2jsonredisserializer Jackson2jsonredisserializer = new JACKSON2JSO
        Nredisserializer (Object.class);
  Objectmapper om = new Objectmapper ();      Om.setvisibility (Propertyaccessor.all, JsonAutoDetect.Visibility.ANY);
        Om.enabledefaulttyping (ObjectMapper.DefaultTyping.NON_FINAL);
        
       Jackson2jsonredisserializer.setobjectmapper (OM);
        Template.setkeyserializer (Redisserializer);
        Template.setvalueserializer (Jackson2jsonredisserializer);
        Template.sethashvalueserializer (Jackson2jsonredisserializer);
        Template.afterpropertiesset ();
    return template; }

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.