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();

    }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM