【22】redis 의 jedisPool 패키지(주해 실현)
4414 단어 redis
위 편 은 크게 다 르 지 않 습 니 다.설정 파일 을 사용 하 시 겠 습 니까?주석 을 사용 하 시 겠 습 니까?redis 클 러 스 터 를 사용 하 시 겠 습 니까?클 러 스 터 를 사용 하 시 겠 습 니까?
RedisPool.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* @description: RedisPool
**/
@Configuration
public class RedisPool {
Logger logger = LoggerFactory.getLogger(RedisPool.class);
@Value("${redis.host}")
private String host ;
@Value("${redis.auth}")
private String auth ;
@Value("${redis.maxIdle}")
private int maxIdle;
@Value("${redis.maxTotal}")
private int maxTotal;
@Value("${redis.port}")
private int port ;
@Value("${redis.timeout}")
private int timeout;
@Bean
public JedisPool redisPoolFactory() {
logger.info("JedisPool !!");
logger.info("redis :" + host + ":" + port);
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
//jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, auth);
return jedisPool;
}
}
RedisService.java
public interface RedisService {
String setex(String key,String token,int seconds);
String get(String key);
long del(String key);
}
RedisServiceImpl.java
import com.base.util.RedisPool;
import com.base.util.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* @description:
**/
@Service
public class RedisServiceImpl implements RedisService {
Logger logger = LoggerFactory.getLogger(RedisPool.class);
@Autowired
JedisPool jedisPool;
@Override
public String setex(String key, String token, int seconds) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.setex(key,seconds,token);
} catch (Exception e) {
logger.error("JedisUtil setex:",e);
} finally {
if (jedis != null) {
try {
jedis.close();
} catch (Exception e) {
logger.error("close redis conn fail : ",e);
}
}
}
return null;
}
@Override
public String get(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.get(key);
} catch (Exception e) {
logger.error("JedisUtil get:",e);
} finally {
if (jedis != null) {
try {
jedis.close();
} catch (Exception e) {
logger.error("close redis conn fail : ",e);
}
}
}
return null;
}
@Override
public long del(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.del(key);
} catch (Exception e) {
logger.error("JedisUtil del:",e);
} finally {
if (jedis != null) {
try {
jedis.close();
} catch (Exception e) {
logger.error("close redis conn fail : ",e);
}
}
}
return 0;
}
}
application.yml
server:
port: 8080
context-path: /sid
redis:
host: 192.168.2.240
auth: 123123
port: 6379
maxIdle: 200
maxTotal: 300
timeout: 3000
쓰다
@Autowired
RedisService redisService ;
redisService.setex("redis:key","testvalue",5000);
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Redis 해시에 대한 완벽한 가이드변경 가능하므로 필요에 따라 쉽게 변경하고 업데이트할 수 있습니다. Redis 해시는 구조가 평평하므로 JSON에서와 같이 여러 수준을 가질 수 없습니다. redis 해시의 명명 규칙은 hash:key 로 입력되므로...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.