亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springboot配置redis過程詳解

 更新時(shí)間:2019年09月03日 16:57:55   作者:Zs夏至  
這篇文章主要介紹了springboot配置redis過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

在springboot中,默認(rèn)繼承好了一套完好的redis包,可以直接使用,但是如果使用中出了錯(cuò)不容易找到錯(cuò)誤的原因,因此這里使用自己配置的redis;

需要使用的三個(gè)主要jar包:

<dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>
<dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
    </dependency>
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
    </dependency>

使用spring-boot-configuration-processor包主要是用來配置加載文件

package com.zs.springboot.config.redis;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
 * @Company
 * @Author Zs
 * 將這個(gè)類作為spring的一個(gè)組件,添加@ConfigurationProperties(prefix = "spring.redis")注解,就會(huì)默認(rèn)從application.properties
 * 文件中加載前綴為spring.redis的配置信息,配置文件中的配置字段與該類中的屬性一致,通過setter方法來設(shè)值
 * @Date Create in 2019/8/30
 **/
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {
  private String ip;
  private Integer[] ports;
  private Integer maxActive;
  private Integer maxWait;
  private Integer expire;
  public String getIp() {
    return ip;
  }
  public void setIp(String ip) {
    this.ip = ip;
  }
  public Integer[] getPorts() {
    return ports;
  }
  public void setPorts(Integer[] ports) {
    this.ports = ports;
  }
  public Integer getMaxActive() {
    return maxActive;
  }
  public void setMaxActive(Integer maxActive) {
    this.maxActive = maxActive;
  }
  public Integer getMaxWait() {
    return maxWait;
  }
  public void setMaxWait(Integer maxWait) {
    this.maxWait = maxWait;
  }
  public Integer getExpire() {
    return expire;
  }
  public void setExpire(Integer expire) {
    this.expire = expire;
  }
}

在application中配置redis:

然后配置redis的配置類,使用jdisCluster來操作redis:

package com.zs.springboot.config.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/**
 * @Company
 * @Author Zs
 * @Date Create in 2019/8/30
 **/
@Configuration
public class RedisConfiguration {
  private RedisProperties redisProperties;

  public RedisConfiguration(RedisProperties redisProperties) {
    this.redisProperties = redisProperties;
  }
  @Bean
  public JedisCluster jedisCluster() {
    Integer[] ports = redisProperties.getPorts();
    String host = redisProperties.getIp();
    Set<HostAndPort> hostAndPortSet = new HashSet<>();
    for (Integer port : ports) {
      hostAndPortSet.add(new HostAndPort(host, port));
    }
    return new JedisCluster(hostAndPortSet, redisProperties.getMaxActive(), redisProperties.getMaxWait());
  }
}

編輯redis的增刪該方法:

/**
 * @Company
 * @Author Zs
 * @Date Create in 2019/8/28
 **/
@Service
public class RedisService {
  @Autowired
  private JedisCluster jedisCluster;
  private static final String SET_SUCCESS = "OK";
  /**
   * 添加string數(shù)據(jù),成功返回code:200,失敗code:404
   * @param key
   * @param value
   * @return
   */
  public Map<String, Object> set(String key, Object value) {
    Map<String, Object> map = new HashMap<>();
    String result = jedisCluster.set(key, JsonUtil.toJsonString(value));
    if (SET_SUCCESS.equals(result)) {
      map.put(Status.SUCCESS.getCodeName(), Status.SUCCESS.getCode());
    } else {
      map.put(Status.FILED.getCodeName(), Status.FILED.getCode());
    }
    return map;
  }
  /**
   * 從redis根據(jù)key獲取string數(shù)據(jù)
   * @param key
   * @return
   */
  public String get(String key) {
    String jsonString = jedisCluster.get(key);
    if (jsonString==null || jsonString.equals("")) {
      return null;
    }
    return jsonString;
  }
  /**
   * 刪除string數(shù)據(jù)
   * @param key
   * @return
   */
  public Map<String, Object> del(String key) {
    Map<String, Object> map = new HashMap<>();
    Long del = jedisCluster.del(key);
    if (del>0) {
      map.put("code", 200);
    } else {
      map.put("code", 404);
    }
    return map;
  }
  /**
   * 設(shè)置失效時(shí)間
   * @param key
   * @param seconds
   * @return
   */
  public Long expire(String key,Integer seconds) {
    return jedisCluster.expire(key, seconds);
  }
}

注意不能在service層中注入service,如果需要可以在controller層將redisService做為參數(shù)傳遞進(jìn)去,如果在service層中注入其他的service對(duì)象,可能造成事務(wù)的串聯(lián),讀到臟數(shù)據(jù)。

該方法需要使用到j(luò)sonUtil類,將數(shù)據(jù)轉(zhuǎn)為json字符串存儲(chǔ)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論