lishenghan 发表于 2018-11-3 13:36:23

springBoot(16):集成redis

package com.example.demo.utils;  

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

  
/**
  
* Redis服务类
  
*
  
* @Author: 我爱大金子
  
* @Description: Redis服务类
  
* @Date: Create in 12:02 2017/7/3
  
*/
  
@Component
  
public class RedisUtil {
  
    @Autowired
  
    private StringRedisTemplate stringRedisTemplate;
  
    public void set(String key, String value) {
  
      ValueOperations ops = this.stringRedisTemplate.opsForValue();
  
      if (!this.stringRedisTemplate.hasKey(key)) {
  
            ops.set(key, value);
  
            System.out.println("set key success");
  
      } else { // 存在则打印之前的 value 值
  
            System.out.println("this key = " + ops.get(key));
  
      }
  
    }
  
    public String get(String key) {
  
      return this.stringRedisTemplate.opsForValue().get(key);
  
    }
  
    public void del(String key) {
  
      this.stringRedisTemplate.delete(key);
  
    }
  
}


页: [1]
查看完整版本: springBoot(16):集成redis