欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot高級(jí)教程之使用Redis實(shí)現(xiàn)session共享

 更新時(shí)間:2017年10月30日 17:11:54   作者:素文宅  
這篇文章主要為大家詳細(xì)介紹了Spring Boot高級(jí)教程之使用Redis實(shí)現(xiàn)session共享,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Redis是一個(gè)緩存消息中間件及具有豐富特性的鍵值存儲(chǔ)系統(tǒng)。Spring BootJedis客戶端庫(kù)和由Spring Data Redis提供的基于Jedis客戶端的抽象提供自動(dòng)配置。spring-boot-starter-redis'Starter POM'為收集依賴提供一種便利的方式。

引入spring-boot-starter-redis,在pom.xml配置文件中增加配置如下(基于之前章節(jié)“Spring Boot 構(gòu)建框架”中的pom.xml文件):

<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-redis</artifactId> 
</dependency>

可以注入一個(gè)自動(dòng)配置的RedisConnectionFactory,StringRedisTemplate或普通的跟其他Spring Bean相同的RedisTemplate實(shí)例。默認(rèn)情況下,這個(gè)實(shí)例將嘗試使用localhost:6379連接Redis服務(wù)器。

@Component
public class MyBean {
  private StringRedisTemplate template;
  
  @Autowired
  public MyBean(StringRedisTemplate template) {
    this.template = template;
  }
  // ...
}

如果添加一個(gè)自己的任何自動(dòng)配置類型的@Bean,它將替換默認(rèn)的(除了RedisTemplate的情況,它是根據(jù)bean的名稱'redisTemplate'而不是它的類型進(jìn)行排除的)。如果在classpath路徑下存在commons-pool2,默認(rèn)會(huì)獲得一個(gè)連接池工廠。

應(yīng)用使用Redis案例

添加配置文件,配置內(nèi)容如下:

# REDIS (RedisProperties)
# Redis服務(wù)器地址
spring.redis.host=192.168.0.58
# Redis服務(wù)器連接端口
spring.redis.port=6379 
# 連接超時(shí)時(shí)間(毫秒)
spring.redis.timeout=0

redis配置類,具體代碼如下:

import org.springframework.boot.context.properties.ConfigurationProperties; 
import org.springframework.stereotype.Component; 
 
@Component 
@ConfigurationProperties(prefix = "spring.redis") 
public class RedisConn { 
   
  private String host; 
   
  private int port; 
   
  private int timeout; 
 
 
  public String getHost() { 
    return host; 
  } 
 
  public void setHost(String host) { 
    this.host = host; 
  } 
 
  public int getPort() { 
    return port; 
  } 
 
  public void setPort(int port) { 
    this.port = port; 
  } 
 
  public int getTimeout() { 
    return timeout; 
  } 
 
  public void setTimeout(int timeout) { 
    this.timeout = timeout; 
  } 
 
  @Override 
  public String toString() { 
    return "Redis [localhost=" + host + ", port=" + port + ", timeout=" + timeout + "]"; 
  } 
   
 
}

注意:在RedisConn類中注解@ConfigurationProperties(prefix = "spring.Redis")的作用是讀取springboot的默認(rèn)配置文件信息中以spring.redis開頭的信息。

配置cache類

import java.lang.reflect.Method; 
import java.util.HashMap; 
import java.util.Map; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.cache.CacheManager; 
import org.springframework.cache.annotation.CachingConfigurerSupport; 
import org.springframework.cache.annotation.EnableCaching; 
import org.springframework.cache.interceptor.KeyGenerator; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.data.redis.cache.RedisCacheManager; 
import org.springframework.data.redis.connection.RedisConnectionFactory; 
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 
import org.springframework.data.redis.core.RedisTemplate; 
import org.springframework.data.redis.core.StringRedisTemplate; 
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; 
import org.springframework.stereotype.Component; 
 
