redis结合sringboot使用

redis-api

还是向spring靠拢,采用spring的方式

spring.io-redis
springboot-redis

package com.lnf.demo;
import com.fastertarget="_blank">xml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.hash.Jackson2HashMapper;
import org.springframework.data.redis.hash.ObjectHashMapper;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
 * @ClassName
 * @Description
 * @Author: linengfeng
 * @Date: 2021/5/23 19:44
 */
@Component
public class TestRedis {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    @Qualifier("ooxx")
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    ObjectMapper objectMapper;
    public void testRedis(){
//        redisTemplate.opsForValue().set("k1","sskkss ");
//        Object k1 = redisTemplate.opsForValue().get("k1");
//        System.out.println(k1);
//        stringRedisTemplate.opsForValue().set("k1","sskkss ");
//        Object k1 = stringRedisTemplate.opsForValue().get("k1");
//        System.out.println(k1);
//        RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
//        redisConnection.set("k2".getBytes(),"002".getBytes());
//        byte[] bytes = redisConnection.get("k2".getBytes());
//        System.out.println(new String(bytes));
        //哈希
//        HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash();
//        hash.put("lnf","age","18");
//        hash.put("lnf","name","linengfeng");
//        Object o = hash.get("lnf", "name");
//        System.out.println(o);
//        Map<Object, Object> lnf = hash.entries("lnf");
//        System.out.println(lnf);
//        Person p = new Person();
//        p.setAge(16);
//        p.setName("zhangsan");
//        Jackson2HashMapper jm = new Jackson2HashMapper(objectMapper, false);
//        redisTemplate.opsForHash().putAll("lnf",jm.toHash(p));
//        Map lnf = redisTemplate.opsForHash().entries("lnf");
//        Person person = objectMapper.convertValue(lnf, Person.class);
//        System.out.println(person.getAge());
//        Person p = new Person();
//        p.setAge(50);
//        p.setName("wangwu");
//        Jackson2HashMapper jm = new Jackson2HashMapper(objectMapper, false);
//        stringRedisTemplate.opsForHash().putAll("wangwu",jm.toHash(p));
//        Map lnf = stringRedisTemplate.opsForHash().entries("wagnwu");
//        Person person = objectMapper.convertValue(lnf, Person.class);
//        System.out.println(person.getAge());
//        Person p = new Person();
//        p.setAge(50);
//        p.setName("wangwu");
//        //直接使用stringRedisTemplate会报错 java.lang.Integer cannot be cast to java.lang.String使用序列化
//        stringRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
//        Jackson2HashMapper jm = new Jackson2HashMapper(objectMapper, false);
//        stringRedisTemplate.opsForHash().putAll("wangwu",jm.toHash(p));
//        Map lnf = stringRedisTemplate.opsForHash().entries("wangwu");
//        Person person = objectMapper.convertValue(lnf, Person.class);
//        System.out.println(person.getAge());
//        Person p = new Person();
//        p.setAge(50);
//        p.setName("wangwu");
//        //自定义序列化MyTemplate已经设置value的序列化方式  @Qualifier("ooxx")
//        Jackson2HashMapper jm = new Jackson2HashMapper(objectMapper, false);
//        stringRedisTemplate.opsForHash().putAll("wangwu",jm.toHash(p));
//        Map lnf = stringRedisTemplate.opsForHash().entries("wangwu");
//        Person person = objectMapper.convertValue(lnf, Person.class);
//        System.out.println(person.getAge());
        //发布订阅
        //简单的
//        redisTemplate.convertAndSend("ooxx","hello");
//        stringRedisTemplate.convertAndSend("ooxx","hello");
        /**
          [root@localhost ~]# redis-cli -p 6380
          127.0.0.1:6380> SUBSCRIBE ooxx
          Reading messages... (press Ctrl-C to quit)
          1) "subscribe"
          2) "ooxx"
          3) (integer) 1
          1) "message"
          2) "ooxx"
          3) "\xac\xed\x00\x05t\x00\x05hello"
         */
//        RedisConnection cc = stringRedisTemplate.getConnectionFactory().getConnection();
//        cc.subscribe(new MessageListener() {
//            @Override
//            public void onMessage(Message message, byte[] bytes) {
//                byte[] body = message.getBody();
//                System.out.println(new String(body));
//            }
//        },"ooxx".getBytes());
//        while(true){
//            //循环等待程序不终止
//        }
        RedisConnection cc = stringRedisTemplate.getConnectionFactory().getConnection();
        cc.subscribe(new MessageListener() {
            @Override
            public void onMessage(Message message, byte[] bytes) {
                byte[] body = message.getBody();
                System.out.println(new String(body));
            }
        },"ooxx".getBytes());
        while(true){
            //循环等待程序不终止
            stringRedisTemplate.convertAndSend("ooxx","hello  from wo zi ji ");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
package com.lnf.demo;
/**
 * @ClassName
 * @Description
 * @Author: linengfeng
 * @Date: 2021/5/23 22:14
 */
public class Person {
    private String name;
    private Integer age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}
package com.lnf.demo;
import com.fasterxml.jackson.databind.ObjectMapper;
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.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
/**
 * @ClassName
 * @Description 自定义好的序列化器
 * @Author: linengfeng
 * @Date: 2021/5/23 22:41
 */
@Configuration
public class MyTemplate {
    @Bean
    public StringRedisTemplate ooxx(RedisConnectionFactory fc){
        StringRedisTemplate tmp = new StringRedisTemplate(fc);
        tmp.setHashValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
        return tmp;
    }
}
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-json</artifactId>
        </dependency>
    </dependencies>