當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
Spring-Boot使用RedisCluster
生活随笔
收集整理的這篇文章主要介紹了
Spring-Boot使用RedisCluster
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
application.properties:
spring.redis.cluster.max-redirects=15 spring.redis.cluster.nodes=redis1:7000,redis1:7001,redis1:7002,redis1:7003,redis1:7004,redis2:7000,redis2:7001,redis2:7002,redis2:7003,redis2:7004,redis3:7000,redis3:7001,redis3:7002,redis3:7003,redis3:7004 spring.redis.database=0 spring.redis.pool.max-active=1000 spring.redis.pool.max-idle=800 spring.redis.pool.max-wait=3000 spring.redis.pool.min-idle=100使用RedisTemplate:
package com.pasenger.redis.services;import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;/*** RedisCluster Operator Service* Created by Pasenger on 2017/3/16.*/@Service @Setter @Getter @Slf4j public class RedisClusterService {@Autowiredprivate RedisTemplate<String, String> redisTemplate;/*** Redis Set String** @param key* @param value*/public void setString(String key, String value) {redisTemplate.opsForValue().set(key, value);}/*** Redis Setex String** @param key* @param value* @param second*/public void setExString(String key, String value, long second) {redisTemplate.opsForValue().set(key, value, second, TimeUnit.SECONDS);}/*** @param key* @return*/public long getTtl(String key) {return redisTemplate.getExpire(key);}/*** Redis Get String** @param key* @return*/public String getString(String key) {return redisTemplate.opsForValue().get(key);}/*** Redis Delete** @param key*/public void delete(String key) {redisTemplate.delete(key);} }單元測(cè)試:
package com.pasenger.redis.services;import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;/*** Created by Pasenger on 2017/3/16.*/ @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class TestRedisClusterService {@Autowiredprivate RedisClusterService redisClusterService;@Testpublic void TestSetString() {redisClusterService.setString("TT1", "TT");Assert.assertEquals("TT", redisClusterService.getString("TT1"));}@Testpublic void TestSetExString() {redisClusterService.setExString("TT2", "TT", 60);try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}Assert.assertTrue(redisClusterService.getTtl("TT2") < 60L);} }轉(zhuǎn)載于:https://my.oschina.net/pasenger/blog/865339
總結(jié)
以上是生活随笔為你收集整理的Spring-Boot使用RedisCluster的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 深入理解PHP异常和错误处理(6)PHP
- 下一篇: http消息当中,post和get两种请