中间件Redis(二)-- 使用redisTemplate进行读写

Redis-Demo

1. Redis自定义注入Bean组件配置:

对于Spring Boot项目整合Redis,最主要的Bean操作组件莫过于RedisTemplate跟StringRedisTemplate,后者其实是前者的一种特殊体现。而在项目中使用Redis的过程中,一般情况下是需要自定义配置上述两个操作Bean组件的,比如指定缓存中Key与Value的序列化策略等配置

package com.test.springboot.common;
import org.springframework.beans.factory.annotation.Autowired;
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.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class CommonConfig {
    // redis链接工厂
    @Autowired
    private RedisConnectionFactory redisConnectionFactory;
    //缓存操作组件redisTemplate的自定义配置
    @Bean
    public RedisTemplate<String, Object> redisTemplate(){
        //定义RedisTemplate实例
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        // 设置redis的链接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //指定大key序列化策略为String序列化,Value为JDK自带的序列化策略
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setKeySerializer(new JdkSerializationRedisSerializer());
        //指定hashKey序列化策略为String序列化-针对hash散列存储
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(){
        //采用默认配置即可
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
        return stringRedisTemplate;
    }
}

2. RedisTemplate实战

  • 在配置文件中配置
server.port=8081
# 使用的默认端口
spring.redis.host=127.0.0.1
spring.redis.port=6379
  • 采用RedisTemplate将字符串信息写入缓存中,并读取出来展示到控制台上。
package com.test.springboot.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class RedisController {
    private static final Logger log = LoggerFactory.getLogger(RedisController.class);
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    @RequestMapping("/one")
    public void one(){
        log.info("--------开始redisTemplate操作");
        final String content="redisTemplate实站字符串";
        final String key="redis";
        // Redis通用操作组件
        ValueOperations valueOperations = redisTemplate.opsForValue();
        // 将字符串写入缓存
        log.info(content);
        valueOperations.set(key, content);
        // 从缓存中读取内容
        Object result = valueOperations.get(key);
        log.info("内容是"+ result);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值