import com.cachemodle.RedisConn; 
import com.fasterxml.jackson.annotation.JsonAutoDetect; 
import com.fasterxml.jackson.annotation.PropertyAccessor; 
import com.fasterxml.jackson.databind.ObjectMapper; 
 
/** 
* 
* @author sandsa redis cache service 
* 
*/ 
 
@Configuration 
@EnableCaching 
public class RedisConfig extends CachingConfigurerSupport { 
 
  @Autowired 
  private RedisConn redisConn; 
   
  /** 
  * 生產(chǎn)key的策略 
  * 
  * @return 
  */ 
 
  @Bean 
  @Override 
  public KeyGenerator keyGenerator() { 
    return new KeyGenerator() { 
 
      @Override 
      public Object generate(Object target, Method method, Object... params) { 
        StringBuilder sb = new StringBuilder(); 
        sb.append(target.getClass().getName()); 
        sb.append(method.getName()); 
        for (Object obj : params) { 
          sb.append(obj.toString()); 
        } 
        return sb.toString(); 
      } 
    }; 
 
  } 
 
  /** 
  * 管理緩存 
  * 
  * @param redisTemplate 
  * @return 
  */ 
 
  @SuppressWarnings("rawtypes") 
  @Bean 
  public CacheManager CacheManager(RedisTemplate redisTemplate) { 
    RedisCacheManager rcm = new RedisCacheManager(redisTemplate); 
    // 設(shè)置cache過期時(shí)間,時(shí)間單位是秒 
    rcm.setDefaultExpiration(60); 
    Map<String, Long> map = new HashMap<String, Long>(); 
    map.put("test", 60L); 
    rcm.setExpires(map); 
    return rcm; 
  } 
   
  /** 
  * redis 數(shù)據(jù)庫(kù)連接池 
  * @return 
  */ 
 
  @Bean 
  public JedisConnectionFactory redisConnectionFactory() { 
    JedisConnectionFactory factory = new JedisConnectionFactory(); 
    factory.setHostName(redisConn.getHost()); 
    factory.setPort(redisConn.getPort()); 
    factory.setTimeout(redisConn.getTimeout()); // 設(shè)置連接超時(shí)時(shí)間 
    return factory; 
  } 
 
  /** 
  * redisTemplate配置 
  * 
  * @param factory 
  * @return 
  */ 
  @SuppressWarnings({ "rawtypes", "unchecked" }) 
  @Bean 
  public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { 
    StringRedisTemplate template = new StringRedisTemplate(factory); 
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 
    ObjectMapper om = new ObjectMapper(); 
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 
    jackson2JsonRedisSerializer.setObjectMapper(om); 
    template.setValueSerializer(jackson2JsonRedisSerializer); 
    template.afterPropertiesSet(); 
    return template; 
  } 
 
}

分析:緩存類繼承的是CachingConfigurerSupport,它把讀取的配置文件信息的RedisConn類對(duì)象注入到這個(gè)類中。在這個(gè)類中keyGenerator()方法是key的生成策略,CacheManager()方法是緩存管理策略,redisConnectionFactory()是redis連接,redisTemplate()方法是redisTemplate配置信息,配置后使redis中能存儲(chǔ)Java對(duì)象。

測(cè)試配置是否成功,實(shí)例:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class TestRedis {
  @Autowired
  private StringRedisTemplate stringRedisTemplate; // 處理字符串
  
  @Autowired
  private RedisTemplate redisTemplate; // 處理對(duì)象
  @Test
  public void test() throws Exception {
    stringRedisTemplate.opsForValue().set("yoodb", "123");
    Assert.assertEquals("123", stringRedisTemplate.opsForValue().get("yoodb"));
  }
}

簡(jiǎn)單封裝的Redis工具類,代碼如下:

import java.io.Serializable; 
import java.util.Set; 
import java.util.concurrent.TimeUnit; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.data.redis.core.RedisTemplate; 
import org.springframework.data.redis.core.ValueOperations; 
import org.springframework.stereotype.Component; 
 
@Component 
public class RedisUtils { 
 
