redis----Springboot+RedisTemplate(SpringBoot操作redis)

1.SpringBoot操作redis

1)创建一个springBoot的框架的项目,并加上一个依赖

 2)添加配置,在yml文件中加入

spring: 
  redis:
    host: 192.168.23.133
    port: 6379
    password: foobared
    database: 1
    

3)使用redisTemplate来访问redis服务器

可以创建一个Controller的类

package com.redis.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.redis.Pojo.User;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/str")
public class RedisController {
    /**
     * 需要注入redis模板
     *
     * 对于RedisTemplate的泛型情况,
     * 可以使用<String, String>
     *       <Object, Object>
     *       或者不写泛型
     *
     *  注意,属性的名称必须为redisTemplate,因为按名称注入,框架创建的对象就是这个名字的
     */
      @Resource
      private RedisTemplate redisTemplate;
      @Resource
      private StringRedisTemplate stringRedisTemplate;
      //添加数据到redis
      @RequestMapping("/set")
      public void set(String key,String value){
          //序列化数据
          redisTemplate.setKeySerializer(new StringRedisSerializer());
          redisTemplate.setValueSerializer(new StringRedisSerializer());
          //调用方法来添加String数据
          //这个value代表的就是String类型,其他的类型和本身的类型都一样
          redisTemplate.opsForValue().set(key,value);
          System.out.println("redis保存字符串完毕");
      }
      @RequestMapping("/get")
      public Object get(String key){
          String result = (String) redisTemplate.opsForValue().get(key);
          return result;
      }

    @RequestMapping("/setex")
    public void setex(String key,String value,Integer time){
        //序列化数据
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        //调用方法来添加String数据
        //这个value代表的就是String类型,其他的类型和本身的类型都一样,设置字符串过期时间
        redisTemplate.opsForValue().set(key,value,time, TimeUnit.MICROSECONDS);
        System.out.println("redis保存字符串完毕");
    }
    @RequestMapping("/incr")
    public void incr(String key){
        //序列化数据
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        //调用方法来添加String数据
        //这个value代表的就是String类型,其他的类型和本身的类型都一样,设置次数,如果没有的话就是1,有的话就一次加一
        redisTemplate.opsForValue().increment(key);
        System.out.println("redis保存字符串完毕");
    }
    //报存一下对象
    @RequestMapping("/setJson")
    public void setJson(){
        User user = new User(1001,"yaya","亚亚");
        //序列化数据
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        //调用方法来添加String数据
        //这个value代表的就是String类型,其他的类型和本身的类型都一样,设置字符串过期时间
        redisTemplate.opsForValue().set("user-yaya", JSON.toJSONString(user));
        System.out.println("redis保存对象完毕");
    }

    @RequestMapping("/getJson")
    public Object getJson(){
        //序列化数据
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        //调用方法来添加String数据
        //这个value代表的就是String类型,其他的类型和本身的类型都一样,设置字符串过期时间

        String userJson =(String) redisTemplate.opsForValue().get("user-yaya");
        User user = JSONObject.parseObject(userJson,User.class);
        System.out.println("将redis缓存数据转换为对象");
        return user;
    }
}

2.springboot操作redis,关于对象的操作

1)首先添加一些配置

  <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.15</version>
        </dependency>

2)redis保存对象

    //保存一下对象
    @RequestMapping("/setJson")
    public void setJson(){
        User user = new User(1001,"yaya","亚亚");
        //序列化数据
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        //调用方法来添加String数据
        //这个value代表的就是String类型,其他的类型和本身的类型都一样,设置字符串过期时间
        redisTemplate.opsForValue().set("user-yaya", JSON.toJSONString(user));
        System.out.println("redis保存对象完毕");
    }

3)从redis中得到对象

@RequestMapping("/getJson")
    public Object getJson(){
        //序列化数据
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        //调用方法来添加String数据
        //这个value代表的就是String类型,其他的类型和本身的类型都一样,设置字符串过期时间

        String userJson =(String) redisTemplate.opsForValue().get("user-yaya");
        User user = JSONObject.parseObject(userJson,User.class);
        System.out.println("将redis缓存数据转换为对象");
        return user;
    }

猜你喜欢

转载自blog.csdn.net/weixin_58419099/article/details/131091307