spring-boot-starter-redis 整合

來源:互聯網
上載者:User
redis支援

代碼塊

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis --><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-redis</artifactId>    <version>1.0.1.RELEASE</version></dependency>

更多支援

http://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis redis配置

# REDIS (RedisProperties)  spring.redis.database= # database name  spring.redis.host=localhost # server host  spring.redis.password= # server password  spring.redis.port=6379 # connection port  spring.redis.pool.max-idle=8 # pool settings ...  spring.redis.pool.min-idle=0  spring.redis.pool.max-active=8  spring.redis.pool.max-wait=-1  spring.redis.sentinel.master= # name of Redis server  spring.redis.sentinel.nodes= # comma-separated list of host:port pairs  
JAVA代碼
package com.pay.channel.business;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.connection.RedisConnection;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.stereotype.Component;/** * redis * com.pay.channel.business.RedisBusiness * * @email h_anke@163.com * @create 2017-03-06 13:57 **/@Componentpublic class RedisBusiness {    private static final Logger logger = LoggerFactory.getLogger(RedisBusiness.class);    @Autowired    private RedisConnectionFactory redisConnectionFactory;    public void setEx(String key, String value , int expire) throws Exception{        RedisConnection connection = redisConnectionFactory.getConnection();        try {            connection.setEx(key.getBytes(), expire, value.getBytes());        }catch (Exception e){            logger.warn("redis 儲存資料出現情況,現發起一次重複串連", e);            if (connection != null)                connection.close();            connection = redisConnectionFactory.getConnection();            connection.setEx(key.getBytes(), expire, value.getBytes());        }finally {            if (connection != null)                connection.close();        }    }    public void del(String key) throws Exception{        RedisConnection connection = redisConnectionFactory.getConnection();        try {            connection.del(key.getBytes());        } finally {            if (connection != null)                connection.close();        }    }    public String get(String key) throws Exception{        RedisConnection connection = redisConnectionFactory.getConnection();        byte[] bytes = null;        try {            bytes = connection.get(key.getBytes());        }finally {            if (connection != null)                connection.close();        }        if (bytes == null) {            logger.warn("redis緩衝無此key,可能是逾時失效了");            throw new Exception("redis 失效");        }        return  new String(bytes);    }    public void set(String key,String value) throws Exception{        RedisConnection connection = redisConnectionFactory.getConnection();        try {            connection.set(key.getBytes(), value.getBytes());        }finally {            if (connection != null)                connection.close();        }    }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.