1.先启动redis服务:
D:\Program Files (x86)\Redis>redis-server.exe redis.windows.conf
启动报错:
解决方案:
2.打开redis客户端,配置连接:没有密码就不填写
3.redis依赖包:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.2</version>
</dependency>
4.application.yml配置文件:
Spring:
redis:
host: 127.0.0.1
port: 6379
5.RedisUtil工具类:
package com.lancet.util;
import com.lancet.exception.ErrorCodeException;
import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* @Description: Redis工具类
*/
public class RedisUtil {
private static Logger logger = Logger.getLogger(RedisUtil.class);
private static String SERVER_IP = PropertyUtil.getProperty("SERVER_IP");
private static int SERVER_PORT = Integer.parseInt(PropertyUtil.getProperty("SERVER_PORT"));
private static String SERVER_PASSWORD = PropertyUtil.getProperty("SERVER_PASSWORD");
// 可用连接实例的最大数目,默认值为8;
// 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
private static int MAX_ACTIVE = 1000;
// 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
private static int MAX_IDLE = 100;
// 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
private static int MAX_WAIT = 5;
// 超时时间
private static int TIMEOUT = 5 * 1000;
// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
private static boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
private static int count = 1;
static{
poolInit();
}
private static void initialPool() {
logger.info("☆初始化redisPool:☆当前第" + (count++) + "次初始化");
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setJmxEnabled(true);
config.setMaxIdle(8);
config.setMaxTotal(200);
config.setMaxWaitMillis(TIMEOUT);
config.setTestOnBorrow(true);
// jedisPool = new JedisPool(config, SERVER_IP, SERVER_PORT, TIMEOUT);
jedisPool = new JedisPool(config, SERVER_IP, SERVER_PORT, TIMEOUT, SERVER_PASSWORD);
logger.info("☆初始化redisPool成功☆");
} catch (Exception e) {
e.printStackTrace();
logger.warn("◆初始化redisPool失败◆");
}
}
private static synchronized void poolInit() {
if (jedisPool == null) {
initialPool();
}
}
public static void returnResource(Jedis jedis) {
if (jedis != null && jedisPool != null) {
jedis.close();
}
}
// 过期时间单位:秒
public static void set(String key, String value, int exprie) {
Jedis jedis = getJedis();
if (jedis != null) {
jedis.set(key, value);
if (exprie > 0) {
jedis.expire(key, exprie);
}
returnResource(jedis);
}
}
public static String get(String key) {
Jedis jedis = getJedis();
if (jedis == null) {
return null;
}
String s = jedis.get(key);
returnResource(jedis);
return s;
}
// 过期时间单位:秒
public static void setPojo(String key, Object obj, int exprie) {
Jedis jedis = getJedis();
if (jedis != null) {
jedis.set(key, JsonUtil.objectToJson(obj));
if (exprie > 0) {
jedis.expire(key, exprie);
}
returnResource(jedis);
}
}
@SuppressWarnings("unchecked")
public static <T> T getPojo(String key,Class clazz) {
Jedis jedis = getJedis();
T t = null;
if (jedis != null) {
t = (T) JsonGsonUtils.fromJson(jedis.get(key), clazz);
returnResource(jedis);
}
return t;
}
// redis分布式锁 解锁
public static void delete(String key) {
Jedis jedis = getJedis();
if (jedis != null) {
jedis.del(key);
returnResource(jedis);
}
}
}
6. 接口调用:
//24小时之内只能调用一次,超过24小时才能再次调用
String redisKey = "doctorid_"+doctorid+"_taskid_"+taskid;
String value =RedisUtil.get(redisKey);
if (StringUtil.isBlank(value)) {
RedisUtil.set(redisKey, String.valueOf(System.currentTimeMillis()), 86400);
return JsonUtil.toJson("更新成功");
} else {
return JsonUtil.toErrorCodeJson(ErrorCode._8050);
}