引入jar包
<!--使用jedis -->
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
直接代码奉上:
package com.qudaowuyou.vote.util.cache;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import javax.annotation.PostConstruct;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Set;
/**
* @Author zbf
* @DATA 2019/11/4 17:58
* @ClassIntroduction
*/
@Component
@Slf4j
public class RedisCache {
@Value("${redis_host}")
private String host="47.110.158.207";
@Value("${redis_port}")
private int port=6379;
@Value("${redis_password}")
private String passWord="";
private JedisPool pool;
@PostConstruct
public void init() {
JedisPoolConfig config = new JedisPoolConfig();
// 最大空闲连接数, 应用自己评估,不要超过ApsaraDB for Redis每个实例最大的连接数
config.setMaxIdle(100);
// 最大连接数, 应用自己评估,不要超过ApsaraDB for Redis每个实例最大的连接数
config.setMaxTotal(300);
config.setMaxWaitMillis(1000);
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
pool = new JedisPool(config, host, Integer.valueOf(port), 3000, passWord);
try {
pool.getResource();
log.info("redis pool init success" + pool);
} catch (Exception e) {
log.info("redis get resource error", e);
}
}
public void destory() {
pool.close();
}
protected Jedis getResource() {
return pool.getResource();
}
protected void returnResource(Jedis jedis) {
jedis.close();
}
public <T> void innerPut(String key, T value) {
innerPut(key, value, 180);
}
public <T> void innerPut(String key, T value, int seconds) {
if (value == null) {
return;
}
byte[] data = stringToByte(JSON.toJSONString(value));
Jedis resource = getResource();
try {
byte[] keyBytes = key.getBytes();
resource.set(keyBytes, data);
resource.expire(keyBytes, seconds);
} finally {
resource.close();
}
}
public String simpleGet(String key) {
String value = null;
Jedis resource = getResource();
try {
byte[] data = resource.get(stringToByte(key));
if (data == null) {
return null;
}
value = byteToString(data);
} finally {
resource.close();
}
return value;
}
private String byteToString(byte[] data) {
try {
return new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
return null;
}
}
public void simplePutLast(String key, String value) {
if (value == null) {
return;
}
byte[] data = stringToByte(value);
Jedis resource = getResource();
try {
byte[] keyBytes = stringToByte(key);
resource.set(keyBytes, data);
} finally {
resource.close();
}
}
public <T> void simplePutLastBean(String key, T value) {
if (value == null) {
return;
}
byte[] data = stringToByte(JSON.toJSONString(value));
Jedis resource = getResource();
try {
byte[] keyBytes = key.getBytes();
resource.set(keyBytes, data);
} finally {
resource.close();
}
}
public void simplePut(String key, String value, int seconds) {
if (value == null) {
return;
}
byte[] data = stringToByte(value);
Jedis resource = getResource();
try {
byte[] keyBytes = stringToByte(key);
resource.set(keyBytes, data);
resource.expire(keyBytes, seconds);
} finally {
resource.close();
}
}
private byte[] stringToByte(String value) {
try {
return value.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
return null;
}
}
public <T> T innerGet(String key, Class<T> clazz) {
Jedis resource = getResource();
try {
byte[] data = resource.get(key.getBytes());
if (data == null) {
return null;
}
T value = JSON.parseObject(byteToString(data),clazz);
return value;
} finally {
resource.close();
}
}
public void innerRemove(String key) {
Jedis resource = getResource();
resource.del(key.getBytes());
resource.close();
}
public void batchDel(String pre_str) {
Jedis resource = getResource();
Set<String> set = resource.keys(pre_str + "*");
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String keyStr = it.next();
resource.del(keyStr);
}
}
public void setHost(String host) {
this.host = host;
}
public void setPort(int port) {
this.port = port;
}
}