RedisTemplate

RedisTemplate

实际项目中对redis的操作,一个是创建RedisConfig,一个是创建RedisUtil

RedisConfig中重写RedisTemplate,设置key的序列化方式,value的序列化方式等

RedisConfig模板

package com.example.redis02springboot.config;

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.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.net.UnknownHostException;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) throws UnknownHostException {
        // 自己开发一般直接设置成<String,Object>
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 默认设置连接工厂
        template.setConnectionFactory(factory);
        // 序列化配置
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        // String 序列化配置
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // 所有的key都采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // 所有的hashkey都采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // 所有的value都采用jackson的序列化方式
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // 所有的hashvalue都采用jackson的序列化方式
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

RedisUtil模板网上很多,这个只是其中一部分,具体用到的时候可以去网上copy

package com.example.redis02springboot.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisUtil {
    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    public Boolean hasKey(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}

实际调用

package com.example.redis02springboot;

import com.example.redis02springboot.pojo.User;
import com.example.redis02springboot.util.RedisUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.RedisTemplate;


@SpringBootTest
class Redis02SpringbootApplicationTests {
    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Autowired
    RedisUtil redisUtil;

    @Test
    void contextLoads() {
        // redisTemplate
        redisTemplate.opsForValue().set("string1", "v1"); //string
        redisTemplate.opsForList().leftPush("list1", 0, "v0"); //list
        redisTemplate.opsForList().rightPush("list1", 1, "v1");
        redisTemplate.opsForSet().add("set1", "v0", "v1", "v2");// set
        redisTemplate.opsForZSet().add("zset1", "v0", 1.1); // zset
        redisTemplate.opsForZSet().add("zset1", "v1", 5.3);
        redisTemplate.opsForHash().put("hash1", "name", "ohmydream"); // hashmap
        redisTemplate.opsForGeo().add("geo1", new Point(116.20, 39.56), 1);// geospatial
        redisTemplate.opsForHyperLogLog().add("pf1", "v1"); // hyperloglog
        redisTemplate.opsForValue().setBit("bitmap", 0, false); // bitmap
        System.out.println(redisTemplate.opsForValue().get("string1"));

        System.out.println(redisUtil.hasKey("set1"));
        // clear db
//        RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
//        connection.flushDb(); 
//        connection.flushAll();


    }

    @Test
    public void test() throws JsonProcessingException {
        User user = new User("狂神", 3);
//        String s = new ObjectMapper().writeValueAsString(user);
        redisTemplate.opsForValue().set("user", user);
        System.out.println(redisTemplate.opsForValue().get("user"));
        new Thread().start();

    }

}
posted @ 2022-03-18 10:19  Oh,mydream!  阅读(1602)  评论(0编辑  收藏  举报