  @SuppressWarnings("rawtypes") 
  @Autowired 
  private RedisTemplate redisTemplate; 
 
  /** 
   * 批量刪除對(duì)應(yīng)的value 
   * 
   * @param keys 
   */ 
  public void remove(final String... keys) { 
    for (String key : keys) { 
      remove(key); 
    } 
  } 
 
  /** 
   * 批量刪除key 
   * 
   * @param pattern 
   */ 
  @SuppressWarnings("unchecked") 
  public void removePattern(final String pattern) { 
    Set<Serializable> keys = redisTemplate.keys(pattern); 
    if (keys.size() > 0) 
      redisTemplate.delete(keys); 
  } 
 
  /** 
   * 刪除對(duì)應(yīng)的value 
   * 
   * @param key 
   */ 
  @SuppressWarnings("unchecked") 
  public void remove(final String key) { 
    if (exists(key)) { 
      redisTemplate.delete(key); 
    } 
  } 
 
  /** 
   * 判斷緩存中是否有對(duì)應(yīng)的value 
   * 
   * @param key 
   * @return 
   */ 
  @SuppressWarnings("unchecked") 
  public boolean exists(final String key) { 
    return redisTemplate.hasKey(key); 
  } 
 
  /** 
   * 讀取緩存 
   * 
   * @param key 
   * @return 
   */ 
  @SuppressWarnings("unchecked") 
  public Object get(final String key) { 
    Object result = null; 
    ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); 
    result = operations.get(key); 
    return result; 
  } 
 
  /** 
   * 寫入緩存 
   * 
   * @param key 
   * @param value 
   * @return 
   */ 
  @SuppressWarnings("unchecked") 
  public boolean set(final String key, Object value) { 
    boolean result = false; 
    try { 
      ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); 
      operations.set(key, value); 
      result = true; 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
    return result; 
  } 
 
  /** 
   * 寫入緩存 
   * 
   * @param key 
   * @param value 
   * @return 
   */ 
  @SuppressWarnings("unchecked") 
  public boolean set(final String key, Object value, Long expireTime) { 
    boolean result = false; 
    try { 
      ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); 
      operations.set(key, value); 
      redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); 
      result = true; 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
    return result; 
  } 
 
}

查詢數(shù)據(jù)庫(kù)時(shí)自動(dòng)使用緩存,根據(jù)方法生成緩存,參考代碼如下:

@Service
public class UserService {
 @Cacheable(value = "redis-key")
 public UserInfo getUserInfo(Long id, String sex, int age, String name) {
   System.out.println("無(wú)緩存時(shí)調(diào)用----數(shù)據(jù)庫(kù)查詢");
   return new UserInfo(id, sex, age, name);
 }
}

注意:value的值就是緩存到redis中的key,此key是需要自己在進(jìn)行增加緩存信息時(shí)定義的key,用于標(biāo)識(shí)唯一性的。

Session 共享

分布式系統(tǒng)中session共享有很多不錯(cuò)的解決方案,其中托管到緩存中是比較常見的方案之一,下面利用Session-spring-session-data-redis實(shí)現(xiàn)session共享。

引入依賴,在pom.xml配置文件中增加如下內(nèi)容:

<dependency>
  <groupId>org.springframework.session</groupId>
  <artifactId>spring-session-data-redis</artifactId>
</dependency>

Session配置,具體代碼如下:

@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30)
public class SessionConfig {
}

maxInactiveIntervalInSeconds: 設(shè)置Session失效時(shí)間,使用Redis Session之后,原Spring Bootserver.session.timeout屬性不再生效。

測(cè)試實(shí)例,具體代碼如下:

@RequestMapping("uid")
String uid(HttpSession session) {
UUID uid = (UUID) session.getAttribute("uid");
if (uid == null) {
uid = UUID.randomUUID();
}
session.setAttribute("uid", uid);
return session.getId();
}

登錄redis服務(wù)端,輸入命令keys 'session*',查看緩存是否成功。

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

