Redis学习笔记二、使用方式一代码示例

1、主要介绍两张不同的使用方法:

package com.haoxiansheng.middleware.springMessage;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.haoxiansheng.middleware.MainApplicationTest;
import com.haoxiansheng.middleware.rabbitmq.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.Optional;

@Slf4j
public class RedisTest extends MainApplicationTest {


    @Autowired
    private RedisTemplate redisTemplate;  //配置中自定义注入 在直接自动装配

    @Autowired
    private ObjectMapper objectMapper;  // 定义JSON 序列化与反序列化框架类

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 采用RedisTemplate 将字符串信息写入缓存中并读取出来
     */
    @Test
    public void tetRedis() {

        log.info("=======开始RedisTemplate 操作组件实战");

        //定义字符串内容及存入缓存的Key
        final String content = "RedisTemplate实战字符串信息";
        final String key = "redis:template:one:string";

        ValueOperations valueOperations = redisTemplate.opsForValue(); //Redis 通用操作组件

        log.info("写入缓冲中的内容:{}", content);
        valueOperations.set(key, content);  //将信息写入缓冲中

        Object result = valueOperations.get(key); //从缓冲中读取内容
        log.info("读取出来的内容:{}", result);

    }

    /**
     * 采用RedisTemplate 将对象信息序列化为JSON 格式字符串后写入缓冲中
     * 然后将其读取出来, 最后反序列化解析其中的内容并展示在控制台上
     */
    @Test
    public void testRedisJSON() {
        log.info("==开始RedisTemplate组件实战操作==");

        User user = new User(1, "debug", "阿修罗");
        ValueOperations valueOperations = redisTemplate.opsForValue(); //Redis 通用操作组件
        final String key = "redis:template:one:object";

        /**
         *   将序列化后的信息写入缓冲中
         */
        try {
            final String content = objectMapper.writeValueAsString(user);
            valueOperations.set(key, content);
            log.info("写入缓冲对象的信息:{}", user);
        } catch (Exception e) {
            log.error("json 序列化失败:{}", user);
        }

        /**
         * 从缓冲中读取内容
         */
        Optional.ofNullable(valueOperations.get(key)).ifPresent(x -> {
            try {
                User userReceive = objectMapper.readValue(x.toString(), User.class);
                log.info("读取内容并反序列化后的结果:{}", userReceive);

            } catch (Exception e) {
                log.error("读取信息异常,{}", x);
            }

        });
    }

    /**
     * 下面是采用StringRedisTemplate 实现上面两个需求
     */

    @Test
    public void testRedisString() {

        log.info("======开始StringRedisTemplate 操作组件实战======");

        final String content = "StringRedisTemplate 实战字符串信息";
        final String key = "redis:String ==>";

        log.info("写入缓冲的内容:{}", content);
        ValueOperations valueOperations = stringRedisTemplate.opsForValue();
        valueOperations.set(key, content);

        Object result = valueOperations.get(key);
        log.info("读取出来的内容:{}", result);
    }

    @Test
    public void testRedisStringObject() {
        log.info(" ===开始StringRedisTemplate操作组件实战===");
        User user = new User(2, "JSON==Oject", "阿凡达");
        ValueOperations valueOperations = stringRedisTemplate.opsForValue();

        final String key = "redis:string:object ==>";
        try {
            final String content = objectMapper.writeValueAsString(user);
            valueOperations.set(key, content);
            log.info("写入缓冲对象的信息:{}", content);
        } catch (Exception e) {
            log.error("JSON 序列化失败:{}", user);
        }

        Optional.ofNullable(valueOperations.get(key)).ifPresent(x -> {
            try {
                User userReceive = objectMapper.readValue(x.toString(), User.class);
                log.info("读取信息并返回序列化的结果:{}", userReceive);
            } catch (Exception e) {
                log.error("读取信息异常: {}", x);
            }
        });
    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值