相關(guān)文章

  • 使用Java編寫串口程序的流程步驟

    使用Java編寫串口程序的流程步驟

    本文將介紹如何使用Java編寫串口程序,包括串口的基本概念、Java串口通信API的使用、串口程序的開發(fā)流程以及一些常見問題的解決方法等,希望通過本文的介紹,讀者能夠?qū)κ褂肑ava編寫串口程序有一個(gè)基本的了解,并能夠?qū)嵺`和應(yīng)用于實(shí)際項(xiàng)目中
    2023-11-11
  • Nacos注冊(cè)中心的部署與用法示例詳解

    Nacos注冊(cè)中心的部署與用法示例詳解

    注冊(cè)中心是微服務(wù)架構(gòu)中的紐帶,類似于“通訊錄”,它記錄了服務(wù)和服務(wù)地址的映射關(guān)系,本文通過示例代碼給大家介紹Nacos注冊(cè)中心的部署與用法,感興趣的朋友跟隨小編一起看看吧
    2022-02-02
  • SpringBoot使用CommandLineRunner接口完成資源初始化方式

    SpringBoot使用CommandLineRunner接口完成資源初始化方式

    這篇文章主要介紹了SpringBoot使用CommandLineRunner接口完成資源初始化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Java 繼承與多態(tài)超詳細(xì)梳理

    Java 繼承與多態(tài)超詳細(xì)梳理

    繼承就是可以直接使用前輩的屬性和方法。自然界如果沒有繼承,那一切都是處于混沌狀態(tài)。多態(tài)是同一個(gè)行為具有多個(gè)不同表現(xiàn)形式或形態(tài)的能力。多態(tài)就是同一個(gè)接口,使用不同的實(shí)例而執(zhí)行不同操作
    2022-04-04
  • SpringBoot中如何解決讀取properties文件讀取問題

    SpringBoot中如何解決讀取properties文件讀取問題

    這篇文章主要介紹了SpringBoot中如何解決讀取properties文件讀取問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Java 實(shí)戰(zhàn)項(xiàng)目錘煉之IT設(shè)備固定資產(chǎn)管理系統(tǒng)的實(shí)現(xiàn)流程

    Java 實(shí)戰(zhàn)項(xiàng)目錘煉之IT設(shè)備固定資產(chǎn)管理系統(tǒng)的實(shí)現(xiàn)流程

    讀萬(wàn)卷書不如行萬(wàn)里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java+SSM+jsp+mysql+maven實(shí)現(xiàn)一個(gè)IT設(shè)備固定資產(chǎn)管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平
    2021-11-11
  • Java基礎(chǔ)鞏固小項(xiàng)目點(diǎn)菜系統(tǒng)的實(shí)現(xiàn)

    Java基礎(chǔ)鞏固小項(xiàng)目點(diǎn)菜系統(tǒng)的實(shí)現(xiàn)

    這篇文章主要介紹了一個(gè)Java小項(xiàng)目點(diǎn)菜系統(tǒng)的實(shí)現(xiàn),主要是用的集合,適合正在學(xué)習(xí)Java的朋友拿來(lái)實(shí)戰(zhàn)練手,感興趣的朋友快來(lái)看看吧
    2022-03-03
  • JVM創(chuàng)建對(duì)象及訪問定位過程詳解

    JVM創(chuàng)建對(duì)象及訪問定位過程詳解

    這篇文章主要介紹了JVM創(chuàng)建對(duì)象及訪問定位過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Java之哈夫曼壓縮原理案例講解

    Java之哈夫曼壓縮原理案例講解

    這篇文章主要介紹了Java之哈夫曼壓縮原理案例講解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • SpringBoot + SpringSecurity 環(huán)境搭建的步驟

    SpringBoot + SpringSecurity 環(huán)境搭建的步驟

    這篇文章主要介紹了SpringBoot + SpringSecurity 環(huán)境搭建的步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2018-05-05

最新評(píng)論